From 1e684b463a23b1226ac7f4f265637a911aa24294 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 11 Aug 2022 10:57:56 +0200 Subject: [PATCH] Do not load Flipper via reflection --- .../com/helloworld/ReactNativeFlipper.java | 5 ++++ .../java/com/helloworld/MainApplication.java | 30 +------------------ .../com/helloworld/ReactNativeFlipper.java | 20 +++++++++++++ 3 files changed, 26 insertions(+), 29 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 8ed784fe5af7..3cd44b6d022a 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,11 @@ 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 9c6a6d829868..637e984cd36e 100644 --- a/template/android/app/src/main/java/com/helloworld/MainApplication.java +++ b/template/android/app/src/main/java/com/helloworld/MainApplication.java @@ -10,7 +10,6 @@ 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 +54,7 @@ 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()); + ReactNativeFlipper.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(); - } - } - } } 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 000000000000..557146643df0 --- /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. + } +}