Skip to content

Commit 6e92ee9

Browse files
cortinicoyayvery
authored andcommitted
Move Flipper integration to a separate Gradle module inside ReactAndroid (facebook#37688)
Summary: Pull Request resolved: facebook#37688 This moves the `ReactNativeFlipper` classes used to configure Flipper on Android from the template to a separate Gradle artifact that will be published under the coordinates: ``` com.facebook.react:flipper-integration:0.73.x ``` This reduces the footprint of Flipper on the app template and makes easier for user on 0.73 to migrate to Kotlin (as they will now have to migrate only 2 files rather than 4). Changelog: [Android] [Changed] - Move Flipper integration to a separate Gradle module inside `ReactAndroid` Reviewed By: huntie Differential Revision: D46441588 fbshipit-source-id: e197f29b7386b52091b8d38ed09bbd8f74a997df
1 parent 7768980 commit 6e92ee9

File tree

14 files changed

+146
-96
lines changed

14 files changed

+146
-96
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ project.xcworkspace
3838
/packages/react-native/ReactAndroid/gradlew.bat
3939
/packages/react-native/ReactAndroid/external-artifacts/build/
4040
/packages/react-native/ReactAndroid/external-artifacts/artifacts/
41+
/packages/react-native/ReactAndroid/flipper-integration/build/
4142
/packages/react-native/ReactAndroid/hermes-engine/build/
4243
/packages/react-native/ReactAndroid/hermes-engine/.cxx/
4344
/packages/react-native/template/android/app/build/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Make sure we never publish the build folders to npm.
2+
build/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
plugins {
9+
id("com.android.library")
10+
id("maven-publish")
11+
id("signing")
12+
id("org.jetbrains.kotlin.android")
13+
}
14+
15+
group = "com.facebook.react"
16+
version = parent.publishing_version
17+
18+
repositories {
19+
// Normally RNGP will set repositories for all modules,
20+
// but when consumed from source, we need to re-declare
21+
// those repositories as there is no app module there.
22+
mavenCentral()
23+
google()
24+
}
25+
26+
android {
27+
compileSdk 33
28+
buildToolsVersion = "33.0.0"
29+
namespace "com.facebook.react.flipper"
30+
31+
defaultConfig {
32+
minSdk = 21
33+
}
34+
35+
compileOptions {
36+
sourceCompatibility = JavaVersion.VERSION_11
37+
targetCompatibility = JavaVersion.VERSION_11
38+
}
39+
40+
kotlin {
41+
jvmToolchain(11)
42+
}
43+
44+
dependencies {
45+
implementation project(':packages:react-native:ReactAndroid')
46+
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
47+
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
48+
exclude group:'com.squareup.okhttp3', module:'okhttp'
49+
}
50+
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
51+
}
52+
53+
publishing {
54+
multipleVariants {
55+
withSourcesJar()
56+
allVariants()
57+
}
58+
}
59+
}
60+
61+
apply from: "../publish.gradle"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Version of flipper SDK to use for this integration
2+
FLIPPER_VERSION=0.182.0
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.flipper
9+
10+
import android.content.Context
11+
import com.facebook.flipper.android.AndroidFlipperClient
12+
import com.facebook.flipper.android.utils.FlipperUtils
13+
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin
14+
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin
15+
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin
16+
import com.facebook.flipper.plugins.inspector.DescriptorMapping
17+
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
18+
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor
19+
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin
20+
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin
21+
import com.facebook.react.ReactInstanceEventListener
22+
import com.facebook.react.ReactInstanceManager
23+
import com.facebook.react.bridge.ReactContext
24+
import com.facebook.react.modules.network.NetworkingModule
25+
26+
/**
27+
* Class responsible of loading Flipper inside your React Native application. This is the debug
28+
* flavor of it. Here you can add your own plugins and customize the Flipper setup.
29+
*/
30+
object ReactNativeFlipper {
31+
@JvmStatic
32+
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) {
33+
if (FlipperUtils.shouldEnableFlipper(context)) {
34+
val client = AndroidFlipperClient.getInstance(context)
35+
client.addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()))
36+
client.addPlugin(DatabasesFlipperPlugin(context))
37+
client.addPlugin(SharedPreferencesFlipperPlugin(context))
38+
client.addPlugin(CrashReporterPlugin.getInstance())
39+
val networkFlipperPlugin = NetworkFlipperPlugin()
40+
NetworkingModule.setCustomClientBuilder { builder ->
41+
builder.addNetworkInterceptor(FlipperOkhttpInterceptor(networkFlipperPlugin))
42+
}
43+
client.addPlugin(networkFlipperPlugin)
44+
client.start()
45+
46+
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
47+
// Hence we run if after all native modules have been initialized
48+
val currentReactContext = reactInstanceManager.currentReactContext
49+
if (currentReactContext == null) {
50+
reactInstanceManager.addReactInstanceEventListener(
51+
object : ReactInstanceEventListener {
52+
override fun onReactContextInitialized(context: ReactContext) {
53+
reactInstanceManager.removeReactInstanceEventListener(this)
54+
context.runOnNativeModulesQueueThread { client.addPlugin(FrescoFlipperPlugin()) }
55+
}
56+
})
57+
} else {
58+
client.addPlugin(FrescoFlipperPlugin())
59+
}
60+
}
61+
}
62+
}

packages/rn-tester/android/app/src/release/java/com/facebook/react/uiapp/ReactNativeFlipper.java renamed to ReactAndroid/flipper-integration/src/release/java/com/facebook/react/flipper/ReactNativeFlipper.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
package com.facebook.react.uiapp;
8+
package com.facebook.react.flipper
99

10-
import android.content.Context;
11-
import com.facebook.react.ReactInstanceManager;
10+
import android.content.Context
11+
import com.facebook.react.ReactInstanceManager
1212

1313
/**
1414
* Class responsible of loading Flipper inside your React Native application. This is the release
1515
* flavor of it so it's empty as we don't want to load Flipper.
1616
*/
17-
public class ReactNativeFlipper {
18-
public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
17+
object ReactNativeFlipper {
18+
@Suppress("UNUSED_PARAMETER")
19+
@JvmStatic
20+
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) {
1921
// Do nothing as we don't want to initialize Flipper on Release.
2022
}
2123
}

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ internal object DependencyUtils {
4545
/**
4646
* This method takes care of configuring the resolution strategy for both the app and all the 3rd
4747
* party libraries which are auto-linked. Specifically it takes care of:
48-
* - Forcing the react-android/hermes-android version to the one specified in the package.json
48+
* - Forcing the react-android/hermes-android/flipper-integration version to the one specified in
49+
* the package.json
4950
* - Substituting `react-native` with `react-android` and `hermes-engine` with `hermes-android`.
5051
*/
5152
fun configureDependencies(
@@ -83,6 +84,7 @@ internal object DependencyUtils {
8384
configuration.resolutionStrategy.force(
8485
"${groupString}:react-android:${versionString}",
8586
"${groupString}:hermes-android:${versionString}",
87+
"${groupString}:flipper-integration:${versionString}",
8688
)
8789
}
8890
}

packages/rn-tester/android/app/build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,11 @@ android {
156156
dependencies {
157157
// Build React Native from source
158158
implementation project(':packages:react-native:ReactAndroid')
159+
implementation project(':packages:react-native:ReactAndroid:flipper-integration')
159160

160161
// Consume Hermes as built from source only for the Hermes variant.
161162
hermesImplementation(project(":packages:react-native:ReactAndroid:hermes-engine"))
162163

163-
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
164-
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}")
165-
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
166-
167164
jscImplementation jscFlavor
168165

169166
androidTestImplementation 'junit:junit:4.12'

packages/rn-tester/android/app/gradle.properties

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ org.gradle.parallel=true
44
android.useAndroidX=true
55
android.enableJetifier=true
66

7-
# Version of flipper SDK to use with React Native
8-
FLIPPER_VERSION=0.182.0
9-
107
# RN-Tester is building with NewArch always enabled
118
newArchEnabled=true
129
# RN-Tester is running with Hermes enabled and filtering variants with enableHermesOnlyInVariants

packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)