From 15a5638c621cbec4a9fcb5ae94938120cdc32fae Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Date: Thu, 21 Mar 2024 18:22:21 -0700 Subject: [PATCH] Implement multiple view manager lookup for the interop layer on Android (#43595) Summary: When running with the new architurece and using the renderer interop layer on Android, view managers don't work if their names start with `RCT`. This happens because the `RCT` prefix is automatically removed from the component name, and inside the internal `mViewManagers` we store view managers with the `RCT` prefix. Using the `RCT` pattern as a prefix works fine with the old architecture and is actually used on the [Android Native UI Components](https://reactnative.dev/docs/next/native-components-android) tutorial in the docs, making me believe that this same patterns is used across many community libraries. This diff adds a secondary lookup logic for view managers: 1. We look for the XXXViewManager. 2. If not found, we look for RCTXXXViewManager. Quite similar to the iOS implementation introduced in https://github.com/facebook/react-native/pull/38093 --- With this change we can also remove most of the entries from FabricNameComponentMapping (I can address this in a follow up PR) https://github.com/facebook/react-native/blob/4e6eba7a2dedaa855af0bff5df3bec73a95f0fc4/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/mountitems/FabricNameComponentMapping.java#L22-L45 ## Changelog: [ANDROID] [ADDED] - Implement multiple view manager lookup for the interop layer Pull Request resolved: https://github.com/facebook/react-native/pull/43595 Test Plan: Tested installing a library such as [react-native-fbsdk-next](https://github.com/thebergamo/react-native-fbsdk-next) that names its view managers starting with `RCT`
Before After
image image
Reviewed By: cortinico Differential Revision: D55208396 Pulled By: arushikesarwani94 fbshipit-source-id: a1fb1f4cee8483cf91ebededd1d7c4ba7021f9d9 --- .../react/uimanager/ViewManagerRegistry.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java index 1f3c219250a71f..b7d7e969649967 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerRegistry.java @@ -53,16 +53,32 @@ public ViewManagerRegistry(Map viewManagerMap) { * @return the {@link ViewManager} registered to the className received as a parameter */ public synchronized ViewManager get(String className) { + // 1. Try to get the manager without the prefix. ViewManager viewManager = mViewManagers.get(className); if (viewManager != null) { return viewManager; } + + // 2. Try to get the manager with the RCT prefix. + String rctViewManagerName = "RCT" + className; + viewManager = mViewManagers.get(rctViewManagerName); + if (viewManager != null) { + return viewManager; + } if (mViewManagerResolver != null) { + // 1. Try to get the manager without the prefix. viewManager = getViewManagerFromResolver(className); if (viewManager != null) return viewManager; + + // 2. Try to get the manager with the RCT prefix. + viewManager = getViewManagerFromResolver(rctViewManagerName); + if (viewManager != null) return viewManager; + throw new IllegalViewOperationException( - "ViewManagerResolver returned null for " + "ViewManagerResolver returned null for either " + className + + " or " + + rctViewManagerName + ", existing names are: " + mViewManagerResolver.getViewManagerNames()); }