@@ -9,7 +9,6 @@ package com.facebook.react
9
9
10
10
import com.android.build.api.variant.AndroidComponentsExtension
11
11
import com.android.build.gradle.AppExtension
12
- import com.android.build.gradle.LibraryExtension
13
12
import com.android.build.gradle.internal.tasks.factory.dependsOn
14
13
import com.facebook.react.tasks.BuildCodegenCLITask
15
14
import com.facebook.react.tasks.GenerateCodegenArtifactsTask
@@ -21,7 +20,6 @@ import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
21
20
import com.facebook.react.utils.findPackageJsonFile
22
21
import java.io.File
23
22
import kotlin.system.exitProcess
24
- import org.gradle.api.Action
25
23
import org.gradle.api.Plugin
26
24
import org.gradle.api.Project
27
25
import org.gradle.api.Task
@@ -31,8 +29,25 @@ class ReactPlugin : Plugin<Project> {
31
29
override fun apply (project : Project ) {
32
30
checkJvmVersion(project)
33
31
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
+ }
36
51
}
37
52
38
53
private fun checkJvmVersion (project : Project ) {
@@ -54,30 +69,12 @@ class ReactPlugin : Plugin<Project> {
54
69
}
55
70
}
56
71
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
-
75
72
/* *
76
73
* A plugin to enable react-native-codegen in Gradle environment. See the Gradle API docs for more
77
74
* information: https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
78
75
*/
79
76
@Suppress(" UnstableApiUsage" )
80
- private fun applyCodegenPlugin (project : Project , extension : ReactExtension ) {
77
+ private fun configureCodegen (project : Project , extension : ReactExtension , isLibrary : Boolean ) {
81
78
// First, we set up the output dir for the codegen.
82
79
val generatedSrcDir = File (project.buildDir, " generated/source/codegen" )
83
80
@@ -86,6 +83,7 @@ class ReactPlugin : Plugin<Project> {
86
83
it.codegenDir.set(extension.codegenDir)
87
84
val bashWindowsHome = project.findProperty(" REACT_WINDOWS_BASH" ) as String?
88
85
it.bashWindowsHome.set(bashWindowsHome)
86
+ it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
89
87
}
90
88
91
89
// We create the task to produce schema from JS files.
@@ -96,6 +94,7 @@ class ReactPlugin : Plugin<Project> {
96
94
it.nodeExecutableAndArgs.set(extension.nodeExecutableAndArgs)
97
95
it.codegenDir.set(extension.codegenDir)
98
96
it.generatedSrcDir.set(generatedSrcDir)
97
+ it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
99
98
100
99
// We're reading the package.json at configuration time to properly feed
101
100
// the `jsRootDir` @Input property of this task. Therefore, the
@@ -123,6 +122,7 @@ class ReactPlugin : Plugin<Project> {
123
122
it.packageJsonFile.set(findPackageJsonFile(project, extension))
124
123
it.codegenJavaPackageName.set(extension.codegenJavaPackageName)
125
124
it.libraryName.set(extension.libraryName)
125
+ it.onlyIf { isLibrary || extension.enableCodegenInApps.get() }
126
126
}
127
127
128
128
// We update the android configuration to include the generated sources.
@@ -133,12 +133,8 @@ class ReactPlugin : Plugin<Project> {
133
133
ext.sourceSets.getByName(" main" ).java.srcDir(File (generatedSrcDir, " java" ))
134
134
}
135
135
136
- // `preBuild` is one of the base tasks automatically registered by Gradle .
136
+ // `preBuild` is one of the base tasks automatically registered by AGP .
137
137
// 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)
143
139
}
144
140
}
0 commit comments