From 997b7199ab4c545e246cd05ca5354602d099f0ae Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Mon, 25 Nov 2024 13:09:49 -0500 Subject: [PATCH] fix(android): preserve new-arch classnames used in backwards-compat logic We use dynamic class-loading via reflection to determine if we are in new architecture mode or not and load the ReactContext If we don't correctly preserve those names in minimization, we will crash in a new architecture context on release builds as the class names will have moved during the minimization --- packages/react-native/android/proguard-rules.pro | 10 +++++++++- .../java/io/invertase/notifee/NotifeeReactUtils.java | 12 ++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/react-native/android/proguard-rules.pro b/packages/react-native/android/proguard-rules.pro index 0bc426ba..80ec12f0 100644 --- a/packages/react-native/android/proguard-rules.pro +++ b/packages/react-native/android/proguard-rules.pro @@ -1,9 +1,17 @@ -keep class io.invertase.notifee.NotifeeEventSubscriber -keep class io.invertase.notifee.NotifeeInitProvider --keepnames class com.facebook.react.ReactActivity -keepnames class io.invertase.notifee.NotifeePackage -keepnames class io.invertase.notifee.NotifeeApiModule +# We depend on certain classes to exist under their names for dynamic +# class-loading to work. We use this to handle new arch / old arch backwards +# compatibility despite the class names moving around +-keep class com.facebook.react.defaults.DefaultNewArchitectureEntryPoint { *; } +-keep class com.facebook.react.ReactApplication { *; } +-keep class com.facebook.react.ReactHost { *; } +-keep class * extends com.facebook.react.ReactHost { *; } +-keepnames class com.facebook.react.ReactActivity + # Preserve all annotations. -keepattributes *Annotation* diff --git a/packages/react-native/android/src/main/java/io/invertase/notifee/NotifeeReactUtils.java b/packages/react-native/android/src/main/java/io/invertase/notifee/NotifeeReactUtils.java index 2e2faa4c..d5aeb932 100644 --- a/packages/react-native/android/src/main/java/io/invertase/notifee/NotifeeReactUtils.java +++ b/packages/react-native/android/src/main/java/io/invertase/notifee/NotifeeReactUtils.java @@ -118,7 +118,7 @@ static void promiseResolver(Promise promise, Exception e) { Method bridgelessEnabled = entryPoint.getMethod("getBridgelessEnabled"); Object result = bridgelessEnabled.invoke(null); if (result == Boolean.TRUE) { - Log.d("getReactContext", "We are in bridgeless new architecture mode"); + Log.d("Notifee::getReactContext", "We are in bridgeless new architecture mode"); Object reactApplication = EventSubscriber.getContext(); Method getReactHost = reactApplication.getClass().getMethod("getReactHost"); Object reactHostInstance = getReactHost.invoke(reactApplication); @@ -126,22 +126,22 @@ static void promiseResolver(Promise promise, Exception e) { reactHostInstance.getClass().getMethod("getCurrentReactContext"); return (ReactContext) getCurrentReactContext.invoke(reactHostInstance); } else { - Log.d("getReactContext", "we are NOT in bridgeless new architecture mode"); + Log.d("Notifee::getReactContext", "we are NOT in bridgeless new architecture mode"); } } catch (Exception e) { - Log.d("getReactContext", "New Architecture class load failed. Using fallback."); + Log.d("Notifee::getReactContext", "New Architecture class load failed. Using fallback."); } - Log.d("getReactContext", "Determining ReactContext using fallback method"); + Log.d("Notifee::getReactContext", "Determining ReactContext using fallback method"); ReactNativeHost reactNativeHost = ((ReactApplication) EventSubscriber.getContext()).getReactNativeHost(); ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager(); return reactInstanceManager.getCurrentReactContext(); } catch (Exception e) { - Log.w("getReactContext", "ReactHost unexpectedly null", e); + Log.w("Notifee::getReactContext", "ReactHost unexpectedly null", e); } - Log.w("getReactContext", "Unable to determine ReactContext"); + Log.w("Notifee::getReactContext", "Unable to determine ReactContext"); return null; }