From 325fa5e21497e4afbfd32d7eede1a068624eed16 Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Tue, 26 Mar 2024 11:03:17 -0700 Subject: [PATCH] fix[android]: fix bridgeless configuration to include DebuggingOverlay in react packages Summary: # Changelog: [Internal] 1. Move `BridgelessDebugReactPackage.java` to core, this was added in D43407534. 2. `ReactInstanceJava` to add `BridgelessDebugReactPackage`, so `DebuggingOverlay` view manager will be included in the bridgeless build. 3. Fix `RNTesterApplication.kt` to NOT create `MyLegacyViewManager` for every possible viewManagerName, apart from `"RNTMyNativeView"`, return null instead. Differential Revision: D55375350 --- .../runtime/BridgelessDebugReactPackage.java | 81 +++++++++++++++++++ .../facebook/react/runtime/ReactInstance.java | 3 + .../react/uiapp/RNTesterApplication.kt | 6 +- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDebugReactPackage.java diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDebugReactPackage.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDebugReactPackage.java new file mode 100644 index 00000000000000..fa966ed6575e84 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessDebugReactPackage.java @@ -0,0 +1,81 @@ +/* + * 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.runtime; + +import androidx.annotation.Nullable; +import com.facebook.infer.annotation.Nullsafe; +import com.facebook.react.TurboReactPackage; +import com.facebook.react.ViewManagerOnDemandReactPackage; +import com.facebook.react.bridge.ModuleSpec; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.module.annotations.ReactModuleList; +import com.facebook.react.module.model.ReactModuleInfoProvider; +import com.facebook.react.uimanager.UIManagerModule; +import com.facebook.react.uimanager.ViewManager; +import com.facebook.react.views.debuggingoverlay.DebuggingOverlayManager; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.inject.Provider; + +@Nullsafe(Nullsafe.Mode.LOCAL) +@ReactModuleList(nativeModules = {}) +public class BridgelessDebugReactPackage extends TurboReactPackage + implements ViewManagerOnDemandReactPackage { + private @Nullable Map mViewManagers; + + public BridgelessDebugReactPackage() {} + + @Override + public @Nullable NativeModule getModule(String name, ReactApplicationContext reactContext) { + return null; + } + + @Override + public ReactModuleInfoProvider getReactModuleInfoProvider() { + return new BridgelessDebugReactPackage$$ReactModuleInfoProvider(); + } + + private static void appendMap( + Map map, String name, Provider provider) { + map.put(name, ModuleSpec.viewManagerSpec(provider)); + } + + /** + * @return a map of view managers that should be registered with {@link UIManagerModule} + */ + private Map getViewManagersMap() { + if (mViewManagers == null) { + Map viewManagers = new HashMap<>(); + appendMap(viewManagers, DebuggingOverlayManager.REACT_CLASS, DebuggingOverlayManager::new); + + mViewManagers = viewManagers; + } + return mViewManagers; + } + + @Override + public List getViewManagers(ReactApplicationContext reactContext) { + return new ArrayList<>(getViewManagersMap().values()); + } + + @Override + public Collection getViewManagerNames(ReactApplicationContext reactContext) { + return getViewManagersMap().keySet(); + } + + @Override + public @Nullable ViewManager createViewManager( + ReactApplicationContext reactContext, String viewManagerName) { + ModuleSpec spec = getViewManagersMap().get(viewManagerName); + return spec != null ? (ViewManager) spec.getProvider().get() : null; + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java index 728f94efbb6221..a2344690af308c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java @@ -176,6 +176,9 @@ final class ReactInstance { new CoreReactPackage( bridgelessReactContext.getDevSupportManager(), bridgelessReactContext.getDefaultHardwareBackBtnHandler())); + if (useDevSupport) { + mReactPackages.add(new BridgelessDebugReactPackage()); + } mReactPackages.addAll(mDelegate.getReactPackages()); TurboModuleManagerDelegate turboModuleManagerDelegate = diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt index 01756aea982108..8d863a242ca2cf 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.kt @@ -111,11 +111,13 @@ class RNTesterApplication : Application(), ReactApplication { override fun createViewManager( reactContext: ReactApplicationContext, viewManagerName: String - ): ViewManager<*, out ReactShadowNode<*>> = + ): ViewManager<*, out ReactShadowNode<*>>? = if (viewManagerName == "RNTMyNativeView") { MyNativeViewManager() - } else { + } else if (viewManagerName == "RNTMyLegacyNativeView") { MyLegacyViewManager(reactContext) + } else { + null } }) }