From 055ea9c7b7dea030ef16da72d1f6ecb5d95ac468 Mon Sep 17 00:00:00 2001 From: Geraint White Date: Mon, 25 Oct 2021 08:10:23 -0700 Subject: [PATCH] Fix devDisabledInStaging not working with multiple productFlavors (#30606) Summary: Fixes https://github.com/facebook/react-native/issues/27052 Since react-native 0.62, the `devDisabledIn${buildType}` syntax has stopped working for apps with multiple `productFlavors`. This PR adds the `disableDevForVariant` lambda to allow dev mode to be disabled for different variants. ## Changelog [Android] [Fixed] - Fix devDisabledIn not working with multiple productFlavors Pull Request resolved: https://github.com/facebook/react-native/pull/30606 Test Plan: I added the following log into `react.gradle` and ran the Android build for my app: ``` println("devEnabled: ${targetName}, ${devEnabled}") ``` ``` # build.gradle project.ext.react = [ entryFile: "index.android.js", enableHermes: true, // clean and rebuild if changing bundleInLive: true, disableDevForVariant: { def variant -> variant.name.toLowerCase().contains('release') || variant.name.toLowerCase().contains('live') }, ] ... flavorDimensions 'branding' productFlavors { cve { dimension 'branding' } whce { dimension 'branding' } } ``` Console output: ``` Reading env from: env/cve/live devEnabled: CveDebug, true devEnabled: CveRelease, false devEnabled: CveLive, false devEnabled: WhceDebug, true devEnabled: WhceRelease, false devEnabled: WhceLive, false ``` Reviewed By: cortinico Differential Revision: D31649977 Pulled By: ShikaSD fbshipit-source-id: 520734314f4bca7608b8dca67c7c5ce0be6d31a5 --- .../src/main/kotlin/com/facebook/react/ReactExtension.kt | 8 ++++++++ .../main/kotlin/com/facebook/react/TaskConfiguration.kt | 2 +- react.gradle | 9 +++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt index c6c85ba26e5790..0d582772ac64aa 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt @@ -114,6 +114,14 @@ abstract class ReactExtension @Inject constructor(project: Project) { val devDisabledInVariants: ListProperty = objects.listProperty(String::class.java).convention(emptyList()) + /** + * Functional interface to disable dev mode only on specific [BaseVariant] Default: will check + * [devDisabledInVariants] or return True for Release variants and False for Debug variants. + */ + var disableDevForVariant: (BaseVariant) -> Boolean = { variant -> + variant.name in devDisabledInVariants.get() || variant.isRelease + } + /** * Variant Name to Boolean map that allows to toggle the bundle command for a specific variant. * Default: {} diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt index b14b138cc72b23..12700ec57247f1 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt @@ -64,7 +64,7 @@ internal fun Project.configureReactTasks(variant: BaseVariant, config: ReactExte } it.execCommand = execCommand it.bundleCommand = config.bundleCommand.get() - it.devEnabled = !(variant.name in config.devDisabledInVariants.get() || isRelease) + it.devEnabled = !config.disableDevForVariant(variant) it.entryFile = detectedEntryFile(config) val extraArgs = mutableListOf() diff --git a/react.gradle b/react.gradle index 528b7a0c36b10f..d903659f7f0f62 100644 --- a/react.gradle +++ b/react.gradle @@ -99,6 +99,12 @@ def hermesFlagsForVariant = config.hermesFlagsForVariant ?: { return hermesFlags } +// Set disableDevForVariant to a function to configure per variant, +// defaults to `devDisabledIn${targetName}` or True for Release variants and False for debug variants +def disableDevForVariant = config.disableDevForVariant ?: { + def variant -> config."devDisabledIn${variant.name.capitalize()}" || variant.name.toLowerCase().contains("release") +} + // Set deleteDebugFilesForVariant to a function to configure per variant, // defaults to True for Release variants and False for debug variants def deleteDebugFilesForVariant = config.deleteDebugFilesForVariant ?: { @@ -172,8 +178,7 @@ afterEvaluate { workingDir(reactRoot) // Set up dev mode - def devEnabled = !(config."devDisabledIn${targetName}" - || targetName.toLowerCase().contains("release")) + def devEnabled = !disableDevForVariant(variant) def extraArgs = []