From 3429dc1ccf32b6b23f4f0ad3cfdf0d5697af665f Mon Sep 17 00:00:00 2001 From: psionic12 Date: Thu, 20 Jun 2024 13:16:32 -0700 Subject: [PATCH] Log a SoftException on SurfaceMountingManager.addRootView (#34785) Summary: Follow the same solution (do not throw a crash when view ID is set already) used in `ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java` for `ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java` ## Changelog [Android] [Changed] - Log a SoftException on SurfaceMountingManager.addRootView Pull Request resolved: https://github.com/facebook/react-native/pull/34785 Test Plan: None Reviewed By: cipolleschi Differential Revision: D40022263 Pulled By: cortinico fbshipit-source-id: d565d2831e2833ccea55f28ea16083b7bae0ed32 --- .../mounting/SurfaceMountingManager.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java index 63b79a4d222..754bc894574 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java @@ -211,11 +211,22 @@ public class SurfaceMountingManager { + " tag: [%d]", rootView.getId(), mSurfaceId); - throw new IllegalViewOperationException( - "Trying to add a root view with an explicit id already set. React Native uses the" - + " id field to track react tags and will overwrite this field. If that is" - + " fine, explicitly overwrite the id field to View.NO_ID before calling" - + " addRootView."); + // This behavior can not be guaranteed in hybrid apps that have a native android layer + // over + // which reactRootViews are added and the native views need to have ids on them in order + // to + // work. + // Hence this can cause unnecessary crashes at runtime for hybrid apps. + // So converting this to a soft exception such that pure react-native devs can still see + // the + // warning while hybrid apps continue to run without crashes + ReactSoftExceptionLogger.logSoftException( + TAG, + new IllegalViewOperationException( + "Trying to add a root view with an explicit id already set. React Native uses" + + " the id field to track react tags and will overwrite this field. If that" + + " is fine, explicitly overwrite the id field to View.NO_ID before calling" + + " addRootView.")); } rootView.setId(mSurfaceId);