From a2eb8db59cd08643a7f683389947520f5b57c409 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 21 Dec 2022 17:35:45 -0300 Subject: [PATCH] [FIX] Flipper on Android (#4771) --- .../reactnative/MainDebugApplication.java | 25 ------------- .../rocket/reactnative/MainApplication.java | 35 +++++++++++++++++++ android/gradle.properties | 2 +- 3 files changed, 36 insertions(+), 26 deletions(-) delete mode 100644 android/app/src/debug/java/chat/rocket/reactnative/MainDebugApplication.java diff --git a/android/app/src/debug/java/chat/rocket/reactnative/MainDebugApplication.java b/android/app/src/debug/java/chat/rocket/reactnative/MainDebugApplication.java deleted file mode 100644 index e6abf3b05..000000000 --- a/android/app/src/debug/java/chat/rocket/reactnative/MainDebugApplication.java +++ /dev/null @@ -1,25 +0,0 @@ -package chat.rocket.reactnative; - -import android.content.Context; - -import com.facebook.react.ReactInstanceManager; - -public class MainDebugApplication extends MainApplication { - - @Override - public void onCreate() { - super.onCreate(); - initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); - } - - /** - * Loads Flipper in React Native templates. Call this in the onCreate method with something like - * initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); - * - * @param context - * @param reactInstanceManager - */ - private static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { - ReactNativeFlipper.initializeFlipper(context, reactInstanceManager); - } -} diff --git a/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java b/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java index 871e6e86d..b94159e09 100644 --- a/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java +++ b/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java @@ -9,13 +9,16 @@ import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.config.ReactFeatureFlags; +import com.facebook.react.ReactInstanceManager; import com.facebook.soloader.SoLoader; import com.reactnativecommunity.viewpager.RNCViewPagerPackage; import com.facebook.react.bridge.JSIModulePackage; import com.swmansion.reanimated.ReanimatedJSIModulePackage; +import android.content.Context; import android.content.res.Configuration; import expo.modules.ApplicationLifecycleDispatcher; import expo.modules.ReactNativeHostWrapper; +import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; @@ -76,6 +79,7 @@ public class MainApplication extends Application implements ReactApplication { // If you opted-in for the New Architecture, we enable the TurboModule system ReactFeatureFlags.useTurboModules = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED; SoLoader.init(this, /* native exopackage */ false); + initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); ApplicationLifecycleDispatcher.onApplicationCreate(this); } @@ -84,4 +88,35 @@ public class MainApplication extends Application implements ReactApplication { super.onConfigurationChanged(newConfig); ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig); } + + /** + * Loads Flipper in React Native templates. Call this in the onCreate method with something like + * initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); + * + * @param context + * @param reactInstanceManager + */ + private static void initializeFlipper( + Context context, ReactInstanceManager reactInstanceManager) { + if (BuildConfig.DEBUG) { + try { + /* + We use reflection here to pick up the class that initializes Flipper, + since Flipper library is not available in release mode + */ + Class aClass = Class.forName("chat.rocket.reactnative.ReactNativeFlipper"); + aClass + .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class) + .invoke(null, context, reactInstanceManager); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } } diff --git a/android/gradle.properties b/android/gradle.properties index 59600f62e..29d16769c 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -23,7 +23,7 @@ android.useAndroidX=true android.enableJetifier=true # Version of flipper SDK to use with React Native -FLIPPER_VERSION=0.125.0 +FLIPPER_VERSION=0.175.0 # Use this property to specify which architecture you want to build. # You can also override it from the CLI using