Skip to content

Commit fafd197

Browse files
authored
fix(android): handle ReactApplication changes to getters (#1483)
Signature changes were introduced in facebook/react-native@c3f672c.
1 parent 984af14 commit fafd197

File tree

7 files changed

+98
-25
lines changed

7 files changed

+98
-25
lines changed

android/app/build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,17 +319,23 @@ android {
319319
? "src/reactinstanceeventlistener-pre-0.68/java"
320320
: "src/reactinstanceeventlistener-0.68/java",
321321

322-
// TODO: Remove this block when we drop support for 0.72
322+
// TODO: Remove this block when we drop support for 0.71
323323
// https://github.com/facebook/react-native/commit/e5dd9cdc6688e63e75a7e0bebf380be1a9a5fe2b
324324
reactNativeVersion > 0 && reactNativeVersion < 7200
325325
? "src/reactactivitydelegate-pre-0.72/java"
326326
: "src/reactactivitydelegate-0.72/java",
327327

328-
// TODO: Remove this block when we drop support for 0.73
328+
// TODO: Remove this block when we drop support for 0.72
329329
// https://github.com/facebook/react-native/commit/da358d0ec7a492edb804b9cdce70e7516ee518ae
330330
reactNativeVersion > 0 && reactNativeVersion < 7300
331331
? "src/devserverhelper-pre-0.73/java"
332332
: "src/devserverhelper-0.73/java",
333+
334+
// TODO: Remove this block when we drop support for 0.72
335+
// https://github.com/facebook/react-native/commit/c3f672cef7d4f287d3d729d33650f917ed132a0c
336+
reactNativeVersion > 0 && reactNativeVersion < 7300
337+
? "src/reactapplication-pre-0.73/java"
338+
: "src/reactapplication-0.73/java",
333339
]
334340

335341
if (project.ext.react.enableFlipper) {

android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// This file was generated by generate-manifest.mjs.
22
// DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN.
33

4+
@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site")
5+
46
package com.microsoft.reacttestapp.manifest
57

68
import android.os.Bundle
79
import com.squareup.moshi.JsonClass
810

9-
/* ktlint-disable trailing-comma */
10-
/* ktlint-disable trailing-comma-on-declaration-site */
11-
1211
@JsonClass(generateAdapter = true)
1312
data class Component(
1413
val appKey: String,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.microsoft.reacttestapp
2+
3+
import android.app.Activity
4+
import android.app.Application
5+
import android.content.Context
6+
import com.facebook.react.PackageList
7+
import com.facebook.react.ReactApplication
8+
import com.facebook.soloader.SoLoader
9+
import com.microsoft.reacttestapp.manifest.ManifestProvider
10+
import com.microsoft.reacttestapp.react.ReactBundleNameProvider
11+
import com.microsoft.reacttestapp.react.TestAppReactNativeHost
12+
import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents
13+
14+
class TestApp : Application(), ReactApplication {
15+
val bundleNameProvider: ReactBundleNameProvider
16+
get() = reactNativeBundleNameProvider
17+
18+
val manifestProvider: ManifestProvider
19+
get() = manifestProviderInternal
20+
21+
override val reactNativeHost: TestAppReactNativeHost
22+
get() = reactNativeHostInternal
23+
24+
private lateinit var manifestProviderInternal: ManifestProvider
25+
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
26+
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
27+
28+
fun reloadJSFromServer(activity: Activity?, bundleURL: String) {
29+
reactNativeHostInternal.reloadJSFromServer(activity, bundleURL)
30+
}
31+
32+
override fun onCreate() {
33+
super.onCreate()
34+
35+
// We need to initialize SoLoader early because ManifestProvider may
36+
// need to use WritableNativeMap when parsing initial properties.
37+
SoLoader.init(this, false)
38+
39+
manifestProviderInternal = ManifestProvider.create(this)
40+
val (manifest, _) = manifestProvider.fromResources()
41+
42+
reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot)
43+
reactNativeHostInternal =
44+
TestAppReactNativeHost(this, reactNativeBundleNameProvider)
45+
46+
val eventConsumers = PackageList(this).packages
47+
.filter { it is ReactTestAppLifecycleEvents }
48+
.map { it as ReactTestAppLifecycleEvents }
49+
50+
eventConsumers.forEach { it.onTestAppInitialized() }
51+
52+
reactNativeHostInternal.init(
53+
beforeReactNativeInit = {
54+
eventConsumers.forEach { it.onTestAppWillInitializeReactNative() }
55+
},
56+
afterReactNativeInit = {
57+
eventConsumers.forEach { it.onTestAppDidInitializeReactNative() }
58+
}
59+
)
60+
}
61+
}
62+
63+
val Context.testApp: TestApp
64+
get() = applicationContext as TestApp

android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt renamed to android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import com.microsoft.reacttestapp.react.ReactBundleNameProvider
1111
import com.microsoft.reacttestapp.react.TestAppReactNativeHost
1212
import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents
1313

14-
val Context.testApp: TestApp
15-
get() = applicationContext as TestApp
16-
1714
class TestApp : Application(), ReactApplication {
15+
val bundleNameProvider: ReactBundleNameProvider
16+
get() = reactNativeBundleNameProvider
17+
18+
val manifestProvider: ManifestProvider
19+
get() = manifestProviderInternal
20+
1821
private lateinit var manifestProviderInternal: ManifestProvider
1922
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
2023
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
@@ -53,11 +56,8 @@ class TestApp : Application(), ReactApplication {
5356
)
5457
}
5558

56-
val bundleNameProvider: ReactBundleNameProvider
57-
get() = reactNativeBundleNameProvider
58-
59-
val manifestProvider: ManifestProvider
60-
get() = manifestProviderInternal
61-
6259
override fun getReactNativeHost() = reactNativeHostInternal
6360
}
61+
62+
val Context.testApp: TestApp
63+
get() = applicationContext as TestApp

android/dependencies.gradle

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Get the recommended Gradle plugin version for the specified React Native
2+
* Returns the recommended Gradle plugin version for the specified React Native
33
* version.
44
*/
55
static String getDefaultGradlePluginVersion(int reactNativeVersion) {
@@ -15,7 +15,8 @@ static String getDefaultGradlePluginVersion(int reactNativeVersion) {
1515
}
1616

1717
/**
18-
* Get the recommended Kotlin version for the specified React Native version.
18+
* Returns the recommended Kotlin version for the specified React Native
19+
* version.
1920
*/
2021
static String getDefaultKotlinVersion(int reactNativeVersion) {
2122
// Kotlin version can be found in the template:
@@ -27,11 +28,6 @@ static String getDefaultKotlinVersion(int reactNativeVersion) {
2728
}
2829
}
2930

30-
static int toVersionNumber(String version) {
31-
def (major, minor, patch) = version.findAll(/\d+/)
32-
return (major as int) * 10000 + (minor as int) * 100 + (patch as int)
33-
}
34-
3531
/**
3632
* Find the latest version of KSP that supports the specified Kotlin version.
3733
*/
@@ -59,6 +55,14 @@ static String getKspVersion(String kotlinVersion) {
5955
].find { it.startsWith(kotlinVersion) }
6056
}
6157

58+
/**
59+
* Converts a version string into a number.
60+
*/
61+
static int toVersionNumber(String version) {
62+
def (major, minor, patch) = version.findAll(/\d+/)
63+
return (major as int) * 10000 + (minor as int) * 100 + (patch as int)
64+
}
65+
6266
ext {
6367
apply(from: "${buildscript.sourceFile.getParent()}/test-app-util.gradle")
6468

scripts/generate-manifest.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,13 @@ function getLanguage(output) {
9898
indent: " ",
9999
level: 0,
100100
header: [
101+
'@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site")',
102+
"",
101103
"package com.microsoft.reacttestapp.manifest",
102104
"",
103105
"import android.os.Bundle",
104106
"import com.squareup.moshi.JsonClass",
105107
"",
106-
"/* ktlint-disable trailing-comma */",
107-
"/* ktlint-disable trailing-comma-on-declaration-site */",
108-
"",
109108
].join("\n"),
110109
},
111110
arrayProperty: (name, type, required) => {

test/pack.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ describe("npm pack", () => {
3838
"android/app/src/main/AndroidManifest.xml",
3939
"android/app/src/main/java/com/microsoft/reacttestapp/MainActivity.kt",
4040
"android/app/src/main/java/com/microsoft/reacttestapp/Session.kt",
41-
"android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt",
4241
"android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentActivity.kt",
4342
"android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentBottomSheetDialogFragment.kt",
4443
"android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentListAdapter.kt",
@@ -86,6 +85,8 @@ describe("npm pack", () => {
8685
"android/app/src/no-turbomodule/java/com/microsoft/reacttestapp/fabric/ComponentsRegistry.kt",
8786
"android/app/src/reactactivitydelegate-0.72/java/com/microsoft/reacttestapp/component/ComponentActivityDelegate.kt",
8887
"android/app/src/reactactivitydelegate-pre-0.72/java/com/microsoft/reacttestapp/component/ComponentActivityDelegate.kt",
88+
"android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt",
89+
"android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt",
8990
"android/app/src/reactinstanceeventlistener-0.68/java/com/microsoft/reacttestapp/compat/ReactInstanceEventListener.kt",
9091
"android/app/src/reactinstanceeventlistener-pre-0.68/java/com/microsoft/reacttestapp/compat/ReactInstanceEventListener.kt",
9192
"android/app/src/turbomodule/java/com/microsoft/reacttestapp/compat/ReactNativeHostCompat.kt",

0 commit comments

Comments
 (0)