From 7f43f7eb8f0433c6aa446715ffcec59f432fc404 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:51:18 +0200 Subject: [PATCH] fix(android): initialize SoLoader with merged libraries on 0.76 --- android/app/build.gradle | 14 ++-- .../com/microsoft/reacttestapp/TestApp.kt | 69 +++++++++++++++++++ test/pack.test.ts | 1 + 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 android/app/src/reactapplication-0.76/java/com/microsoft/reacttestapp/TestApp.kt diff --git a/android/app/build.gradle b/android/app/build.gradle index 5c7c775b2..3c2638624 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -249,11 +249,15 @@ android { ? "src/devserverhelper-0.73/java" : "src/devserverhelper-pre-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 < v(0, 73, 0) - ? "src/reactapplication-pre-0.73/java" - : "src/reactapplication-0.73/java", + // TODO: Remove this block when we drop support for 0.75 + // https://github.com/react-native-community/template/commit/f738a366b194dd21d4d2bc14c9215b630714dd70 + reactNativeVersion >= v(0, 76, 0) + ? "src/reactapplication-0.76/java" + // TODO: Remove this block when we drop support for 0.72 + // https://github.com/facebook/react-native/commit/c3f672cef7d4f287d3d729d33650f917ed132a0c + : reactNativeVersion < v(0, 73, 0) + ? "src/reactapplication-pre-0.73/java" + : "src/reactapplication-0.73/java", ] } diff --git a/android/app/src/reactapplication-0.76/java/com/microsoft/reacttestapp/TestApp.kt b/android/app/src/reactapplication-0.76/java/com/microsoft/reacttestapp/TestApp.kt new file mode 100644 index 000000000..9b4e95e4b --- /dev/null +++ b/android/app/src/reactapplication-0.76/java/com/microsoft/reacttestapp/TestApp.kt @@ -0,0 +1,69 @@ +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.react.ReactHost +import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost +import com.facebook.react.soloader.OpenSourceMergedSoMapping +import com.facebook.soloader.SoLoader +import com.microsoft.reacttestapp.manifest.Manifest +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 manifest: Manifest by lazy { + ManifestProvider.manifest() + } + + override val reactHost: ReactHost + get() = getDefaultReactHost(this.applicationContext, reactNativeHost) + + override val reactNativeHost: TestAppReactNativeHost + get() = reactNativeHostInternal + + 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() + + SoLoader.init(this, OpenSourceMergedSoMapping) + + 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/test/pack.test.ts b/test/pack.test.ts index 794031ca6..5cd4cb358 100644 --- a/test/pack.test.ts +++ b/test/pack.test.ts @@ -95,6 +95,7 @@ describe("npm pack", () => { "android/app/src/reactactivitydelegate-0.75/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-0.76/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",