Skip to content

Commit 8ad86c7

Browse files
cortinicofacebook-github-bot
authored andcommitted
RNGP - Add Variant Support (#35063)
Summary: Pull Request resolved: #35063 This is part of a series of tasks to make the React Native Gradle Plugin (RNGP) variant-aware. Here I'm add Variant support to RNGP via the Variant API from AGP 7.x A short summary of changes: - I've pushed a number of breaking changes to ReactExtension (we should document them in the release notes). Specifically I've removed properties which I believe were unnecessary and confusing - I've removed all the extra logic to do the .so cleanups and use the new tasks that use the Artifacts API - I've introduced only a `debuggableVariants` to make all the decisions for the bundling and minification which should be sufficient for users - I've removed all the funcional interfaces are replaced them with lists as they're easy to handle and understand for users. Changelog: [Android] [Changed] - Added Flavor Support to React Native Gradle Plugin (RNGP) Reviewed By: cipolleschi Differential Revision: D40335028 fbshipit-source-id: d9ac1437de8a27db2e93df15b13772b221e036b2
1 parent 2cc2ca1 commit 8ad86c7

File tree

6 files changed

+96
-466
lines changed

6 files changed

+96
-466
lines changed

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

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
package com.facebook.react
99

10-
import com.android.build.gradle.api.BaseVariant
1110
import com.facebook.react.utils.projectPathToLibraryName
12-
import java.io.File
1311
import javax.inject.Inject
1412
import org.gradle.api.Project
1513
import org.gradle.api.file.DirectoryProperty
1614
import org.gradle.api.file.RegularFileProperty
1715
import org.gradle.api.provider.ListProperty
18-
import org.gradle.api.provider.MapProperty
1916
import org.gradle.api.provider.Property
2017

2118
abstract class ReactExtension @Inject constructor(project: Project) {
@@ -54,10 +51,10 @@ abstract class ReactExtension @Inject constructor(project: Project) {
5451
val bundleCommand: Property<String> = objects.property(String::class.java).convention("bundle")
5552

5653
/**
57-
* Custom configuration for the [bundleCommand]. If provided it will be passed over with a
54+
* Custom configuration file for the [bundleCommand]. If provided, it will be passed over with a
5855
* `--config` flag to the bundle command.
5956
*/
60-
val bundleConfig: Property<String> = objects.property(String::class.java)
57+
val bundleConfig: RegularFileProperty = objects.fileProperty()
6158

6259
/**
6360
* The Bundle Asset name. This name will be used also for deriving other bundle outputs such as
@@ -69,69 +66,24 @@ abstract class ReactExtension @Inject constructor(project: Project) {
6966
objects.property(String::class.java).convention("index.android.bundle")
7067

7168
/**
72-
* Variant Name to File destination map that allows to specify where is the resource dir for a
73-
* specific variant. If a value is supplied, the plugin will copy the bundled resource for that
74-
* variant from `generated/res/react/<variant>` into the custom specified location. Default: {}
69+
* Toggles the .so Cleanup step. If enabled, we will clean up all the unnecessary files before the
70+
* bundle task. If disabled, the developers will have to manually cleanup the files. Default: true
7571
*/
76-
val resourcesDir: MapProperty<String, File> =
77-
objects.mapProperty(String::class.java, File::class.java).convention(emptyMap())
78-
79-
/**
80-
* Variant Name to File destination map that allows to specify where is the asset dir for a
81-
* specific variant. If a value is supplied, the plugin will copy the bundled JS for that variant
82-
* from `generated/assets/react/<variant>` into the custom specified location. Default: {}
83-
*/
84-
val jsBundleDir: MapProperty<String, File> =
85-
objects.mapProperty(String::class.java, File::class.java).convention(emptyMap())
86-
87-
/** ANT-style excludes for the bundle command. Default: ["android / **", "ios / **"] */
88-
val inputExcludes: ListProperty<String> =
89-
objects.listProperty(String::class.java).convention(listOf("android/**", "ios/**"))
90-
91-
/**
92-
* Toggles the VM Cleanup step. If enabled, before the bundle task we will clean up all the
93-
* unnecessary files. If disabled, the developers will have to manually cleanup the files.
94-
* Default: true
95-
*/
96-
val enableVmCleanup: Property<Boolean> = objects.property(Boolean::class.java).convention(true)
72+
val enableSoCleanup: Property<Boolean> = objects.property(Boolean::class.java).convention(true)
9773

9874
/** Extra args that will be passed to the [bundleCommand] Default: [] */
9975
val extraPackagerArgs: ListProperty<String> =
10076
objects.listProperty(String::class.java).convention(emptyList())
10177

10278
/**
103-
* Allows to disable dev mode for certain variants. That's useful if you have a production variant
104-
* (say `canary`) where you don't want dev mode to be enabled. Default: []
105-
*/
106-
val devDisabledInVariants: ListProperty<String> =
107-
objects.listProperty(String::class.java).convention(emptyList())
108-
109-
/**
110-
* Functional interface to disable dev mode only on specific [BaseVariant] Default: will check
111-
* [devDisabledInVariants] or return True for Release variants and False for Debug variants.
112-
*/
113-
var disableDevForVariant: (BaseVariant) -> Boolean = { variant ->
114-
variant.name in devDisabledInVariants.get() || variant.isRelease
115-
}
116-
117-
/**
118-
* Variant Name to Boolean map that allows to toggle the bundle command for a specific variant.
119-
* Default: {}
120-
*/
121-
// todo maybe lambda as for hermes?
122-
val bundleIn: MapProperty<String, Boolean> =
123-
objects.mapProperty(String::class.java, Boolean::class.java).convention(emptyMap())
124-
125-
/**
126-
* Functional interface to toggle the bundle command only on specific [BaseVariant] Default: will
127-
* check [bundleIn] or return True for Release variants and False for Debug variants.
79+
* Allows to specify the debuggable variants (by default just 'debug'). Variants in this list
80+
* will:
81+
* - Not be bundled (the bundle file will not be created and won't be copied over).
82+
* - Have the Hermes Debug flags set. That's useful if you have another variant (say `canary`)
83+
* where you want dev mode to be enabled. Default: ['debug']
12884
*/
129-
var bundleForVariant: (BaseVariant) -> Boolean = { variant ->
130-
if (bundleIn.getting(variant.name).isPresent) bundleIn.getting(variant.name).get()
131-
else if (bundleIn.getting(variant.buildType.name).isPresent)
132-
bundleIn.getting(variant.buildType.name).get()
133-
else variant.isRelease
134-
}
85+
val debuggableVariants: ListProperty<String> =
86+
objects.listProperty(String::class.java).convention(listOf("debug"))
13587

13688
/** Hermes Config */
13789

@@ -141,35 +93,18 @@ abstract class ReactExtension @Inject constructor(project: Project) {
14193
*/
14294
val hermesCommand: Property<String> = objects.property(String::class.java).convention("")
14395

144-
/** Toggle Hermes for the whole build. Default: false */
145-
val enableHermes: Property<Boolean> = objects.property(Boolean::class.java).convention(false)
146-
14796
/**
148-
* Functional interface to selectively enabled Hermes only on specific [BaseVariant] Default: will
149-
* return [enableHermes] for all the variants.
150-
*/
151-
var enableHermesForVariant: (BaseVariant) -> Boolean = { enableHermes.get() }
152-
153-
/**
154-
* Functional interface specify flags for Hermes on specific [BaseVariant] Default: will return
155-
* [hermesFlagsRelease] for Release variants and [hermesFlagsDebug] for Debug variants.
156-
*/
157-
var hermesFlagsForVariant: (BaseVariant) -> List<String> = { variant ->
158-
if (variant.isRelease) hermesFlagsRelease.get() else hermesFlagsDebug.get()
159-
}
160-
161-
/**
162-
* Functional interface to delete debug files only on specific [BaseVariant] Default: will return
163-
* True for Release variants and False for Debug variants.
97+
* Whether to enable Hermes only on certain variants. If specified as a non-empty list, hermesc
98+
* and the .so cleanup for Hermes will be executed only for variants in this list. An empty list
99+
* assumes you're either using Hermes for all variants or not (see [enableHermes]).
100+
*
101+
* Default: []
164102
*/
165-
var deleteDebugFilesForVariant: (BaseVariant) -> Boolean = { variant -> variant.isRelease }
166-
167-
/** Flags to pass to Hermes for Debug variants. Default: [] */
168-
val hermesFlagsDebug: ListProperty<String> =
103+
val enableHermesOnlyInVariants: ListProperty<String> =
169104
objects.listProperty(String::class.java).convention(emptyList())
170105

171-
/** Flags to pass to Hermes for Release variants. Default: ["-O", "-output-source-map"] */
172-
val hermesFlagsRelease: ListProperty<String> =
106+
/** Flags to pass to Hermesc. Default: ["-O", "-output-source-map"] */
107+
val hermesFlags: ListProperty<String> =
173108
objects.listProperty(String::class.java).convention(listOf("-O", "-output-source-map"))
174109

175110
/**

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package com.facebook.react
99

1010
import com.android.build.api.variant.AndroidComponentsExtension
11-
import com.android.build.gradle.AppExtension
1211
import com.android.build.gradle.internal.tasks.factory.dependsOn
1312
import com.facebook.react.tasks.BuildCodegenCLITask
1413
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
@@ -47,10 +46,8 @@ class ReactPlugin : Plugin<Project> {
4746
configureBuildConfigFields(project)
4847
configureDevPorts(project)
4948

50-
project.afterEvaluate {
51-
project.extensions.getByType(AppExtension::class.java).applicationVariants.all {
52-
project.configureReactTasks(variant = it, config = extension)
53-
}
49+
project.extensions.getByType(AndroidComponentsExtension::class.java).onVariants { variant ->
50+
project.configureReactTasks(variant = variant, config = extension)
5451
}
5552
configureCodegen(project, extension, isLibrary = false)
5653
}

0 commit comments

Comments
 (0)