diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index c59ab9f50beccc..1ef2f605c46e97 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -379,6 +379,12 @@ dependencies { androidTestImplementation("androidx.test:runner:1.1.0") androidTestImplementation("androidx.test:rules:1.1.0") androidTestImplementation("org.mockito:mockito-core:${MOCKITO_CORE_VERSION}") + + debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") { + exclude group:'com.facebook.yoga' + exclude group:'com.facebook.flipper', module: 'fbjni' + exclude group:'com.facebook.litho', module: 'litho-annotations' + } } apply(from: "release.gradle") diff --git a/ReactAndroid/gradle.properties b/ReactAndroid/gradle.properties index df50cd3a287e9c..f9578765c023e6 100644 --- a/ReactAndroid/gradle.properties +++ b/ReactAndroid/gradle.properties @@ -20,7 +20,8 @@ BOOST_VERSION=1_63_0 DOUBLE_CONVERSION_VERSION=1.1.6 FOLLY_VERSION=2018.10.22.00 GLOG_VERSION=0.3.5 +FLIPPER_VERSION=0.23.4 android.useAndroidX=true android.enableJetifier=true -android.enableR8=false \ No newline at end of file +android.enableR8=false diff --git a/ReactAndroid/src/debug/java/com/facebook/flipper/ReactNativeFlipper.java b/ReactAndroid/src/debug/java/com/facebook/flipper/ReactNativeFlipper.java new file mode 100644 index 00000000000000..c2973ad5d20a60 --- /dev/null +++ b/ReactAndroid/src/debug/java/com/facebook/flipper/ReactNativeFlipper.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) Facebook, Inc. and its 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.facebook.flipper; + +import android.content.Context; +import com.facebook.flipper.android.AndroidFlipperClient; +import com.facebook.flipper.android.utils.FlipperUtils; +import com.facebook.flipper.core.FlipperClient; +import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin; +import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin; +import com.facebook.flipper.plugins.inspector.DescriptorMapping; +import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin; +import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor; +import com.facebook.flipper.plugins.network.NetworkFlipperPlugin; +import com.facebook.flipper.plugins.react.ReactFlipperPlugin; +import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin; +import com.facebook.react.BuildConfig; +import com.facebook.react.modules.network.NetworkingModule; +import okhttp3.OkHttpClient; + +public class ReactNativeFlipper { + public static final String CLASS_NAME = "com.facebook.flipper.ReactNativeFlipper"; + + public static void initializeFlipper(Context context) { + if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(context)) { + final FlipperClient client = AndroidFlipperClient.getInstance(context); + + client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())); + client.addPlugin(new ReactFlipperPlugin()); + client.addPlugin(new DatabasesFlipperPlugin(context)); + client.addPlugin(new SharedPreferencesFlipperPlugin(context)); + client.addPlugin(CrashReporterPlugin.getInstance()); + + NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin(); + NetworkingModule.setCustomClientBuilder( + new NetworkingModule.CustomClientBuilder() { + @Override + public void apply(OkHttpClient.Builder builder) { + builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin)); + } + }); + client.addPlugin(networkFlipperPlugin); + + client.start(); + } + } +} 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 68c6007108a1e0..ae1ff7cf28e462 100644 --- a/template/android/app/src/main/java/com/helloworld/MainApplication.java +++ b/template/android/app/src/main/java/com/helloworld/MainApplication.java @@ -1,11 +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.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.soloader.SoLoader; +import java.lang.reflect.InvocationTargetException; import java.util.List; public class MainApplication extends Application implements ReactApplication { @@ -41,5 +43,32 @@ public ReactNativeHost getReactNativeHost() { public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); + initializeFlipper(this); // Remove this line if you don't want Flipper enabled + } + + /** + * Loads Flipper in React Native templates. + * + * @param context + */ + private static void initializeFlipper(Context context) { + 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.facebook.flipper.ReactNativeFlipper"); + aClass.getMethod("initializeFlipper", Context.class).invoke(null, context); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } } }