Speed `react-native run-android` up (more)

React Native's Gradle script does not bundle the JS bundle in the Debug
configuration. Copy that source code (and adapt it) into our sdk Gradle
script.
This commit is contained in:
Lyubo Marinov 2017-06-20 12:19:18 -05:00
parent 2fa7e777d6
commit d117989b55
1 changed files with 91 additions and 67 deletions

View File

@ -1,5 +1,7 @@
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
def config = project.hasProperty('react') ? project.react : [];
android { android {
compileSdkVersion rootProject.ext.compileSdkVersion compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion buildToolsVersion rootProject.ext.buildToolsVersion
@ -43,38 +45,57 @@ void runBefore(String dependentTaskName, Task task) {
} }
gradle.projectsEvaluated { gradle.projectsEvaluated {
android.buildTypes.all { buildType -> // Grab all build types and product flavors
def buildNameCapitalized = "${buildType.name.capitalize()}" def buildTypes = android.buildTypes.collect { type -> type.name }
def bundlePath = "${buildDir}/intermediates/bundles/${buildType.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}"
// Bundle JavaScript and React resources (like react-native/react.gradle)
//
// React js bundle directories
def jsBundleDir = file("$buildDir/intermediates/assets/${targetPath}")
def resourcesDir = file("$buildDir/intermediates/res/merged/${targetPath}")
def jsBundleFile = file("${jsBundleDir}/index.android.bundle")
// Create dirs if they are not there (e.g. the "clean" task just ran)
jsBundleDir.mkdirs()
resourcesDir.mkdirs()
// Bundle fonts in react-native-vector-icons. // Bundle fonts in react-native-vector-icons.
// //
def currentFontTask = tasks.create(name: "${buildType.name}CopyFonts", type: Copy) { def currentFontTask = tasks.create(
name: "${buildTypeName}CopyFonts",
type: Copy) {
from("${projectDir}/../../fonts/jitsi.ttf") from("${projectDir}/../../fonts/jitsi.ttf")
from("${projectDir}/../../node_modules/react-native-vector-icons/Fonts/") from("${projectDir}/../../node_modules/react-native-vector-icons/Fonts/")
into("${bundlePath}/assets/fonts") into("${jsBundleDir}/fonts")
} }
currentFontTask.dependsOn("merge${buildNameCapitalized}Resources") currentFontTask.dependsOn("merge${targetName}Resources")
currentFontTask.dependsOn("merge${buildNameCapitalized}Assets") currentFontTask.dependsOn("merge${targetName}Assets")
runBefore("processArmeabi-v7a${buildNameCapitalized}Resources", currentFontTask) runBefore("process${flavorNameCapitalized}Armeabi-v7a${buildNameCapitalized}Resources", currentFontTask)
runBefore("processX86${buildNameCapitalized}Resources", currentFontTask) runBefore("process${flavorNameCapitalized}X86${buildNameCapitalized}Resources", currentFontTask)
runBefore("processUniversal${buildNameCapitalized}Resources", currentFontTask) runBefore("processUniversal${targetName}Resources", currentFontTask)
runBefore("process${buildNameCapitalized}Resources", currentFontTask) runBefore("process${targetName}Resources", currentFontTask)
// Bundle JavaScript and React resources. // Bundle task name for variant
// (adapted from react-native/react.gradle) def bundleJsAndAssetsTaskName = "bundle${targetName}JsAndAssets"
//
// React JS bundle directories
def jsBundleDir = file("${bundlePath}/assets")
def resourcesDir = file("${bundlePath}/res/merged")
def jsBundleFile = file("${jsBundleDir}/index.android.bundle")
// Bundle task name for variant.
def bundleJsAndAssetsTaskName = "bundle${buildNameCapitalized}JsAndAssets"
def currentBundleTask = tasks.create( def currentBundleTask = tasks.create(
name: bundleJsAndAssetsTaskName, name: bundleJsAndAssetsTaskName,
@ -90,28 +111,31 @@ gradle.projectsEvaluated {
workingDir reactRoot workingDir reactRoot
// Create JS bundle // Create JS bundle
def devEnabled = !buildNameCapitalized.toLowerCase().contains("release") def devEnabled = !targetName.toLowerCase().contains('release')
commandLine( commandLine(
"node", 'node',
"node_modules/react-native/local-cli/cli.js", 'node_modules/react-native/local-cli/cli.js',
"bundle", 'bundle',
"--assets-dest", resourcesDir, '--assets-dest', resourcesDir,
"--bundle-output", jsBundleFile, '--bundle-output', jsBundleFile,
"--dev", "${devEnabled}", '--dev', "${devEnabled}",
"--entry-file", "index.android.js", '--entry-file', 'index.android.js',
"--platform", "android", '--platform', 'android',
"--reset-cache") '--reset-cache')
// TODO: disable task in Debug mode? enabled config."bundleIn${targetName}" ||
config."bundleIn${buildNameCapitalized}" ?:
targetName.toLowerCase().contains('release')
} }
// Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process // Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process
currentBundleTask.dependsOn("merge${buildNameCapitalized}Resources") currentBundleTask.dependsOn("merge${targetName}Resources")
currentBundleTask.dependsOn("merge${buildNameCapitalized}Assets") currentBundleTask.dependsOn("merge${targetName}Assets")
runBefore("processArmeabi-v7a${buildNameCapitalized}Resources", currentBundleTask) runBefore("process${flavorNameCapitalized}Armeabi-v7a${buildNameCapitalized}Resources", currentBundleTask)
runBefore("processX86${buildNameCapitalized}Resources", currentBundleTask) runBefore("process${flavorNameCapitalized}X86${buildNameCapitalized}Resources", currentBundleTask)
runBefore("processUniversal${buildNameCapitalized}Resources", currentBundleTask) runBefore("processUniversal${targetName}Resources", currentBundleTask)
runBefore("process${buildNameCapitalized}Resources", currentBundleTask) runBefore("process${targetName}Resources", currentBundleTask)
}
} }
} }