Skip to content

Commit 44d2823

Browse files
authored
fix(android): fix compatibility with react-native@nightly (#1169)
1 parent 38100f7 commit 44d2823

File tree

4 files changed

+66
-45
lines changed

4 files changed

+66
-45
lines changed

android/app/build.gradle

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ plugins {
1010
id("org.jetbrains.kotlin.kapt") version "${kotlinVersion}"
1111
}
1212

13+
def enableNewArchitecture = isNewArchitectureEnabled(project)
14+
def reactNativeVersion = getPackageVersionNumber("react-native", rootDir)
1315
def reactNativePath = file(findNodeModulesPath("react-native", rootDir))
1416
def codegenPath = file(findNodeModulesPath("react-native-codegen", reactNativePath))
1517

16-
if (isNewArchitectureEnabled(project)) {
18+
if (reactNativeVersion == 0 || enableNewArchitecture) {
1719
apply(plugin: "com.facebook.react")
1820

19-
react {
20-
codegenDir = codegenPath
21-
reactNativeDir = reactNativePath
21+
if (enableNewArchitecture) {
22+
react {
23+
codegenDir = codegenPath
24+
reactNativeDir = reactNativePath
25+
}
2226
}
2327
}
2428

@@ -27,8 +31,6 @@ if (isNewArchitectureEnabled(project)) {
2731
// https://github.com/react-native-community/cli/blob/6cf12b00c02aca6d4bc843446394331d71a9749e/packages/platform-android/src/commands/runAndroid/index.ts#L180
2832
buildDir = "${rootDir}/${name}/build"
2933

30-
def reactNativeVersion = getPackageVersionNumber("react-native", rootDir)
31-
3234
repositories {
3335
maven {
3436
url = uri("${reactNativePath}/android")
@@ -38,7 +40,7 @@ repositories {
3840
google()
3941

4042
// TODO: Remove this block when we drop support for 0.64.
41-
if (reactNativeVersion < 6500) {
43+
if (reactNativeVersion > 0 && reactNativeVersion < 6500) {
4244
// Artifacts for 0.65+ are published to Maven Central. If we're on an
4345
// older version, we still need to use JCenter.
4446
// noinspection JcenterRepositoryObsolete
@@ -70,7 +72,7 @@ project.ext.react = [
7072
enableFabric : isFabricEnabled(project),
7173
enableFlipper : getFlipperVersion(rootDir),
7274
enableHermes : true,
73-
enableNewArchitecture: isNewArchitectureEnabled(project),
75+
enableNewArchitecture: enableNewArchitecture,
7476
]
7577

7678
project.ext.signingConfigs = getSigningConfigs()
@@ -86,7 +88,7 @@ android {
8688

8789
// TODO: Remove this block when minSdkVersion >= 24. See
8890
// https://stackoverflow.com/q/53402639 for details.
89-
if (reactNativeVersion < 6900) {
91+
if (reactNativeVersion > 0 && reactNativeVersion < 6900) {
9092
compileOptions {
9193
sourceCompatibility(JavaVersion.VERSION_1_8)
9294
targetCompatibility(JavaVersion.VERSION_1_8)
@@ -95,7 +97,7 @@ android {
9597

9698
kotlinOptions {
9799
allWarningsAsErrors = true
98-
if (reactNativeVersion < 6900) {
100+
if (reactNativeVersion > 0 && reactNativeVersion < 6900) {
99101
jvmTarget = JavaVersion.VERSION_1_8
100102
} else {
101103
jvmTarget = JavaVersion.VERSION_11
@@ -129,7 +131,7 @@ android {
129131

130132
if (project.ext.react.enableNewArchitecture) {
131133
externalNativeBuild {
132-
if (reactNativeVersion < 7000) {
134+
if (reactNativeVersion > 0 && reactNativeVersion < 7000) {
133135
ndkBuild {
134136
arguments "APP_PLATFORM=android-${project.ext.minSdkVersion}",
135137
"APP_STL=c++_shared",
@@ -172,7 +174,7 @@ android {
172174

173175
if (project.ext.react.enableNewArchitecture) {
174176
externalNativeBuild {
175-
if (reactNativeVersion < 7000) {
177+
if (reactNativeVersion > 0 && reactNativeVersion < 7000) {
176178
ndkBuild {
177179
path "${projectDir}/src/main/jni/Android.mk"
178180
}
@@ -199,7 +201,7 @@ android {
199201
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
200202
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
201203

202-
if (reactNativeVersion < 7000) {
204+
if (reactNativeVersion > 0 && reactNativeVersion < 7000) {
203205
// Due to a bug in AGP, we have to explicitly set a dependency
204206
// between configureNdkBuild* tasks and the preBuild tasks. This can
205207
// be removed once this issue is resolved:
@@ -214,7 +216,7 @@ android {
214216
dependsOn("preReleaseBuild")
215217
}
216218
}
217-
} else if (reactNativeVersion < 7100) {
219+
} else if (reactNativeVersion > 0 && reactNativeVersion < 7100) {
218220
// Due to a bug in AGP, we have to explicitly set a dependency
219221
// between configureCMakeDebug* tasks and the preBuild tasks. This can
220222
// be removed once this issue is resolved:
@@ -238,24 +240,31 @@ android {
238240
}
239241
}
240242

241-
def version = getPackageVersion("react-native", rootDir)
242-
def allAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}.aar")
243-
244-
def prepareDebugJSI = tasks.register("prepareDebugJSI", Copy) {
245-
def debugAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}-debug.aar")
246-
from(zipTree(debugAar.exists() ? debugAar : allAar).matching({ it.include "**/libjsi.so" }))
247-
into("${buildDir}/outputs/jniLibs/debug")
248-
}
243+
// Nightlies are downloaded from Sonatype and cannot be found under
244+
// `node_modules`. Instead, they can be found in Gradle's cache folder,
245+
// `.gradle/caches/modules-2/files-2.1/com.facebook.react/react-native`.
246+
// For now, we will simply disable this step as we only need to verify
247+
// that things build.
248+
if (reactNativeVersion > 0) {
249+
def version = getPackageVersion("react-native", rootDir)
250+
def allAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}.aar")
251+
252+
def prepareDebugJSI = tasks.register("prepareDebugJSI", Copy) {
253+
def debugAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}-debug.aar")
254+
from(zipTree(debugAar.exists() ? debugAar : allAar).matching({ it.include "**/libjsi.so" }))
255+
into("${buildDir}/outputs/jniLibs/debug")
256+
}
249257

250-
def prepareReleaseJSI = tasks.register("prepareReleaseJSI", Copy) {
251-
def releaseAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}-release.aar")
252-
from(zipTree(releaseAar.exists() ? releaseAar : allAar).matching({ it.include "**/libjsi.so" }))
253-
into("${buildDir}/outputs/jniLibs/release")
254-
}
258+
def prepareReleaseJSI = tasks.register("prepareReleaseJSI", Copy) {
259+
def releaseAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}-release.aar")
260+
from(zipTree(releaseAar.exists() ? releaseAar : allAar).matching({ it.include "**/libjsi.so" }))
261+
into("${buildDir}/outputs/jniLibs/release")
262+
}
255263

256-
afterEvaluate {
257-
preDebugBuild.dependsOn(prepareDebugJSI)
258-
preReleaseBuild.dependsOn(prepareReleaseJSI)
264+
afterEvaluate {
265+
preDebugBuild.dependsOn(prepareDebugJSI)
266+
preReleaseBuild.dependsOn(prepareReleaseJSI)
267+
}
259268
}
260269
}
261270

@@ -320,7 +329,7 @@ android {
320329

321330
// TODO: Remove this block when we drop support for 0.67.
322331
// https://github.com/facebook/react-native/commit/ce74aa4ed335d4c36ce722d47937b582045e05c4
323-
if (reactNativeVersion < 6800) {
332+
if (reactNativeVersion > 0 && reactNativeVersion < 6800) {
324333
main.java.srcDirs += "src/reactinstanceeventlistener-pre-0.68/java"
325334
} else {
326335
main.java.srcDirs += "src/reactinstanceeventlistener-0.68/java"
@@ -341,8 +350,14 @@ dependencies {
341350
implementation project(":support")
342351

343352
if (project.ext.react.enableHermes) {
344-
// TODO: Remove this block when we drop support for 0.68.
345-
if (reactNativeVersion < 6900) {
353+
if (reactNativeVersion == 0) {
354+
implementation("com.facebook.react:hermes-engine")
355+
} else if (reactNativeVersion >= 6900) {
356+
implementation("com.facebook.react:hermes-engine:+") {
357+
exclude(group: "com.facebook.fbjni")
358+
}
359+
} else {
360+
// TODO: Remove this block when we drop support for 0.68.
346361
def hermesEngineDir =
347362
findNodeModulesPath("hermes-engine", reactNativePath)
348363
?: findNodeModulesPath("hermesvm", reactNativePath)
@@ -353,10 +368,6 @@ dependencies {
353368
def hermesAndroidDir = "${hermesEngineDir}/android"
354369
releaseImplementation files("${hermesAndroidDir}/hermes-release.aar")
355370
debugImplementation files("${hermesAndroidDir}/hermes-debug.aar")
356-
} else {
357-
implementation("com.facebook.react:hermes-engine:+") {
358-
exclude(group: "com.facebook.fbjni")
359-
}
360371
}
361372
}
362373

@@ -370,7 +381,7 @@ dependencies {
370381
implementation libraries.kotlinStdlibJdk8
371382
implementation libraries.kotlinReflect
372383

373-
if (reactNativeVersion < 6800) {
384+
if (reactNativeVersion > 0 && reactNativeVersion < 6800) {
374385
// androidx.appcompat:appcompat:1.4.0+ breaks TextInput. This was fixed
375386
// in react-native 0.68. For more details, see
376387
// https://github.com/facebook/react-native/issues/31572.

android/app/src/main/java/com/microsoft/reacttestapp/react/TestAppReactNativeHost.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ class TestAppReactNativeHost(
162162
.addPackages(packages)
163163
.setUseDeveloperSupport(useDeveloperSupport)
164164
.setInitialLifecycleState(LifecycleState.BEFORE_CREATE)
165-
.setUIImplementationProvider(uiImplementationProvider)
166165
.setRedBoxHandler(redBoxHandler)
167166
.setJSIModulesPackage(jsiModulePackage)
168167
.build()

android/test-app-util.gradle

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,24 @@ ext.getVersionName = {
223223
}
224224

225225
ext.isFabricEnabled = { project ->
226-
return isNewArchitectureEnabled(project) ||
227-
(isPropertySet(project, "USE_FABRIC", "1") &&
228-
getPackageVersionNumber("react-native", project.rootDir) >= 6800)
226+
if (isNewArchitectureEnabled(project)) {
227+
return true
228+
}
229+
230+
if (isPropertySet(project, "USE_FABRIC", "1")) {
231+
def version = getPackageVersionNumber("react-native", project.rootDir)
232+
return version == 0 || version >= 6800
233+
}
234+
235+
return false
229236
}
230237

231238
ext.isNewArchitectureEnabled = { project ->
232-
return isPropertySet(project, "newArchEnabled", "true") &&
233-
getPackageVersionNumber("react-native", project.rootDir) >= 6800
239+
if (isPropertySet(project, "newArchEnabled", "true")) {
240+
def version = getPackageVersionNumber("react-native", project.rootDir)
241+
return version == 0 || version >= 6800
242+
}
243+
return false
234244
}
235245

236246
ext.isPropertySet = { project, propertyName, value ->

example/android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ buildscript {
1111
dependencies {
1212
classpath("com.android.tools.build:gradle:${androidPluginVersion}")
1313

14-
if (isNewArchitectureEnabled(project)) {
14+
def isNightly = getPackageVersionNumber("react-native", rootDir) == 0
15+
if (isNightly || isNewArchitectureEnabled(project)) {
1516
classpath("com.facebook.react:react-native-gradle-plugin")
1617
classpath("de.undercouch:gradle-download-task:5.2.1")
1718
}

0 commit comments

Comments
 (0)