From 2c7cb1d2d71a3dd492963905adcdd51692db85e1 Mon Sep 17 00:00:00 2001 From: Nick Morgan Date: Fri, 6 Sep 2024 16:39:44 -0400 Subject: [PATCH 1/4] Using node resolver to find react native package for better monorepo support --- android/build.gradle | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 03f63baafc..d4f8dbfa2b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -152,10 +152,18 @@ android { repositories { maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm - // Matches the RN Hello World template - // https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/local-cli/templates/HelloWorld/android/build.gradle#L21 - url "$projectDir/../node_modules/react-native/android" + + // Use node resolver to locate react-native package + def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, projectDir).text.trim()) + if (reactNativePackage.exists()) { + url "$reactNativePackage.parentFile/android" + } + // Fallback to react-native package colocated in node_modules + else { + url "$projectDir/../node_modules/react-native/android" + } } + mavenCentral() mavenLocal() google() From 31f663ac6dc62f8bd16c838216a3aecb0b044038 Mon Sep 17 00:00:00 2001 From: Nick Morgan Date: Mon, 9 Sep 2024 15:35:33 -0400 Subject: [PATCH 2/4] Using rootDir instead of projectDir --- android/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index d4f8dbfa2b..3dc14f6439 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -154,13 +154,13 @@ repositories { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm // Use node resolver to locate react-native package - def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, projectDir).text.trim()) + def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()) if (reactNativePackage.exists()) { url "$reactNativePackage.parentFile/android" } // Fallback to react-native package colocated in node_modules else { - url "$projectDir/../node_modules/react-native/android" + url "$rootDir/../node_modules/react-native/android" } } From b756626427fbb9691a87e5582498fed0d1ef08b0 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Wed, 25 Sep 2024 12:24:01 +0200 Subject: [PATCH 3/4] TOREVERT: DEBUGGING SETUP --- android/build.gradle | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 3dc14f6439..5a9a99a48b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -152,15 +152,25 @@ android { repositories { maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm + def rnPackage = "$projectDir/../node_modules/react-native/android" + url "$projectDir/../node_modules/react-native/android" + println("old solution") + println(rnPackage) - // Use node resolver to locate react-native package +// // Use node resolver to locate react-native package def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()) if (reactNativePackage.exists()) { + def newRnPackage = "$reactNativePackage.parentFile/android" + println("eagerly found") + println(newRnPackage) url "$reactNativePackage.parentFile/android" } // Fallback to react-native package colocated in node_modules - else { - url "$rootDir/../node_modules/react-native/android" + { + def newRnPackage = "$rootDir/../node_modules/react-native/android" + println("modified old solution") + println(newRnPackage) +// url "$rootDir/../node_modules/react-native/android" } } From 3dc84f8b8e72a68206f84859e39b2a29c5235132 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Wed, 25 Sep 2024 16:43:44 +0200 Subject: [PATCH 4/4] Change the order of lookup --- android/build.gradle | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 5a9a99a48b..81c29a720a 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -152,25 +152,23 @@ android { repositories { maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm - def rnPackage = "$projectDir/../node_modules/react-native/android" - url "$projectDir/../node_modules/react-native/android" - println("old solution") - println(rnPackage) - -// // Use node resolver to locate react-native package - def reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()) - if (reactNativePackage.exists()) { - def newRnPackage = "$reactNativePackage.parentFile/android" - println("eagerly found") - println(newRnPackage) - url "$reactNativePackage.parentFile/android" - } - // Fallback to react-native package colocated in node_modules - { - def newRnPackage = "$rootDir/../node_modules/react-native/android" - println("modified old solution") - println(newRnPackage) -// url "$rootDir/../node_modules/react-native/android" + + // First look for the standard location of react-native, as in RN Hello World template + // https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/local-cli/templates/HelloWorld/android/build.gradle#L21 + // TODO(kkafar): Note, that in latest template app https://github.com/react-native-community/template/blob/0f4745b7a9d84232aeedec2def8d75ab9b050d11/template/android/build.gradle + // this is not specified at all. + File standardRnAndroidDirLocation = file("$rootDir/../node_modules/react-native/android") + if (standardRnAndroidDirLocation.exists()) { + url standardRnAndroidDirLocation + } else { + // We're in non standard setup - try to use node resolver to locate the react-native package. + File reactNativePackage = file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()) + def rnAndroidDirLocation = "$reactNativePackage.parentFile/android" + if (reactNativePackage.exists()) { + url rnAndroidDirLocation + } else { + println "[RNScreens] Failed to resolve react-native directory. Attempted locations: ${standardRnAndroidDirLocation}, ${rnAndroidDirLocation}" + } } }