2015-11-18 20:17:39 +00:00
|
|
|
import org.apache.tools.ant.taskdefs.condition.Os
|
|
|
|
|
2015-10-19 13:32:02 +00:00
|
|
|
def config = project.hasProperty("react") ? project.react : [];
|
|
|
|
|
2017-04-10 19:08:07 +00:00
|
|
|
def cliPath = config.cliPath ?: "node_modules/react-native/local-cli/cli.js"
|
2015-10-19 13:32:02 +00:00
|
|
|
def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
|
|
|
|
def entryFile = config.entryFile ?: "index.android.js"
|
2017-10-14 00:22:00 +00:00
|
|
|
def bundleCommand = config.bundleCommand ?: "bundle"
|
2018-03-05 18:53:00 +00:00
|
|
|
|
|
|
|
// because elvis operator
|
|
|
|
def elvisFile(thing) {
|
|
|
|
return thing ? file(thing) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
def reactRoot = elvisFile(config.root) ?: file("../../")
|
2015-10-19 13:32:02 +00:00
|
|
|
def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
|
2017-10-14 00:22:00 +00:00
|
|
|
def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;
|
2015-10-19 13:32:02 +00:00
|
|
|
|
2018-03-05 18:53:00 +00:00
|
|
|
void runBefore(String dependentTaskName, Task task) {
|
|
|
|
Task dependentTask = tasks.findByPath(dependentTaskName);
|
|
|
|
if (dependentTask != null) {
|
|
|
|
dependentTask.dependsOn task
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 17:32:46 +00:00
|
|
|
|
2018-03-26 05:00:11 +00:00
|
|
|
afterEvaluate {
|
2018-03-05 18:53:00 +00:00
|
|
|
// Grab all build types and product flavors
|
|
|
|
def buildTypes = android.buildTypes.collect { type -> type.name }
|
|
|
|
def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
|
|
|
|
|
|
|
|
// When no product flavors defined, use empty
|
|
|
|
if (!productFlavors) productFlavors.add('')
|
|
|
|
|
|
|
|
productFlavors.each { productFlavorName ->
|
|
|
|
buildTypes.each { buildTypeName ->
|
|
|
|
// Create variant and target names
|
|
|
|
def flavorNameCapitalized = "${productFlavorName.capitalize()}"
|
|
|
|
def buildNameCapitalized = "${buildTypeName.capitalize()}"
|
|
|
|
def targetName = "${flavorNameCapitalized}${buildNameCapitalized}"
|
|
|
|
def targetPath = productFlavorName ?
|
|
|
|
"${productFlavorName}/${buildTypeName}" :
|
|
|
|
"${buildTypeName}"
|
|
|
|
|
|
|
|
// React js bundle directories
|
|
|
|
def jsBundleDirConfigName = "jsBundleDir${targetName}"
|
|
|
|
def jsBundleDir = elvisFile(config."$jsBundleDirConfigName") ?:
|
|
|
|
file("$buildDir/intermediates/assets/${targetPath}")
|
|
|
|
|
|
|
|
def resourcesDirConfigName = "resourcesDir${targetName}"
|
|
|
|
def resourcesDir = elvisFile(config."${resourcesDirConfigName}") ?:
|
|
|
|
file("$buildDir/intermediates/res/merged/${targetPath}")
|
|
|
|
def jsBundleFile = file("$jsBundleDir/$bundleAssetName")
|
|
|
|
|
|
|
|
// Bundle task name for variant
|
|
|
|
def bundleJsAndAssetsTaskName = "bundle${targetName}JsAndAssets"
|
|
|
|
|
|
|
|
// Additional node and packager commandline arguments
|
|
|
|
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
|
|
|
|
def extraPackagerArgs = config.extraPackagerArgs ?: []
|
|
|
|
|
|
|
|
def currentBundleTask = tasks.create(
|
|
|
|
name: bundleJsAndAssetsTaskName,
|
|
|
|
type: Exec) {
|
2016-01-08 20:11:34 +00:00
|
|
|
group = "react"
|
2018-03-05 18:53:00 +00:00
|
|
|
description = "bundle JS and assets for ${targetName}."
|
|
|
|
|
|
|
|
// Create dirs if they are not there (e.g. the "clean" task just ran)
|
|
|
|
doFirst {
|
|
|
|
jsBundleDir.mkdirs()
|
|
|
|
resourcesDir.mkdirs()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up inputs and outputs so gradle can cache the result
|
|
|
|
inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
|
|
|
|
outputs.dir jsBundleDir
|
|
|
|
outputs.dir resourcesDir
|
|
|
|
|
|
|
|
// Set up the call to the react-native cli
|
|
|
|
workingDir reactRoot
|
|
|
|
|
|
|
|
// Set up dev mode
|
|
|
|
def devEnabled = !(config."devDisabledIn${targetName}"
|
|
|
|
|| targetName.toLowerCase().contains("release"))
|
|
|
|
|
|
|
|
def extraArgs = extraPackagerArgs;
|
|
|
|
|
|
|
|
if (bundleConfig) {
|
|
|
|
extraArgs = extraArgs.clone()
|
|
|
|
extraArgs.add("--config");
|
|
|
|
extraArgs.add(bundleConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
|
|
|
commandLine("cmd", "/c", *nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
|
|
|
|
"--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
|
|
|
|
} else {
|
|
|
|
commandLine(*nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
|
|
|
|
"--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled config."bundleIn${targetName}" ||
|
|
|
|
config."bundleIn${buildTypeName.capitalize()}" ?:
|
|
|
|
targetName.toLowerCase().contains("release")
|
2016-01-08 20:11:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:53:00 +00:00
|
|
|
// Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process
|
|
|
|
currentBundleTask.dependsOn("merge${targetName}Resources")
|
|
|
|
currentBundleTask.dependsOn("merge${targetName}Assets")
|
2016-01-08 20:11:34 +00:00
|
|
|
|
2018-03-05 18:53:00 +00:00
|
|
|
runBefore("process${flavorNameCapitalized}Armeabi-v7a${buildNameCapitalized}Resources", currentBundleTask)
|
|
|
|
runBefore("process${flavorNameCapitalized}X86${buildNameCapitalized}Resources", currentBundleTask)
|
|
|
|
runBefore("processUniversal${targetName}Resources", currentBundleTask)
|
|
|
|
runBefore("process${targetName}Resources", currentBundleTask)
|
|
|
|
runBefore("dataBindingProcessLayouts${targetName}", currentBundleTask)
|
2016-01-08 20:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-19 13:32:02 +00:00
|
|
|
}
|