Fix crash on ReactInstance due to null returned for getViewManagerNames (#52035)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/52035

Fixes https://github.com/facebook/react-native/issues/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
This commit is contained in:
Nicola Corti
2025-06-23 15:46:14 +00:00
committed by React Native Bot
parent 69a5ad5d8c
commit 4d346e2b2d
2 changed files with 17 additions and 1 deletions
@@ -1076,6 +1076,10 @@ public class ReactInstanceManager {
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
Collection<String> names =
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
// When converting this class to Kotlin, you need to retain this null check
// or wrap around a try/catch otherwise this will cause a crash for OSS libraries
// that are not migrated to Kotlin yet and are returning null for
// `getViewManagerNames`
if (names != null) {
uniqueNames.addAll(names);
}
@@ -69,6 +69,7 @@ import com.facebook.react.uimanager.ViewManager
import com.facebook.react.uimanager.ViewManagerRegistry
import com.facebook.react.uimanager.ViewManagerResolver
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.util.RNLog
import com.facebook.soloader.SoLoader
import com.facebook.systrace.Systrace
import com.facebook.systrace.SystraceMessage
@@ -544,7 +545,18 @@ internal class ReactInstance(
for (reactPackage in reactPackages) {
if (reactPackage is ViewManagerOnDemandReactPackage) {
val names = reactPackage.getViewManagerNames(context)
uniqueNames.addAll(names)
// We need to null check here because some Java implementation of the
// `ViewManagerOnDemandReactPackage` interface could still return null even
// if the method is marked as returning a non-nullable collection in Kotlin.
// See https://github.com/facebook/react-native/issues/52014
@Suppress("SENSELESS_COMPARISON")
if (names == null) {
RNLog.w(
context,
"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.")
} else {
uniqueNames.addAll(names)
}
}
}
return uniqueNames