Created
June 16, 2022 13:12
-
-
Save Kudo/ff8eda980172457b90da37040954b923 to your computer and use it in GitHub Desktop.
expo-dev-launcher + flipper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/node_modules/expo-dev-launcher/android/src/debug/java/expo/modules/devlauncher/launcher/loaders/DevLauncherAppLoader.kt b/node_modules/expo-dev-launcher/android/src/debug/java/expo/modules/devlauncher/launcher/loaders/DevLauncherAppLoader.kt | |
index ee9e76c..ad3a915 100644 | |
--- a/node_modules/expo-dev-launcher/android/src/debug/java/expo/modules/devlauncher/launcher/loaders/DevLauncherAppLoader.kt | |
+++ b/node_modules/expo-dev-launcher/android/src/debug/java/expo/modules/devlauncher/launcher/loaders/DevLauncherAppLoader.kt | |
@@ -10,6 +10,7 @@ import com.facebook.react.ReactInstanceManager | |
import com.facebook.react.ReactNativeHost | |
import com.facebook.react.bridge.ReactContext | |
import expo.modules.devlauncher.launcher.DevLauncherControllerInterface | |
+import java.lang.reflect.InvocationTargetException | |
import kotlin.coroutines.Continuation | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.suspendCoroutine | |
@@ -54,6 +55,7 @@ abstract class DevLauncherAppLoader( | |
controller.onAppLoaded(context) | |
onReactContext(context) | |
+ maybeInitFlipper(context.applicationContext, appHost.reactInstanceManager) | |
appHost.reactInstanceManager.removeReactInstanceEventListener(this) | |
reactContextWasInitialized = true | |
continuation!!.resume(true) | |
@@ -94,4 +96,26 @@ abstract class DevLauncherAppLoader( | |
private fun launchIntent(intent: Intent) { | |
context.applicationContext.startActivity(intent) | |
} | |
+ | |
+ private fun maybeInitFlipper(appContext: Context, reactInstanceManager: ReactInstanceManager) { | |
+ try { | |
+ /* | |
+ We use reflection here to pick up the class that initializes Flipper, | |
+ since Flipper library is not available in release mode | |
+ */ | |
+ val packageName = appContext.packageName | |
+ val aClass = Class.forName("$packageName.ReactNativeFlipper") | |
+ aClass | |
+ .getMethod("initializeFlipper", Context::class.java, ReactInstanceManager::class.java) | |
+ .invoke(null, context, reactInstanceManager) | |
+ } catch (e: ClassNotFoundException) { | |
+ e.printStackTrace() | |
+ } catch (e: NoSuchMethodException) { | |
+ e.printStackTrace() | |
+ } catch (e: IllegalAccessException) { | |
+ e.printStackTrace() | |
+ } catch (e: InvocationTargetException) { | |
+ e.printStackTrace() | |
+ } | |
+ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/android/app/src/debug/java/com/test/expoexample/ReactNativeFlipper.java b/android/app/src/debug/java/com/test/expoexample/ReactNativeFlipper.java | |
index 77543f3..d728f62 100644 | |
--- a/android/app/src/debug/java/com/test/expoexample/ReactNativeFlipper.java | |
+++ b/android/app/src/debug/java/com/test/expoexample/ReactNativeFlipper.java | |
@@ -10,6 +10,7 @@ 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.core.FlipperPlugin; | |
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin; | |
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin; | |
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin; | |
@@ -22,11 +23,35 @@ import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPl | |
import com.facebook.react.ReactInstanceManager; | |
import com.facebook.react.bridge.ReactContext; | |
import com.facebook.react.modules.network.NetworkingModule; | |
+ | |
+import java.util.Arrays; | |
+import java.util.List; | |
+ | |
import tech.bam.rnperformance.flipper.RNPerfMonitorPlugin; | |
import okhttp3.OkHttpClient; | |
public class ReactNativeFlipper { | |
public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { | |
+ if (AndroidFlipperClient.getInstanceIfInitialized() != null) { | |
+ final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized(); | |
+ client.stop(); | |
+ final List<Class> pluginClasses = Arrays.asList( | |
+ InspectorFlipperPlugin.class, | |
+ ReactFlipperPlugin.class, | |
+ DatabasesFlipperPlugin.class, | |
+ SharedPreferencesFlipperPlugin.class, | |
+ CrashReporterPlugin.class, | |
+ RNPerfMonitorPlugin.class, | |
+ NetworkFlipperPlugin.class, | |
+ FrescoFlipperPlugin.class | |
+ ); | |
+ for (Class pluginClass : pluginClasses) { | |
+ FlipperPlugin plugin = client.getPluginByClass(pluginClass); | |
+ if (plugin != null) { | |
+ client.removePlugin(plugin); | |
+ } | |
+ } | |
+ } | |
if (FlipperUtils.shouldEnableFlipper(context)) { | |
final FlipperClient client = AndroidFlipperClient.getInstance(context); | |
client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment