Skip to content

Commit b91c37c

Browse files
author
Fran Montiel
committed
Update build.gradle to use local version of react-native
- This change is taken from the build.gradle created after running this command: npx @react-native-community/bob create react-native-awesome-module - Info taken from here: https://reactnative.dev/docs/native-modules-setup
1 parent e3dd24d commit b91c37c

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

android/build.gradle

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,71 @@ android {
3030
repositories {
3131
jcenter()
3232
google()
33+
34+
def found = false
35+
def defaultDir = null
36+
def androidSourcesName = 'React Native sources'
37+
38+
if (rootProject.ext.has('reactNativeAndroidRoot')) {
39+
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
40+
} else {
41+
defaultDir = new File(
42+
projectDir,
43+
'/../../../node_modules/react-native/android'
44+
)
45+
}
46+
47+
if (defaultDir.exists()) {
48+
maven {
49+
url defaultDir.toString()
50+
name androidSourcesName
51+
}
52+
53+
logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
54+
found = true
55+
} else {
56+
def parentDir = rootProject.projectDir
57+
58+
1.upto(5, {
59+
if (found) return true
60+
parentDir = parentDir.parentFile
61+
62+
def androidSourcesDir = new File(
63+
parentDir,
64+
'node_modules/react-native'
65+
)
66+
67+
def androidPrebuiltBinaryDir = new File(
68+
parentDir,
69+
'node_modules/react-native/android'
70+
)
71+
72+
if (androidPrebuiltBinaryDir.exists()) {
73+
maven {
74+
url androidPrebuiltBinaryDir.toString()
75+
name androidSourcesName
76+
}
77+
78+
logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
79+
found = true
80+
} else if (androidSourcesDir.exists()) {
81+
maven {
82+
url androidSourcesDir.toString()
83+
name androidSourcesName
84+
}
85+
86+
logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
87+
found = true
88+
}
89+
})
90+
}
91+
92+
if (!found) {
93+
throw new GradleException(
94+
"${project.name}: unable to locate React Native android sources. " +
95+
"Ensure you have you installed React Native as a dependency in your project and try again."
96+
)
97+
}
3398
}
3499

35100
dependencies {

0 commit comments

Comments
 (0)