Skip to content

Commit 4d346e2

Browse files
cortinicoreact-native-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 69a5ad5 commit 4d346e2

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
@@ -1076,6 +1076,10 @@ public Collection<String> getViewManagerNames() {
10761076
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
10771077
Collection<String> names =
10781078
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
1079+
// When converting this class to Kotlin, you need to retain this null check
1080+
// or wrap around a try/catch otherwise this will cause a crash for OSS libraries
1081+
// that are not migrated to Kotlin yet and are returning null for
1082+
// `getViewManagerNames`
10791083
if (names != null) {
10801084
uniqueNames.addAll(names);
10811085
}

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
@@ -69,6 +69,7 @@ import com.facebook.react.uimanager.ViewManager
6969
import com.facebook.react.uimanager.ViewManagerRegistry
7070
import com.facebook.react.uimanager.ViewManagerResolver
7171
import com.facebook.react.uimanager.events.EventDispatcher
72+
import com.facebook.react.util.RNLog
7273
import com.facebook.soloader.SoLoader
7374
import com.facebook.systrace.Systrace
7475
import com.facebook.systrace.SystraceMessage
@@ -544,7 +545,18 @@ internal class ReactInstance(
544545
for (reactPackage in reactPackages) {
545546
if (reactPackage is ViewManagerOnDemandReactPackage) {
546547
val names = reactPackage.getViewManagerNames(context)
547-
uniqueNames.addAll(names)
548+
// We need to null check here because some Java implementation of the
549+
// `ViewManagerOnDemandReactPackage` interface could still return null even
550+
// if the method is marked as returning a non-nullable collection in Kotlin.
551+
// See https://github.com/facebook/react-native/issues/52014
552+
@Suppress("SENSELESS_COMPARISON")
553+
if (names == null) {
554+
RNLog.w(
555+
context,
556+
"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.")
557+
} else {
558+
uniqueNames.addAll(names)
559+
}
548560
}
549561
}
550562
return uniqueNames

0 commit comments

Comments
 (0)