Repository: microsoft/react-native-test-app
Pull request #1193 (2022-11-07 09:50:37)

fix(android): workaround for dynamic version issues
### Description

Implements workarounds introduced in https://github.com/facebook/react-native/issues/35210.

### Platforms affected

- [x] Android
- [ ] iOS
- [ ] macOS
- [ ] Windows

### Test plan

Repeat the steps below for 0.64, 0.70, 0.71:

```
npm run set-react-version 0.64
npm run clean && yarn
cd example/android
./gradlew clean assembleDebug
```

### Screenshots

| 0.64 | 0.70 | 0.71 |
| :-: | :-: | :-: |
| ![Screenshot_1667815303](https://user-images.githubusercontent.com/4123478/200282379-954bf1c6-7a75-458f-a55d-31532fea2927.png) | ![Screenshot_1667815421](https://user-images.githubusercontent.com/4123478/200282705-f731fb9b-d428-4873-b3eb-549863381697.png) | ![Screenshot_1667815186](https://user-images.githubusercontent.com/4123478/200282396-8d3f9dae-e1cf-447d-9614-1ddd892861d9.png) |



Patch:
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 8c9e3f5..718b585 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -13,14 +13,13 @@ plugins {
 def enableNewArchitecture = isNewArchitectureEnabled(project)
 def reactNativeVersion = getPackageVersionNumber("react-native", rootDir)
 def reactNativePath = file(findNodeModulesPath("react-native", rootDir))
-def codegenPath = file(findNodeModulesPath("react-native-codegen", reactNativePath))
 
 if (reactNativeVersion == 0 || enableNewArchitecture) {
     apply(plugin: "com.facebook.react")
 
     if (enableNewArchitecture) {
         react {
-            codegenDir = codegenPath
+            codegenDir = file(findNodeModulesPath("react-native-codegen", reactNativePath))
             reactNativeDir = reactNativePath
         }
     }
@@ -245,7 +244,7 @@ android {
         // `.gradle/caches/modules-2/files-2.1/com.facebook.react/react-native`.
         // For now, we will simply disable this step as we only need to verify
         // that things build.
-        if (reactNativeVersion > 0) {
+        if (reactNativeVersion > 0 && reactNativeVersion < 7100) {
             def version = getPackageVersion("react-native", rootDir)
             def allAar = file("${reactNativePath}/android/com/facebook/react/react-native/${version}/react-native-${version}.aar")
 
@@ -373,8 +372,11 @@ dependencies {
 
     if (project.ext.react.enableNewArchitecture) {
         implementation project(":ReactAndroid")
+    } else if (reactNativeVersion == 0 || reactNativeVersion >= 7100) {
+        implementation("com.facebook.react:react-native")
     } else {
-        implementation "com.facebook.react:react-native:+"
+        def version = getPackageVersion("react-native", rootDir)
+        implementation("com.facebook.react:react-native:${version}")
     }
 
     implementation libraries.kotlinStdlibJdk7
@@ -434,6 +436,16 @@ if (project.ext.react.enableNewArchitecture) {
                 .because("On New Architecture, we are building Hermes from source")
         }
     }
+} else if (reactNativeVersion > 0 && reactNativeVersion < 7100) {
+    configurations.all {
+        resolutionStrategy {
+            // Force version here otherwise Gradle will pick up a newer version:
+            // https://github.com/facebook/react-native/issues/35210
+            def version = getPackageVersion("react-native", rootDir)
+            force("com.facebook.react:react-native:${version}")
+            force("com.facebook.react:hermes-engine:${version}")
+        }
+    }
 }
 
 // `@react-native-community/cli` currently requires this function to be defined.
diff --git a/example/android/build.gradle b/example/android/build.gradle
index 9044f61..984a681 100644
--- a/example/android/build.gradle
+++ b/example/android/build.gradle
@@ -14,7 +14,7 @@ buildscript {
         def isNightly = getPackageVersionNumber("react-native", rootDir) == 0
         if (isNightly || isNewArchitectureEnabled(project)) {
             classpath("com.facebook.react:react-native-gradle-plugin")
-            classpath("de.undercouch:gradle-download-task:5.2.1")
+            classpath("de.undercouch:gradle-download-task:5.3.0")
         }
     }
 }
diff --git a/scripts/configure.js b/scripts/configure.js
index 9d4bed2..31cc2c5 100755
--- a/scripts/configure.js
+++ b/scripts/configure.js
@@ -487,7 +487,7 @@ const getConfig = (() => {
               "",
               "        if (isNewArchitectureEnabled(project)) {",
               '            classpath("com.facebook.react:react-native-gradle-plugin")',
-              '            classpath("de.undercouch:gradle-download-task:5.2.1")',
+              '            classpath("de.undercouch:gradle-download-task:5.3.0")',
               "        }",
               "    }",
               "}",


Test patch:
diff --git a/test/configure/__snapshots__/gatherConfig.test.js.snap b/test/configure/__snapshots__/gatherConfig.test.js.snap
index 4861aeb..7d93a74 100644
--- a/test/configure/__snapshots__/gatherConfig.test.js.snap
+++ b/test/configure/__snapshots__/gatherConfig.test.js.snap
@@ -84,7 +84,7 @@ Object {
 
         if (isNewArchitectureEnabled(project)) {
             classpath(\\"com.facebook.react:react-native-gradle-plugin\\")
-            classpath(\\"de.undercouch:gradle-download-task:5.2.1\\")
+            classpath(\\"de.undercouch:gradle-download-task:5.3.0\\")
         }
     }
 }
@@ -386,7 +386,7 @@ Object {
 
         if (isNewArchitectureEnabled(project)) {
             classpath(\\"com.facebook.react:react-native-gradle-plugin\\")
-            classpath(\\"de.undercouch:gradle-download-task:5.2.1\\")
+            classpath(\\"de.undercouch:gradle-download-task:5.3.0\\")
         }
     }
 }
@@ -537,7 +537,7 @@ Object {
 
         if (isNewArchitectureEnabled(project)) {
             classpath(\\"com.facebook.react:react-native-gradle-plugin\\")
-            classpath(\\"de.undercouch:gradle-download-task:5.2.1\\")
+            classpath(\\"de.undercouch:gradle-download-task:5.3.0\\")
         }
     }
 }