diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 79fc6198a3301a..c0294c6d8e4287 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -68,10 +68,11 @@ public class com/facebook/react/MemoryPressureRouter : android/content/Component public fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V } -public class com/facebook/react/NativeModuleRegistryBuilder { +public final class com/facebook/react/NativeModuleRegistryBuilder { + public fun (Lcom/facebook/react/bridge/ReactApplicationContext;)V public fun (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/ReactInstanceManager;)V - public fun build ()Lcom/facebook/react/bridge/NativeModuleRegistry; - public fun processPackage (Lcom/facebook/react/ReactPackage;)V + public final fun build ()Lcom/facebook/react/bridge/NativeModuleRegistry; + public final fun processPackage (Lcom/facebook/react/ReactPackage;)V } public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/AppCompatActivity, com/facebook/react/modules/core/DefaultHardwareBackBtnHandler, com/facebook/react/modules/core/PermissionAwareActivity { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java deleted file mode 100644 index a33bc6e732b563..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react; - -import com.facebook.react.bridge.ModuleHolder; -import com.facebook.react.bridge.NativeModuleRegistry; -import com.facebook.react.bridge.ReactApplicationContext; -import java.util.HashMap; -import java.util.Map; - -/** Helper class to build NativeModuleRegistry. */ -public class NativeModuleRegistryBuilder { - - private final ReactApplicationContext mReactApplicationContext; - - private final Map mModules = new HashMap<>(); - - public NativeModuleRegistryBuilder( - ReactApplicationContext reactApplicationContext, ReactInstanceManager reactInstanceManager) { - mReactApplicationContext = reactApplicationContext; - } - - public void processPackage(ReactPackage reactPackage) { - // We use an iterable instead of an iterator here to ensure thread safety, and that this list - // cannot be modified - Iterable moduleHolders; - if (reactPackage instanceof LazyReactPackage) { - moduleHolders = - ((LazyReactPackage) reactPackage).getNativeModuleIterator(mReactApplicationContext); - } else if (reactPackage instanceof BaseReactPackage) { - moduleHolders = - ((BaseReactPackage) reactPackage).getNativeModuleIterator(mReactApplicationContext); - } else { - moduleHolders = - ReactPackageHelper.getNativeModuleIterator(reactPackage, mReactApplicationContext); - } - - for (ModuleHolder moduleHolder : moduleHolders) { - String name = moduleHolder.getName(); - if (mModules.containsKey(name)) { - ModuleHolder existingNativeModule = mModules.get(name); - if (!moduleHolder.getCanOverrideExistingModule()) { - throw new IllegalStateException( - "Native module " - + name - + " tried to override " - + existingNativeModule.getClassName() - + ". Check the getPackages() method in MainApplication.java, it might be that" - + " module is being created twice. If this was your intention, set" - + " canOverrideExistingModule=true. This error may also be present if the package" - + " is present only once in getPackages() but is also automatically added later" - + " during build time by autolinking. Try removing the existing entry and" - + " rebuild."); - } - mModules.remove(existingNativeModule); - } - mModules.put(name, moduleHolder); - } - } - - public NativeModuleRegistry build() { - return new NativeModuleRegistry(mReactApplicationContext, mModules); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.kt new file mode 100644 index 00000000000000..b50070c3d175a8 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.kt @@ -0,0 +1,61 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react + +import com.facebook.react.bridge.ModuleHolder +import com.facebook.react.bridge.NativeModuleRegistry +import com.facebook.react.bridge.ReactApplicationContext + +/** Helper class to build NativeModuleRegistry. */ +public class NativeModuleRegistryBuilder( + private val reactApplicationContext: ReactApplicationContext, +) { + + private val modules = HashMap() + + @Deprecated( + "ReactInstanceManager is not used", + ReplaceWith("NativeModuleRegistryBuilder(reactApplicationContext)")) + public constructor( + reactApplicationContext: ReactApplicationContext, + @Suppress("UNUSED_PARAMETER") reactInstanceManager: ReactInstanceManager + ) : this(reactApplicationContext) {} + + public fun processPackage(reactPackage: ReactPackage) { + // We use an iterable instead of an iterator here to ensure thread safety, and that this list + // cannot be modified + val moduleHolders = + @Suppress("DEPRECATION") + if (reactPackage is LazyReactPackage) { + reactPackage.getNativeModuleIterator(reactApplicationContext) + } else if (reactPackage is BaseReactPackage) { + reactPackage.getNativeModuleIterator(reactApplicationContext) + } else { + ReactPackageHelper.getNativeModuleIterator(reactPackage, reactApplicationContext) + } + for (moduleHolder in moduleHolders) { + val name = moduleHolder.name + val existingNativeModule = modules[name] + if (existingNativeModule != null) { + check(moduleHolder.canOverrideExistingModule) { + """ +Native module $name tried to override ${existingNativeModule.className}. + +Check the getPackages() method in MainApplication.java, it might be that module is being created twice. +If this was your intention, set canOverrideExistingModule=true. This error may also be present if the +package is present only once in getPackages() but is also automatically added later during build time +by autolinking. Try removing the existing entry and rebuild. +""" + } + } + modules[name] = moduleHolder + } + } + + public fun build(): NativeModuleRegistry = NativeModuleRegistry(reactApplicationContext, modules) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index dc07b2bfaaf093..1ba517b3075851 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -1536,7 +1536,7 @@ private ReactApplicationContext createReactContext( private NativeModuleRegistry processPackages( ReactApplicationContext reactContext, List packages) { NativeModuleRegistryBuilder nativeModuleRegistryBuilder = - new NativeModuleRegistryBuilder(reactContext, this); + new NativeModuleRegistryBuilder(reactContext); ReactMarker.logMarker(PROCESS_PACKAGES_START);