Skip to content

Commit 56d8c23

Browse files
cortinicofacebook-github-bot
authored andcommitted
RNGP - Introduce enableCodegenInApps
Summary: This is part of a series of tasks to make the React Native Gradle Plugin (RNGP) variant-aware. Historically we used to enable the codegen only for library modules. This is not working well for apps like RN Tester which don't have library modules but are running the codegen. Therefore I'm introducing the `enableCodegenInApps` that is allowing apps to turn off the codegen if needed. Changelog: [Internal] [Changed] - RNGP - Introduce enableCodegenInApps Reviewed By: cipolleschi Differential Revision: D40633106 fbshipit-source-id: dc2ae9bbfe5b7940f9e9fe505ab2198a42c8a3f6
1 parent 41fec07 commit 56d8c23

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,11 @@ abstract class ReactExtension @Inject constructor(project: Project) {
221221
*/
222222
val codegenJavaPackageName: Property<String> =
223223
objects.property(String::class.java).convention("com.facebook.fbreact.specs")
224+
225+
/**
226+
* Toggles the Codegen for App modules. By default we enable the codegen only for library modules.
227+
* If you want to enable codegen in your app, you will have to set this to true. Default: false
228+
*/
229+
val enableCodegenInApps: Property<Boolean> =
230+
objects.property(Boolean::class.java).convention(false)
224231
}

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package com.facebook.react
99

1010
import com.android.build.api.variant.AndroidComponentsExtension
1111
import com.android.build.gradle.AppExtension
12-
import com.android.build.gradle.LibraryExtension
1312
import com.android.build.gradle.internal.tasks.factory.dependsOn
1413
import com.facebook.react.tasks.BuildCodegenCLITask
1514
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
@@ -21,7 +20,6 @@ import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
2120
import com.facebook.react.utils.findPackageJsonFile
2221
import java.io.File
2322
import kotlin.system.exitProcess
24-
import org.gradle.api.Action
2523
import org.gradle.api.Plugin
2624
import org.gradle.api.Project
2725
import org.gradle.api.Task
@@ -31,8 +29,25 @@ class ReactPlugin : Plugin<Project> {
3129
override fun apply(project: Project) {
3230
checkJvmVersion(project)
3331
val extension = project.extensions.create("react", ReactExtension::class.java, project)
34-
applyAppPlugin(project, extension)
35-
applyCodegenPlugin(project, extension)
32+
33+
// App Only Configuration
34+
project.pluginManager.withPlugin("com.android.application") {
35+
configureReactNativeNdk(project, extension)
36+
configureBuildConfigFields(project)
37+
configureDevPorts(project)
38+
39+
project.afterEvaluate {
40+
project.extensions.getByType(AppExtension::class.java).applicationVariants.all {
41+
project.configureReactTasks(variant = it, config = extension)
42+
}
43+
}
44+
configureCodegen(project, extension, isLibrary = false)
45+
}
46+
47+
// Library Only Configuration
48+
project.pluginManager.withPlugin("com.android.library") {
49+
configureCodegen(project, extension, isLibrary = true)
50+
}
3651
}
3752

3853
private fun checkJvmVersion(project: Project) {
@@ -54,30 +69,12 @@ class ReactPlugin : Plugin<Project> {
5469
}
5570
}
5671

57-
private fun applyAppPlugin(project: Project, config: ReactExtension) {
58-
project.pluginManager.withPlugin("com.android.application") {
59-
configureReactNativeNdk(project, config)
60-
configureBuildConfigFields(project)
61-
configureDevPorts(project)
62-
project.afterEvaluate {
63-
val isAndroidLibrary = project.plugins.hasPlugin("com.android.library")
64-
val variants =
65-
if (isAndroidLibrary) {
66-
project.extensions.getByType(LibraryExtension::class.java).libraryVariants
67-
} else {
68-
project.extensions.getByType(AppExtension::class.java).applicationVariants
69-
}
70-
variants.all { project.configureReactTasks(variant = it, config = config) }
71-
}
72-
}
73-
}
74-
7572
/**
7673
* A plugin to enable react-native-codegen in Gradle environment. See the Gradle API docs for more
7774
* information: https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
7875
*/
7976
@Suppress("UnstableApiUsage")
80-
private fun applyCodegenPlugin(project: Project, extension: ReactExtension) {
77+
private fun configureCodegen(project: Project, extension: ReactExtension, isLibrary: Boolean) {
8178
// First, we set up the output dir for the codegen.
8279
val generatedSrcDir = File(project.buildDir, "generated/source/codegen")
8380

@@ -86,6 +83,7 @@ class ReactPlugin : Plugin<Project> {
8683
it.codegenDir.set(extension.codegenDir)
8784
val bashWindowsHome = project.findProperty("REACT_WINDOWS_BASH") as String?
8885
it.bashWindowsHome.set(bashWindowsHome)
86+
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
8987
}
9088

9189
// We create the task to produce schema from JS files.
@@ -96,6 +94,7 @@ class ReactPlugin : Plugin<Project> {
9694
it.nodeExecutableAndArgs.set(extension.nodeExecutableAndArgs)
9795
it.codegenDir.set(extension.codegenDir)
9896
it.generatedSrcDir.set(generatedSrcDir)
97+
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
9998

10099
// We're reading the package.json at configuration time to properly feed
101100
// the `jsRootDir` @Input property of this task. Therefore, the
@@ -123,6 +122,7 @@ class ReactPlugin : Plugin<Project> {
123122
it.packageJsonFile.set(findPackageJsonFile(project, extension))
124123
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
125124
it.libraryName.set(extension.libraryName)
125+
it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
126126
}
127127

128128
// We update the android configuration to include the generated sources.
@@ -133,12 +133,8 @@ class ReactPlugin : Plugin<Project> {
133133
ext.sourceSets.getByName("main").java.srcDir(File(generatedSrcDir, "java"))
134134
}
135135

136-
// `preBuild` is one of the base tasks automatically registered by Gradle.
136+
// `preBuild` is one of the base tasks automatically registered by AGP.
137137
// This will invoke the codegen before compiling the entire project.
138-
val androidPluginHandler = Action { _: Plugin<*> ->
139-
project.tasks.named("preBuild", Task::class.java).dependsOn(generateCodegenArtifactsTask)
140-
}
141-
project.plugins.withId("com.android.application", androidPluginHandler)
142-
project.plugins.withId("com.android.library", androidPluginHandler)
138+
project.tasks.named("preBuild", Task::class.java).dependsOn(generateCodegenArtifactsTask)
143139
}
144140
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ react {
8888
// Codegen Configs
8989
reactNativeDir = rootDir
9090
codegenDir = new File(rootDir, "node_modules/react-native-codegen")
91+
enableCodegenInApps = true
9192
}
9293

9394
/**

0 commit comments

Comments
 (0)