Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): improve RN detection in build scripts #2537

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String searchResult = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()
String searchResult = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()
if (searchResult == null || searchResult.isBlank()) {
searchResult = ["node", "--print", "require.resolve('react-native/package.json')"].execute(null, projectDir).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()
Expand Down
Loading