From 9214da12385fd88264ca5de3e5fdc78559ec3080 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 11 Aug 2022 05:10:39 -0700 Subject: [PATCH] Do not load Flipper via reflection (#34383) Summary: Followup to https://github.com/facebook/react-native/issues/34379 by danilobuerger Loading Flipper via reflection is type unsafe and requires extra code + exception handling that we can get rid of. The recommended way to use Flipper on Android is either via a `no-op` artifact or by using build flavors. As we already had a setup for Flipper for `debug`, I'm creating the `release` equivalent which is just a stub. This allows us to get rid of some code inside `MainApplication.java` ## Changelog [Android] [Changed] - Do not load Flipper via reflection Pull Request resolved: https://github.com/facebook/react-native/pull/34383 Test Plan: Will wait for a CI result on this. Reviewed By: cipolleschi Differential Revision: D38615257 Pulled By: cortinico fbshipit-source-id: 66bb2c46c5df36a15c1b27512209a849f55d64c9 --- .../com/helloworld/ReactNativeFlipper.java | 4 +++ .../java/com/helloworld/MainApplication.java | 33 +------------------ .../com/helloworld/ReactNativeFlipper.java | 20 +++++++++++ 3 files changed, 25 insertions(+), 32 deletions(-) create mode 100644 template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.java diff --git a/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java b/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java index 8ed784fe5af7c4..283027d2df69d5 100644 --- a/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java +++ b/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java @@ -25,6 +25,10 @@ import com.facebook.react.modules.network.NetworkingModule; import okhttp3.OkHttpClient; +/** + * Class responsible of loading Flipper inside your React Native application. This is the debug + * flavor of it. Here you can add your own plugins and customize the Flipper setup. + */ public class ReactNativeFlipper { public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { if (FlipperUtils.shouldEnableFlipper(context)) { diff --git a/template/android/app/src/main/java/com/helloworld/MainApplication.java b/template/android/app/src/main/java/com/helloworld/MainApplication.java index 9c6a6d8298689b..8c15d22c596318 100644 --- a/template/android/app/src/main/java/com/helloworld/MainApplication.java +++ b/template/android/app/src/main/java/com/helloworld/MainApplication.java @@ -1,16 +1,13 @@ package com.helloworld; import android.app.Application; -import android.content.Context; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; -import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.config.ReactFeatureFlags; import com.facebook.soloader.SoLoader; import com.helloworld.newarchitecture.MainApplicationReactNativeHost; -import java.lang.reflect.InvocationTargetException; import java.util.List; public class MainApplication extends Application implements ReactApplication { @@ -55,34 +52,6 @@ public void onCreate() { // 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()); - } - - /** - * 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("com.helloworld.ReactNativeFlipper"); - aClass - .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class) - .invoke(null, context, reactInstanceManager); - } catch (ClassNotFoundException - | InvocationTargetException - | IllegalAccessException - | NoSuchMethodException e) { - e.printStackTrace(); - } - } + ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); } } diff --git a/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.java b/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.java new file mode 100644 index 00000000000000..b5103644047489 --- /dev/null +++ b/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.java @@ -0,0 +1,20 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + *

This source code is licensed under the MIT license found in the LICENSE file in the root + * directory of this source tree. + */ +package com.helloworld; + +import android.content.Context; +import com.facebook.react.ReactInstanceManager; + +/** + * Class responsible of loading Flipper inside your React Native application. This is the release + * flavor of it so it's empty as we don't want to load Flipper. + */ +public class ReactNativeFlipper { + public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { + // Do nothing as we don't want to initialize Flipper on Release. + } +}