Skip to content

Commit

Permalink
fix(android): handle ReactApplication changes to getters (#1483)
Browse files Browse the repository at this point in the history
Signature changes were introduced in
facebook/react-native@c3f672c.
  • Loading branch information
tido64 authored Jul 4, 2023
1 parent 984af14 commit fafd197
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 25 deletions.
10 changes: 8 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
18 changes: 11 additions & 7 deletions android/dependencies.gradle
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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:
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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")

Expand Down
5 changes: 2 additions & 3 deletions scripts/generate-manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion test/pack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fafd197

Please sign in to comment.