From 65de2ecf7013c3ad32d9669bf530a46d988d281c Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Fri, 22 Nov 2024 15:02:12 +0100 Subject: [PATCH 1/3] Check $projectDir/../node_modules for RN installation in gradle --- android/build.gradle | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 81c29a720a..2c6cc8d11f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -160,16 +160,34 @@ repositories { 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}" - } + return + } + + // This is legacy code, I'm not sure why it works in certain scenarios but it was reported that one of our + // projects needs this. + File secondStandardRnAndroidDirLocation = file("$projectDir/../node_modules/react-native/android") + if (secondStandardRnAndroidDirLocation.exists()) { + url secondStandardRnAndroidDirLocation + return + } + + // We're in non standard setup - try to use node resolver to locate the react-native package. + String searchResult = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim() + + // file() constructor fails in case string is null or blank + if (searchResult == null || searchResult.isBlank()) { + println "[RNScreens] Failed to resolve react-native directory. Attempted locations: ${standardRnAndroidDirLocation}, ${secondStandardRnAndroidDirLocation} and search with node resolution algorithm failed." + return } + + File reactNativePackage = file(searchResult) + def rnAndroidDirLocation = "$reactNativePackage.parentFile/android" + if (reactNativePackage.exists()) { + url rnAndroidDirLocation + return + } + + println "[RNScreens] Failed to resolve react-native directory. Attempted locations: ${standardRnAndroidDirLocation}, ${secondStandardRnAndroidDirLocation}, ${rnAndroidDirLocation}" } mavenCentral() From c55b9cd1f299b480b861b211ffa69628e2fc02ec Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Fri, 22 Nov 2024 16:08:07 +0100 Subject: [PATCH 2/3] Extract rn dir finding logic to function --- android/build.gradle | 74 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 2c6cc8d11f..315d943126 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -38,6 +38,42 @@ def isNewArchitectureEnabled() { return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true" } +def resolveReactNativeDirectory() { + def userDefinedRnDirPath = safeAppExtGet("REACT_NATIVE_NODE_MODULES_DIR", null) + if (userDefinedRnDirPath != null) { + return file(userDefinedRnDirPath) + } + + File standardRnDirFile = file("$rootDir/../node_modules/react-native/") + if (standardRnDirFile.exists()) { + return standardRnDirFile + } + + // This is legacy code, I'm not sure why it works in certain scenarios but it was reported that one of our + // projects needs this. + File legacyRnDirFile = file("$projectDir/../node_modules/react-native/") + if (legacyRnDirFile.exists()) { + return legacyRnDirFile + } + + // We're in non standard setup, e.g. monorepo - try to use node resolver to locate the react-native package. + String maybeRnPackagePath = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim() + + File nodeResolverRnDirFile = null + // file() constructor fails in case string is null or blank + if (maybeRnPackagePath != null && !maybeRnPackagePath.isBlank()) { + File maybeRnPackageFile = file(maybeRnPackagePath) + nodeResolverRnDirFile = maybeRnPackageFile.parentFile + if (nodeResolverRnDirFile.exists()) { + return nodeResolverRnDirFile + } + } + + throw new Exception("[RNScreens] Failed to resolve react-native directory. " + + "Attempted locations: ${standardRnDirFile}, ${legacyRnDirFile} and ${nodeResolverRnDirFile}. " + + "You should set project extension property (in `app/build.gradle`) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native.") +} + // spotless is only accessible within react-native-screens repo if (isRunningInContextOfScreensRepo()) { apply from: 'spotless.gradle' @@ -151,43 +187,7 @@ android { repositories { maven { - // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm - - // 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 - return - } - - // This is legacy code, I'm not sure why it works in certain scenarios but it was reported that one of our - // projects needs this. - File secondStandardRnAndroidDirLocation = file("$projectDir/../node_modules/react-native/android") - if (secondStandardRnAndroidDirLocation.exists()) { - url secondStandardRnAndroidDirLocation - return - } - - // We're in non standard setup - try to use node resolver to locate the react-native package. - String searchResult = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim() - - // file() constructor fails in case string is null or blank - if (searchResult == null || searchResult.isBlank()) { - println "[RNScreens] Failed to resolve react-native directory. Attempted locations: ${standardRnAndroidDirLocation}, ${secondStandardRnAndroidDirLocation} and search with node resolution algorithm failed." - return - } - - File reactNativePackage = file(searchResult) - def rnAndroidDirLocation = "$reactNativePackage.parentFile/android" - if (reactNativePackage.exists()) { - url rnAndroidDirLocation - return - } - - println "[RNScreens] Failed to resolve react-native directory. Attempted locations: ${standardRnAndroidDirLocation}, ${secondStandardRnAndroidDirLocation}, ${rnAndroidDirLocation}" + url "${resolveReactNativeDirectory()}/android" } mavenCentral() From 726f373f44f7d13bd6b115e1d1221346ed22a32d Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Fri, 22 Nov 2024 16:10:09 +0100 Subject: [PATCH 3/3] Minor adjustement --- android/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 315d943126..909ba779cd 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -63,8 +63,8 @@ def resolveReactNativeDirectory() { // file() constructor fails in case string is null or blank if (maybeRnPackagePath != null && !maybeRnPackagePath.isBlank()) { File maybeRnPackageFile = file(maybeRnPackagePath) - nodeResolverRnDirFile = maybeRnPackageFile.parentFile - if (nodeResolverRnDirFile.exists()) { + if (maybeRnPackageFile.exists()) { + nodeResolverRnDirFile = maybeRnPackageFile.parentFile return nodeResolverRnDirFile } }