From 16201f8be3cb38ce5c2efb6dd4639fde9b16002d Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Tue, 4 Jul 2023 06:01:09 -0700 Subject: [PATCH] Move Flipper integration to a separate Gradle module inside `ReactAndroid` (#37688) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .gitignore | 1 + build.gradle.kts | 3 + .../facebook/react/utils/DependencyUtils.kt | 4 +- .../react/utils/NdkConfiguratorUtils.kt | 2 +- .../flipper-integration/.npmignore | 2 + .../flipper-integration/build.gradle | 61 +++++++++++++++ .../flipper-integration/gradle.properties | 2 + .../react/flipper}/ReactNativeFlipper.kt | 10 ++- .../react/flipper}/ReactNativeFlipper.kt | 10 ++- packages/react-native/settings.gradle.kts | 5 ++ .../template/android/app/build.gradle | 7 +- .../java/com/helloworld/MainApplication.kt | 1 + .../template/android/gradle.properties | 3 - packages/rn-tester/android/app/build.gradle | 5 +- .../rn-tester/android/app/gradle.properties | 3 - .../react/uiapp/ReactNativeFlipper.java | 74 ------------------- .../react/uiapp/RNTesterApplication.java | 1 + .../react/uiapp/ReactNativeFlipper.java | 21 ------ settings.gradle.kts | 1 + 19 files changed, 101 insertions(+), 115 deletions(-) create mode 100644 packages/react-native/ReactAndroid/flipper-integration/.npmignore create mode 100644 packages/react-native/ReactAndroid/flipper-integration/build.gradle create mode 100644 packages/react-native/ReactAndroid/flipper-integration/gradle.properties rename packages/react-native/{template/android/app/src/debug/java/com/helloworld => ReactAndroid/flipper-integration/src/debug/java/com/facebook/react/flipper}/ReactNativeFlipper.kt (91%) rename packages/react-native/{template/android/app/src/release/java/com/helloworld => ReactAndroid/flipper-integration/src/release/java/com/facebook/react/flipper}/ReactNativeFlipper.kt (66%) delete mode 100644 packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java delete mode 100644 packages/rn-tester/android/app/src/release/java/com/facebook/react/uiapp/ReactNativeFlipper.java diff --git a/.gitignore b/.gitignore index 5f30b988d17..90f2382d7df 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ project.xcworkspace /packages/react-native/ReactAndroid/gradlew.bat /packages/react-native/ReactAndroid/external-artifacts/build/ /packages/react-native/ReactAndroid/external-artifacts/artifacts/ +/packages/react-native/ReactAndroid/flipper-integration/build/ /packages/react-native/ReactAndroid/hermes-engine/build/ /packages/react-native/ReactAndroid/hermes-engine/.cxx/ /packages/react-native/template/android/app/build/ diff --git a/build.gradle.kts b/build.gradle.kts index db10f36431a..c5b21e74cb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -99,6 +99,8 @@ tasks.register("publishAllToMavenTempLocal") { description = "Publish all the artifacts to be available inside a Maven Local repository on /tmp." dependsOn(":packages:react-native:ReactAndroid:publishAllPublicationsToMavenTempLocalRepository") // We don't publish the external-artifacts to Maven Local as CircleCI is using it via workspace. + dependsOn( + ":packages:react-native:ReactAndroid:flipper-integration:publishAllPublicationsToMavenTempLocalRepository") dependsOn( ":packages:react-native:ReactAndroid:hermes-engine:publishAllPublicationsToMavenTempLocalRepository") } @@ -107,5 +109,6 @@ tasks.register("publishAllToSonatype") { description = "Publish all the artifacts to Sonatype (Maven Central or Snapshot repository)" dependsOn(":packages:react-native:ReactAndroid:publishToSonatype") dependsOn(":packages:react-native:ReactAndroid:external-artifacts:publishToSonatype") + dependsOn(":packages:react-native:ReactAndroid:flipper-integration:publishToSonatype") dependsOn(":packages:react-native:ReactAndroid:hermes-engine:publishToSonatype") } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt index 7c34ce80d1a..a82696e93b4 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt @@ -45,7 +45,8 @@ internal object DependencyUtils { /** * This method takes care of configuring the resolution strategy for both the app and all the 3rd * party libraries which are auto-linked. Specifically it takes care of: - * - Forcing the react-android/hermes-android version to the one specified in the package.json + * - Forcing the react-android/hermes-android/flipper-integration version to the one specified in + * the package.json * - Substituting `react-native` with `react-android` and `hermes-engine` with `hermes-android`. */ fun configureDependencies( @@ -68,6 +69,7 @@ internal object DependencyUtils { configuration.resolutionStrategy.force( "${groupString}:react-android:${versionString}", "${groupString}:hermes-android:${versionString}", + "${groupString}:flipper-integration:${versionString}", ) } } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt index 28a33b85f8f..5c3faa99417 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/NdkConfiguratorUtils.kt @@ -78,7 +78,7 @@ internal object NdkConfiguratorUtils { ) { if (!project.isNewArchEnabled) { // For Old Arch, we set a pickFirst only on libraries that we know are - // clashing with our direct dependencies (FBJNI, Flipper and Hermes). + // clashing with our direct dependencies (mainly FBJNI and Hermes). variant.packaging.jniLibs.pickFirsts.addAll( listOf( "**/libfbjni.so", diff --git a/packages/react-native/ReactAndroid/flipper-integration/.npmignore b/packages/react-native/ReactAndroid/flipper-integration/.npmignore new file mode 100644 index 00000000000..b1bfa20d8f8 --- /dev/null +++ b/packages/react-native/ReactAndroid/flipper-integration/.npmignore @@ -0,0 +1,2 @@ +# Make sure we never publish the build folders to npm. +build/ diff --git a/packages/react-native/ReactAndroid/flipper-integration/build.gradle b/packages/react-native/ReactAndroid/flipper-integration/build.gradle new file mode 100644 index 00000000000..5094bd983cf --- /dev/null +++ b/packages/react-native/ReactAndroid/flipper-integration/build.gradle @@ -0,0 +1,61 @@ +/* + * 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. + */ + +plugins { + id("com.android.library") + id("maven-publish") + id("signing") + id("org.jetbrains.kotlin.android") +} + +group = "com.facebook.react" +version = parent.publishing_version + +repositories { + // Normally RNGP will set repositories for all modules, + // but when consumed from source, we need to re-declare + // those repositories as there is no app module there. + mavenCentral() + google() +} + +android { + compileSdk 33 + buildToolsVersion = "33.0.0" + namespace "com.facebook.react.flipper" + + defaultConfig { + minSdk = 21 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + kotlin { + jvmToolchain(11) + } + + dependencies { + implementation project(':packages:react-native:ReactAndroid') + debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") + debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") { + exclude group:'com.squareup.okhttp3', module:'okhttp' + } + debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") + } + + publishing { + multipleVariants { + withSourcesJar() + allVariants() + } + } +} + +apply from: "../publish.gradle" diff --git a/packages/react-native/ReactAndroid/flipper-integration/gradle.properties b/packages/react-native/ReactAndroid/flipper-integration/gradle.properties new file mode 100644 index 00000000000..637bb4b57cc --- /dev/null +++ b/packages/react-native/ReactAndroid/flipper-integration/gradle.properties @@ -0,0 +1,2 @@ +# Version of flipper SDK to use for this integration +FLIPPER_VERSION=0.182.0 \ No newline at end of file diff --git a/packages/react-native/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.kt b/packages/react-native/ReactAndroid/flipper-integration/src/debug/java/com/facebook/react/flipper/ReactNativeFlipper.kt similarity index 91% rename from packages/react-native/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.kt rename to packages/react-native/ReactAndroid/flipper-integration/src/debug/java/com/facebook/react/flipper/ReactNativeFlipper.kt index c628808885f..ecbef22f773 100644 --- a/packages/react-native/template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.kt +++ b/packages/react-native/ReactAndroid/flipper-integration/src/debug/java/com/facebook/react/flipper/ReactNativeFlipper.kt @@ -1,4 +1,11 @@ -package com.helloworld +/* + * 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.facebook.react.flipper import android.content.Context import com.facebook.flipper.android.AndroidFlipperClient @@ -21,6 +28,7 @@ import com.facebook.react.modules.network.NetworkingModule * flavor of it. Here you can add your own plugins and customize the Flipper setup. */ object ReactNativeFlipper { + @JvmStatic fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) { if (FlipperUtils.shouldEnableFlipper(context)) { val client = AndroidFlipperClient.getInstance(context) diff --git a/packages/react-native/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.kt b/packages/react-native/ReactAndroid/flipper-integration/src/release/java/com/facebook/react/flipper/ReactNativeFlipper.kt similarity index 66% rename from packages/react-native/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.kt rename to packages/react-native/ReactAndroid/flipper-integration/src/release/java/com/facebook/react/flipper/ReactNativeFlipper.kt index 5eea0530a74..e44f63d8d1d 100644 --- a/packages/react-native/template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.kt +++ b/packages/react-native/ReactAndroid/flipper-integration/src/release/java/com/facebook/react/flipper/ReactNativeFlipper.kt @@ -1,4 +1,11 @@ -package com.helloworld +/* + * 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.facebook.react.flipper import android.content.Context import com.facebook.react.ReactInstanceManager @@ -9,6 +16,7 @@ import com.facebook.react.ReactInstanceManager */ object ReactNativeFlipper { @Suppress("UNUSED_PARAMETER") + @JvmStatic fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) { // Do nothing as we don't want to initialize Flipper on Release. } diff --git a/packages/react-native/settings.gradle.kts b/packages/react-native/settings.gradle.kts index df9e6e8d294..c0748c7b488 100644 --- a/packages/react-native/settings.gradle.kts +++ b/packages/react-native/settings.gradle.kts @@ -29,3 +29,8 @@ include(":packages:react-native:ReactAndroid:hermes-engine") project(":packages:react-native:ReactAndroid:hermes-engine").projectDir = file("ReactAndroid/hermes-engine/") + +include(":packages:react-native:ReactAndroid:flipper-integration") + +project(":packages:react-native:ReactAndroid:flipper-integration").projectDir = + file("ReactAndroid/flipper-integration/") diff --git a/packages/react-native/template/android/app/build.gradle b/packages/react-native/template/android/app/build.gradle index b464d3616cd..00e28c6f065 100644 --- a/packages/react-native/template/android/app/build.gradle +++ b/packages/react-native/template/android/app/build.gradle @@ -107,13 +107,8 @@ android { dependencies { // The version of react-native is set by the React Native Gradle Plugin implementation("com.facebook.react:react-android") + implementation("com.facebook.react:flipper-integration") - debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") - debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") { - exclude group:'com.squareup.okhttp3', module:'okhttp' - } - - debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") if (hermesEnabled.toBoolean()) { implementation("com.facebook.react:hermes-android") } else { diff --git a/packages/react-native/template/android/app/src/main/java/com/helloworld/MainApplication.kt b/packages/react-native/template/android/app/src/main/java/com/helloworld/MainApplication.kt index 7da676738b0..77a64826ad9 100644 --- a/packages/react-native/template/android/app/src/main/java/com/helloworld/MainApplication.kt +++ b/packages/react-native/template/android/app/src/main/java/com/helloworld/MainApplication.kt @@ -7,6 +7,7 @@ import com.facebook.react.ReactNativeHost import com.facebook.react.ReactPackage import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load import com.facebook.react.defaults.DefaultReactNativeHost +import com.facebook.react.flipper.ReactNativeFlipper import com.facebook.soloader.SoLoader class MainApplication : Application(), ReactApplication { diff --git a/packages/react-native/template/android/gradle.properties b/packages/react-native/template/android/gradle.properties index a3b2fa12490..a46a5b90fad 100644 --- a/packages/react-native/template/android/gradle.properties +++ b/packages/react-native/template/android/gradle.properties @@ -24,9 +24,6 @@ android.useAndroidX=true # Automatically convert third-party libraries to use AndroidX android.enableJetifier=true -# Version of flipper SDK to use with React Native -FLIPPER_VERSION=0.182.0 - # Use this property to specify which architecture you want to build. # You can also override it from the CLI using # ./gradlew -PreactNativeArchitectures=x86_64 diff --git a/packages/rn-tester/android/app/build.gradle b/packages/rn-tester/android/app/build.gradle index e9e68fee660..d4cbe004cf1 100644 --- a/packages/rn-tester/android/app/build.gradle +++ b/packages/rn-tester/android/app/build.gradle @@ -156,14 +156,11 @@ android { dependencies { // Build React Native from source implementation project(':packages:react-native:ReactAndroid') + implementation project(':packages:react-native:ReactAndroid:flipper-integration') // Consume Hermes as built from source only for the Hermes variant. hermesImplementation(project(":packages:react-native:ReactAndroid:hermes-engine")) - debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") - debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") - debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") - jscImplementation jscFlavor androidTestImplementation 'junit:junit:4.12' diff --git a/packages/rn-tester/android/app/gradle.properties b/packages/rn-tester/android/app/gradle.properties index 1d6bf975964..afa8164ce4c 100644 --- a/packages/rn-tester/android/app/gradle.properties +++ b/packages/rn-tester/android/app/gradle.properties @@ -4,9 +4,6 @@ org.gradle.parallel=true android.useAndroidX=true android.enableJetifier=true -# Version of flipper SDK to use with React Native -FLIPPER_VERSION=0.182.0 - # RN-Tester is building with NewArch always enabled newArchEnabled=true # RN-Tester is running with Hermes enabled and filtering variants with enableHermesOnlyInVariants diff --git a/packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java b/packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java deleted file mode 100644 index 28579c8450d..00000000000 --- a/packages/rn-tester/android/app/src/debug/java/com/facebook/react/uiapp/ReactNativeFlipper.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.facebook.react.uiapp; - -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.fresco.FrescoFlipperPlugin; -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.ReactInstanceEventListener; -import com.facebook.react.ReactInstanceManager; -import com.facebook.react.bridge.ReactContext; -import com.facebook.react.modules.network.NetworkingModule; -import okhttp3.OkHttpClient; - -public class ReactNativeFlipper { - public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { - if (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(); - - // Fresco Plugin needs to ensure that ImagePipelineFactory is initialized - // Hence we run if after all native modules have been initialized - ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); - if (reactContext == null) { - reactInstanceManager.addReactInstanceEventListener( - new ReactInstanceEventListener() { - @Override - public void onReactContextInitialized(ReactContext reactContext) { - reactInstanceManager.removeReactInstanceEventListener(this); - reactContext.runOnNativeModulesQueueThread( - new Runnable() { - @Override - public void run() { - client.addPlugin(new FrescoFlipperPlugin()); - } - }); - } - }); - } else { - client.addPlugin(new FrescoFlipperPlugin()); - } - } - } -} diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java index 8d1cf706d93..53c416400ef 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java @@ -26,6 +26,7 @@ import com.facebook.react.defaults.DefaultComponentsRegistry; import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint; import com.facebook.react.defaults.DefaultReactNativeHost; import com.facebook.react.fabric.ComponentFactory; +import com.facebook.react.flipper.ReactNativeFlipper; import com.facebook.react.interfaces.ReactHostInterface; import com.facebook.react.module.model.ReactModuleInfo; import com.facebook.react.module.model.ReactModuleInfoProvider; diff --git a/packages/rn-tester/android/app/src/release/java/com/facebook/react/uiapp/ReactNativeFlipper.java b/packages/rn-tester/android/app/src/release/java/com/facebook/react/uiapp/ReactNativeFlipper.java deleted file mode 100644 index 26cc2168b99..00000000000 --- a/packages/rn-tester/android/app/src/release/java/com/facebook/react/uiapp/ReactNativeFlipper.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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.facebook.react.uiapp; - -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. - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 43a368a8aa2..2f562866e1a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -15,6 +15,7 @@ pluginManagement { include( ":packages:react-native:ReactAndroid", + ":packages:react-native:ReactAndroid:flipper-integration", ":packages:react-native:ReactAndroid:hermes-engine", ":packages:react-native:ReactAndroid:external-artifacts")