Skip to content

Commit f6b7bd9

Browse files
cortinicofacebook-github-bot
authored andcommitted
Fix crash on ReactInstance due to null returned for getViewManagerNames (#52035)
Summary: Pull Request resolved: #52035 Fixes #52014 Some OSS library is still returning null for `getViewManagerNames` especially if they're implementing the `ViewManagerOnDemandReactPackage` in Java. I'm adding a try-catch here so that we prevent the NPE for those scenarios. Changelog: [Android] [Fixed] - Fix crash on ReactInstance due to null returned for getViewManagerNames Reviewed By: javache Differential Revision: D76723826 fbshipit-source-id: cc159dee389257c6877b03a67840a45ee5bec165
1 parent 2ade12d commit f6b7bd9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,10 @@ public Collection<String> getViewManagerNames() {
10791079
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
10801080
Collection<String> names =
10811081
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
1082+
// When converting this class to Kotlin, you need to retain this null check
1083+
// or wrap around a try/catch otherwise this will cause a crash for OSS libraries
1084+
// that are not migrated to Kotlin yet and are returning null for
1085+
// `getViewManagerNames`
10821086
if (names != null) {
10831087
uniqueNames.addAll(names);
10841088
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import com.facebook.react.uimanager.ViewManager
6767
import com.facebook.react.uimanager.ViewManagerRegistry
6868
import com.facebook.react.uimanager.ViewManagerResolver
6969
import com.facebook.react.uimanager.events.EventDispatcher
70+
import com.facebook.react.util.RNLog
7071
import com.facebook.soloader.SoLoader
7172
import com.facebook.systrace.Systrace
7273
import com.facebook.systrace.SystraceMessage
@@ -542,7 +543,18 @@ internal class ReactInstance(
542543
for (reactPackage in reactPackages) {
543544
if (reactPackage is ViewManagerOnDemandReactPackage) {
544545
val names = reactPackage.getViewManagerNames(context)
545-
uniqueNames.addAll(names)
546+
// We need to null check here because some Java implementation of the
547+
// `ViewManagerOnDemandReactPackage` interface could still return null even
548+
// if the method is marked as returning a non-nullable collection in Kotlin.
549+
// See https://github.com/facebook/react-native/issues/52014
550+
@Suppress("SENSELESS_COMPARISON")
551+
if (names == null) {
552+
RNLog.w(
553+
context,
554+
"The ReactPackage called: `${reactPackage.javaClass.simpleName}` is returning null for getViewManagerNames(). This is violating the signature of the method. That method should be updated to return an empty collection.")
555+
} else {
556+
uniqueNames.addAll(names)
557+
}
546558
}
547559
}
548560
return uniqueNames

0 commit comments

Comments
 (0)