Skip to content

Commit

Permalink
fix(android): use default app setup for New Arch (#1774)
Browse files Browse the repository at this point in the history
Fixes a crash on startup and reduces our footprint.
  • Loading branch information
tido64 authored Jan 19, 2024
1 parent 52efdcb commit 558a6e1
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 196 deletions.
8 changes: 1 addition & 7 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,7 @@ android {
main.java.srcDirs += [
project.ext.react.enableCamera ? "src/camera/java" : "src/no-camera/java",

!enableNewArchitecture
? "src/old-arch/java"
// TODO: Remove this block when we drop support for 0.73
// https://github.com/facebook/react-native/commit/65552055392a5996bf50548018953875dde4560b
: reactNativeVersion >= v(0, 74, 0)
? "src/new-arch-0.74/java"
: "src/new-arch/java",
enableNewArchitecture ? "src/new-arch/java" : "src/old-arch/java",

// TODO: Remove this block when we drop support for 0.67
// https://github.com/facebook/react-native/commit/ce74aa4ed335d4c36ce722d47937b582045e05c4
Expand Down
11 changes: 8 additions & 3 deletions android/app/src/main/jni/ComponentsRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <CoreComponentsRegistry.h>
#endif

#include <DefaultComponentsRegistry.h>

#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/componentregistry/ComponentDescriptorRegistry.h>
#include <react/renderer/components/rncore/ComponentDescriptors.h>
Expand All @@ -25,6 +27,12 @@ using ReactTestApp::ComponentsRegistry;
void ComponentsRegistry::registerNatives()
{
registerHybrid({makeNativeMethod("initHybrid", ComponentsRegistry::initHybrid)});

facebook::react::DefaultComponentsRegistry::registerComponentDescriptorsFromEntryPoint =
[](std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
// Register providers generated by `@react-native-community/cli`
rncli_registerProviders(registry);
};
}

ComponentsRegistry::ComponentsRegistry(ComponentFactory *)
Expand All @@ -39,9 +47,6 @@ ComponentsRegistry::initHybrid(facebook::jni::alias_ref<jclass>, ComponentFactor
-> ComponentDescriptorRegistry::Shared {
auto providerRegistry = CoreComponentsRegistry::sharedProviderRegistry();

// Register providers generated by `@react-native-community/cli`
rncli_registerProviders(providerRegistry);

auto registry = providerRegistry->createComponentDescriptorRegistry(
{eventDispatcher, contextContainer});

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,68 +1,31 @@
package com.microsoft.reacttestapp.compat

import android.app.Application
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactNativeHost
import com.facebook.react.bridge.JSIModulePackage
import com.facebook.react.bridge.JSIModuleProvider
import com.facebook.react.bridge.JSIModuleSpec
import com.facebook.react.bridge.JSIModuleType
import com.facebook.react.bridge.JavaScriptContextHolder
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.UIManager
import com.facebook.react.fabric.ComponentFactory
import com.facebook.react.fabric.CoreComponentsRegistry
import com.facebook.react.fabric.EmptyReactNativeConfig
import com.facebook.react.fabric.FabricJSIModuleProvider
import com.facebook.react.uimanager.ViewManagerRegistry
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.microsoft.reacttestapp.BuildConfig
import com.microsoft.reacttestapp.fabric.ComponentsRegistry
import com.microsoft.reacttestapp.turbomodule.TurboModuleManagerDelegate
import java.lang.ref.WeakReference

abstract class ReactNativeHostCompat(application: Application) : ReactNativeHost(application) {
override fun getReactPackageTurboModuleManagerDelegateBuilder() =
TurboModuleManagerDelegate.Builder()

override fun getJSIModulePackage(): JSIModulePackage? {
return if (BuildConfig.ReactTestApp_useFabric) {
object : JSIModulePackage {

private val reactInstanceManager: ReactInstanceManager?
get() = reactNativeHost.get()?.reactInstanceManager

private val reactNativeHost: WeakReference<ReactNativeHost> =
WeakReference(this@ReactNativeHostCompat)

override fun getJSIModules(
reactApplicationContext: ReactApplicationContext,
jsContext: JavaScriptContextHolder?
): ArrayList<JSIModuleSpec<*>> {
return arrayListOf(object : JSIModuleSpec<UIManager?> {
override fun getJSIModuleType(): JSIModuleType = JSIModuleType.UIManager

override fun getJSIModuleProvider(): JSIModuleProvider<UIManager?> {
val componentFactory = ComponentFactory()
CoreComponentsRegistry.register(componentFactory)

ComponentsRegistry.register(componentFactory)

return FabricJSIModuleProvider(
reactApplicationContext,
componentFactory,
EmptyReactNativeConfig(),
ViewManagerRegistry(
reactInstanceManager?.getOrCreateViewManagers(
reactApplicationContext
)
)
)
}
})
}
abstract class ReactNativeHostCompat(application: Application) : DefaultReactNativeHost(
application
) {
companion object {
init {
try {
DefaultNewArchitectureEntryPoint.load(
/* turboModulesEnabled */
BuildConfig.ReactTestApp_useFabric,
/* fabricEnabled */
BuildConfig.ReactTestApp_useFabric
)
} catch (e: UnsatisfiedLinkError) {
// Older versions of `DefaultNewArchitectureEntryPoint` is
// hard coded to load `libappmodules.so`
}
} else {
null
SoLoader.loadLibrary("reacttestapp_appmodules")
}
}

override val isNewArchEnabled: Boolean = BuildConfig.ReactTestApp_useFabric
override val isHermesEnabled: Boolean? = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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.soloader.SoLoader
import com.microsoft.reacttestapp.manifest.ManifestProvider
import com.microsoft.reacttestapp.react.ReactBundleNameProvider
Expand All @@ -21,6 +23,9 @@ class TestApp : Application(), ReactApplication {
override val reactNativeHost: TestAppReactNativeHost
get() = reactNativeHostInternal

override val reactHost: ReactHost
get() = getDefaultReactHost(this.applicationContext, reactNativeHost)

private lateinit var manifestProviderInternal: ManifestProvider
private lateinit var reactNativeBundleNameProvider: ReactBundleNameProvider
private lateinit var reactNativeHostInternal: TestAppReactNativeHost
Expand Down
6 changes: 3 additions & 3 deletions android/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ ext {
// We need only set `ndkVersion` when building react-native from source.
if (rootProject.hasProperty("ANDROID_NDK_VERSION")) {
ndkVersion = rootProject.properties["ANDROID_NDK_VERSION"]
} else if (reactNativeVersion >= v(0, 74, 0)) {
// https://github.com/facebook/react-native/commit/9ce7b564131c5b2075489c09ff05325ddc28014a
ndkVersion = "26.1.10909125"
} else if (System.properties["os.arch"] == "aarch64" && androidPluginVersion == "7.2.2") {
// NDK r23c has been patched to support Apple M1 and is default in AGP
// 7.3.0. Prior to 0.71, we still need to set `ndkVersion` because we'll
Expand All @@ -91,9 +94,6 @@ ext {
// default. See also
// https://developer.android.com/studio/releases/gradle-plugin#compatibility-7-3-0
ndkVersion = "23.1.7779620"
} else if (reactNativeVersion >= v(0, 74, 0)) {
// https://github.com/facebook/react-native/commit/9ce7b564131c5b2075489c09ff05325ddc28014a
ndkVersion = "26.1.10909125"
}

def kotlinVersionNumber = toVersionNumber(kotlinVersion)
Expand Down
4 changes: 2 additions & 2 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
#newArchEnabled=true

# Uncomment the line below if building react-native from source
#ANDROID_NDK_VERSION=23.1.7779620
#ANDROID_NDK_VERSION=26.1.10909125

# Version of Kotlin to build against.
#KOTLIN_VERSION=1.7.22
#KOTLIN_VERSION=1.8.22
3 changes: 0 additions & 3 deletions test/pack.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ describe("npm pack", () => {
"android/app/src/main/res/values/colors.xml",
"android/app/src/main/res/values/strings.xml",
"android/app/src/main/res/values/styles.xml",
"android/app/src/new-arch-0.74/java/com/microsoft/reacttestapp/compat/ReactNativeHostCompat.kt",
"android/app/src/new-arch-0.74/java/com/microsoft/reacttestapp/fabric/ComponentsRegistry.kt",
"android/app/src/new-arch-0.74/java/com/microsoft/reacttestapp/turbomodule/TurboModuleManagerDelegate.kt",
"android/app/src/new-arch/java/com/microsoft/reacttestapp/compat/ReactNativeHostCompat.kt",
"android/app/src/new-arch/java/com/microsoft/reacttestapp/fabric/ComponentsRegistry.kt",
"android/app/src/new-arch/java/com/microsoft/reacttestapp/turbomodule/TurboModuleManagerDelegate.kt",
Expand Down

0 comments on commit 558a6e1

Please sign in to comment.