From 18c666e28f9a8e85d6323ef1e1ab5fa0d9d2e371 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Mon, 3 Jul 2023 21:16:35 +0200 Subject: [PATCH 1/2] fix(android): handle `ReactApplication` changes to getters Signature changes were introduced in https://github.com/facebook/react-native/commit/c3f672cef7d4f287d3d729d33650f917ed132a0c. --- android/app/build.gradle | 10 ++- .../reacttestapp/manifest/Manifest.kt | 5 +- .../com/microsoft/reacttestapp/TestApp.kt | 64 +++++++++++++++++++ .../com/microsoft/reacttestapp/TestApp.kt | 18 +++--- android/dependencies.gradle | 18 ++++-- scripts/generate-manifest.mjs | 5 +- 6 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt rename android/app/src/{main => reactapplication-pre-0.73}/java/com/microsoft/reacttestapp/TestApp.kt (100%) diff --git a/android/app/build.gradle b/android/app/build.gradle index bb94321ba..f4b0c52ce 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -319,17 +319,23 @@ android { ? "src/reactinstanceeventlistener-pre-0.68/java" : "src/reactinstanceeventlistener-0.68/java", - // TODO: Remove this block when we drop support for 0.72 + // TODO: Remove this block when we drop support for 0.71 // https://github.com/facebook/react-native/commit/e5dd9cdc6688e63e75a7e0bebf380be1a9a5fe2b reactNativeVersion > 0 && reactNativeVersion < 7200 ? "src/reactactivitydelegate-pre-0.72/java" : "src/reactactivitydelegate-0.72/java", - // TODO: Remove this block when we drop support for 0.73 + // TODO: Remove this block when we drop support for 0.72 // https://github.com/facebook/react-native/commit/da358d0ec7a492edb804b9cdce70e7516ee518ae reactNativeVersion > 0 && reactNativeVersion < 7300 ? "src/devserverhelper-pre-0.73/java" : "src/devserverhelper-0.73/java", + + // TODO: Remove this block when we drop support for 0.72 + // https://github.com/facebook/react-native/commit/c3f672cef7d4f287d3d729d33650f917ed132a0c + reactNativeVersion > 0 && reactNativeVersion < 7300 + ? "src/reactapplication-pre-0.73/java" + : "src/reactapplication-0.73/java", ] if (project.ext.react.enableFlipper) { diff --git a/android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt b/android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt index a1d621c4e..b9731a452 100644 --- a/android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt +++ b/android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt @@ -1,14 +1,13 @@ // This file was generated by generate-manifest.mjs. // DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN. +@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site") + package com.microsoft.reacttestapp.manifest import android.os.Bundle import com.squareup.moshi.JsonClass -/* ktlint-disable trailing-comma */ -/* ktlint-disable trailing-comma-on-declaration-site */ - @JsonClass(generateAdapter = true) data class Component( val appKey: String, diff --git a/android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt b/android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt new file mode 100644 index 000000000..608aebb7b --- /dev/null +++ b/android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt @@ -0,0 +1,64 @@ +package com.microsoft.reacttestapp + +import android.app.Activity +import android.app.Application +import android.content.Context +import com.facebook.react.PackageList +import com.facebook.react.ReactApplication +import com.facebook.soloader.SoLoader +import com.microsoft.reacttestapp.manifest.ManifestProvider +import com.microsoft.reacttestapp.react.ReactBundleNameProvider +import com.microsoft.reacttestapp.react.TestAppReactNativeHost +import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents + +class TestApp : Application(), ReactApplication { + val bundleNameProvider: ReactBundleNameProvider + get() = reactNativeBundleNameProvider + + val manifestProvider: ManifestProvider + get() = manifestProviderInternal + + override val reactNativeHost: TestAppReactNativeHost + get() = reactNativeHostInternal + + private lateinit var manifestProviderInternal: ManifestProvider + private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider + private lateinit var reactNativeHostInternal: TestAppReactNativeHost + + fun reloadJSFromServer(activity: Activity?, bundleURL: String) { + reactNativeHostInternal.reloadJSFromServer(activity, bundleURL) + } + + override fun onCreate() { + super.onCreate() + + // We need to initialize SoLoader early because ManifestProvider may + // need to use WritableNativeMap when parsing initial properties. + SoLoader.init(this, false) + + manifestProviderInternal = ManifestProvider.create(this) + val (manifest, _) = manifestProvider.fromResources() + + reactNativeBundleNameProvider = ReactBundleNameProvider(this, manifest.bundleRoot) + reactNativeHostInternal = + TestAppReactNativeHost(this, reactNativeBundleNameProvider) + + val eventConsumers = PackageList(this).packages + .filter { it is ReactTestAppLifecycleEvents } + .map { it as ReactTestAppLifecycleEvents } + + eventConsumers.forEach { it.onTestAppInitialized() } + + reactNativeHostInternal.init( + beforeReactNativeInit = { + eventConsumers.forEach { it.onTestAppWillInitializeReactNative() } + }, + afterReactNativeInit = { + eventConsumers.forEach { it.onTestAppDidInitializeReactNative() } + } + ) + } +} + +val Context.testApp: TestApp + get() = applicationContext as TestApp diff --git a/android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt b/android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt similarity index 100% rename from android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt rename to android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt index 45736016b..d00913239 100644 --- a/android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt +++ b/android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt @@ -11,10 +11,13 @@ import com.microsoft.reacttestapp.react.ReactBundleNameProvider import com.microsoft.reacttestapp.react.TestAppReactNativeHost import com.microsoft.reacttestapp.support.ReactTestAppLifecycleEvents -val Context.testApp: TestApp - get() = applicationContext as TestApp - class TestApp : Application(), ReactApplication { + val bundleNameProvider: ReactBundleNameProvider + get() = reactNativeBundleNameProvider + + val manifestProvider: ManifestProvider + get() = manifestProviderInternal + private lateinit var manifestProviderInternal: ManifestProvider private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider private lateinit var reactNativeHostInternal: TestAppReactNativeHost @@ -53,11 +56,8 @@ class TestApp : Application(), ReactApplication { ) } - val bundleNameProvider: ReactBundleNameProvider - get() = reactNativeBundleNameProvider - - val manifestProvider: ManifestProvider - get() = manifestProviderInternal - override fun getReactNativeHost() = reactNativeHostInternal } + +val Context.testApp: TestApp + get() = applicationContext as TestApp diff --git a/android/dependencies.gradle b/android/dependencies.gradle index 727f152bc..7109fcba4 100644 --- a/android/dependencies.gradle +++ b/android/dependencies.gradle @@ -1,5 +1,5 @@ /** - * Get the recommended Gradle plugin version for the specified React Native + * Returns the recommended Gradle plugin version for the specified React Native * version. */ static String getDefaultGradlePluginVersion(int reactNativeVersion) { @@ -15,7 +15,8 @@ static String getDefaultGradlePluginVersion(int reactNativeVersion) { } /** - * Get the recommended Kotlin version for the specified React Native version. + * Returns the recommended Kotlin version for the specified React Native + * version. */ static String getDefaultKotlinVersion(int reactNativeVersion) { // Kotlin version can be found in the template: @@ -27,11 +28,6 @@ static String getDefaultKotlinVersion(int reactNativeVersion) { } } -static int toVersionNumber(String version) { - def (major, minor, patch) = version.findAll(/\d+/) - return (major as int) * 10000 + (minor as int) * 100 + (patch as int) -} - /** * Find the latest version of KSP that supports the specified Kotlin version. */ @@ -59,6 +55,14 @@ static String getKspVersion(String kotlinVersion) { ].find { it.startsWith(kotlinVersion) } } +/** + * Converts a version string into a number. + */ +static int toVersionNumber(String version) { + def (major, minor, patch) = version.findAll(/\d+/) + return (major as int) * 10000 + (minor as int) * 100 + (patch as int) +} + ext { apply(from: "${buildscript.sourceFile.getParent()}/test-app-util.gradle") diff --git a/scripts/generate-manifest.mjs b/scripts/generate-manifest.mjs index 51fdb3230..fca88ff47 100755 --- a/scripts/generate-manifest.mjs +++ b/scripts/generate-manifest.mjs @@ -98,14 +98,13 @@ function getLanguage(output) { indent: " ", level: 0, header: [ + '@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site")', + "", "package com.microsoft.reacttestapp.manifest", "", "import android.os.Bundle", "import com.squareup.moshi.JsonClass", "", - "/* ktlint-disable trailing-comma */", - "/* ktlint-disable trailing-comma-on-declaration-site */", - "", ].join("\n"), }, arrayProperty: (name, type, required) => { From b347a4d15e0b1cfcd739966a381503f88660d9cd Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 4 Jul 2023 13:35:27 +0200 Subject: [PATCH 2/2] update test --- test/pack.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/pack.test.js b/test/pack.test.js index 5c0bd90d1..f596750fc 100644 --- a/test/pack.test.js +++ b/test/pack.test.js @@ -38,7 +38,6 @@ describe("npm pack", () => { "android/app/src/main/AndroidManifest.xml", "android/app/src/main/java/com/microsoft/reacttestapp/MainActivity.kt", "android/app/src/main/java/com/microsoft/reacttestapp/Session.kt", - "android/app/src/main/java/com/microsoft/reacttestapp/TestApp.kt", "android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentActivity.kt", "android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentBottomSheetDialogFragment.kt", "android/app/src/main/java/com/microsoft/reacttestapp/component/ComponentListAdapter.kt", @@ -86,6 +85,8 @@ describe("npm pack", () => { "android/app/src/no-turbomodule/java/com/microsoft/reacttestapp/fabric/ComponentsRegistry.kt", "android/app/src/reactactivitydelegate-0.72/java/com/microsoft/reacttestapp/component/ComponentActivityDelegate.kt", "android/app/src/reactactivitydelegate-pre-0.72/java/com/microsoft/reacttestapp/component/ComponentActivityDelegate.kt", + "android/app/src/reactapplication-0.73/java/com/microsoft/reacttestapp/TestApp.kt", + "android/app/src/reactapplication-pre-0.73/java/com/microsoft/reacttestapp/TestApp.kt", "android/app/src/reactinstanceeventlistener-0.68/java/com/microsoft/reacttestapp/compat/ReactInstanceEventListener.kt", "android/app/src/reactinstanceeventlistener-pre-0.68/java/com/microsoft/reacttestapp/compat/ReactInstanceEventListener.kt", "android/app/src/turbomodule/java/com/microsoft/reacttestapp/compat/ReactNativeHostCompat.kt",