UNPKG

5.88 kBPlain TextView Raw
1import org.apache.tools.ant.taskdefs.condition.Os
2
3def config = project.hasProperty("react") ? project.react : [];
4
5def cliPath = config.cliPath ?: "node_modules/react-native/local-cli/cli.js"
6def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
7def entryFile = config.entryFile ?: "index.android.js"
8def bundleCommand = config.bundleCommand ?: "bundle"
9def reactRoot = file(config.root ?: "../../")
10def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
11def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;
12
13
14afterEvaluate {
15 android.applicationVariants.all { def variant ->
16 // Create variant and target names
17 def targetName = variant.name.capitalize()
18 def targetPath = variant.dirName
19
20 // React js bundle directories
21 def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
22 def resourcesDir = file("$buildDir/generated/res/react/${targetPath}")
23
24 def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
25
26 // Additional node and packager commandline arguments
27 def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
28 def extraPackagerArgs = config.extraPackagerArgs ?: []
29
30 def currentBundleTask = tasks.create(
31 name: "bundle${targetName}JsAndAssets",
32 type: Exec) {
33 group = "react"
34 description = "bundle JS and assets for ${targetName}."
35
36 // Create dirs if they are not there (e.g. the "clean" task just ran)
37 doFirst {
38 jsBundleDir.deleteDir()
39 jsBundleDir.mkdirs()
40 resourcesDir.deleteDir()
41 resourcesDir.mkdirs()
42 }
43
44 // Set up inputs and outputs so gradle can cache the result
45 inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
46 outputs.dir jsBundleDir
47 outputs.dir resourcesDir
48
49 // Set up the call to the react-native cli
50 workingDir reactRoot
51
52 // Set up dev mode
53 def devEnabled = !(config."devDisabledIn${targetName}"
54 || targetName.toLowerCase().contains("release"))
55
56 def extraArgs = extraPackagerArgs;
57
58 if (bundleConfig) {
59 extraArgs = extraArgs.clone()
60 extraArgs.add("--config");
61 extraArgs.add(bundleConfig);
62 }
63
64 if (Os.isFamily(Os.FAMILY_WINDOWS)) {
65 commandLine("cmd", "/c", *nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
66 "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
67 } else {
68 commandLine(*nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
69 "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
70 }
71
72 enabled config."bundleIn${targetName}" ||
73 config."bundleIn${variant.buildType.name.capitalize()}" ?:
74 targetName.toLowerCase().contains("release")
75 }
76
77 // Expose a minimal interface on the application variant and the task itself:
78 variant.ext.bundleJsAndAssets = currentBundleTask
79 currentBundleTask.ext.generatedResFolders = files(resourcesDir).builtBy(currentBundleTask)
80 currentBundleTask.ext.generatedAssetsFolders = files(jsBundleDir).builtBy(currentBundleTask)
81
82 // registerGeneratedResFolders for Android plugin 3.x
83 if (variant.respondsTo("registerGeneratedResFolders")) {
84 variant.registerGeneratedResFolders(currentBundleTask.generatedResFolders)
85 } else {
86 variant.registerResGeneratingTask(currentBundleTask)
87 }
88 variant.mergeResources.dependsOn(currentBundleTask)
89
90 // packageApplication for Android plugin 3.x
91 def packageTask = variant.hasProperty("packageApplication")
92 ? variant.packageApplication
93 : tasks.findByName("package${targetName}")
94
95 def resourcesDirConfigValue = config."resourcesDir${targetName}"
96 if (resourcesDirConfigValue) {
97 def currentCopyResTask = tasks.create(
98 name: "copy${targetName}BundledResources",
99 type: Copy) {
100 group = "react"
101 description = "copy bundled resources into custom location for ${targetName}."
102
103 from resourcesDir
104 into file(resourcesDirConfigValue)
105
106 dependsOn(currentBundleTask)
107
108 enabled currentBundleTask.enabled
109 }
110
111 packageTask.dependsOn(currentCopyResTask)
112 }
113
114 def currentAssetsCopyTask = tasks.create(
115 name: "copy${targetName}BundledJs",
116 type: Copy) {
117 group = "react"
118 description = "copy bundled JS into ${targetName}."
119
120 if (config."jsBundleDir${targetName}") {
121 from jsBundleDir
122 into file(config."jsBundleDir${targetName}")
123 } else {
124 into ("$buildDir/intermediates")
125 into ("assets/${targetPath}") {
126 from jsBundleDir
127 }
128
129 // Workaround for Android Gradle Plugin 3.2+ new asset directory
130 into ("merged_assets/${targetPath}/merge${targetName}Assets/out") {
131 from jsBundleDir
132 }
133 }
134
135 // mergeAssets must run first, as it clears the intermediates directory
136 dependsOn(variant.mergeAssets)
137
138 enabled currentBundleTask.enabled
139 }
140
141 packageTask.dependsOn(currentAssetsCopyTask)
142 }
143}