如今大部分混合应用使用了React Naitve,在Debug模式下随着工程不断的叠加庞大,加上自己工程没有作组件化,使得每次工程编译时间大大增长。java
首先咱们切到工程的主Gradle文件,能够发现主Gradle引入了React的编译脚本以下所示node
project.ext.react = [ root : "../..", bundleInDebug: true, ] apply from: "../../node_modules/react-native/react.gradle" 复制代码
bundleInDebug默认为false,可是大部分工程首页通常都会有引用RN的部分页面,若是这里不设置为true的话,进入首页会引发首页黑屏。因此通常这边的值都会设置为true。而这个也是致使工程编译很慢的罪魁祸首。那有没有什么办法能够保证,Android在Debug模式下便可以把JsBundle打进App应用内,并且在平时开发调试中页会很快的编译的完成呢? 答案是必定能够的,咱们接着去查看主Gradle引用的react.gradle脚本里的内容react
import org.apache.tools.ant.taskdefs.condition.Os def config = project.hasProperty("react") ? project.react : []; def cliPath = config.cliPath ?: "node_modules/react-native/local-cli/cli.js" def bundleAssetName = config.bundleAssetName ?: "index.android.bundle" def entryFile = config.entryFile ?: "index.android.js" def bundleCommand = config.bundleCommand ?: "bundle" // because elvis operator def elvisFile(thing) { return thing ? file(thing) : null; } def reactRoot = elvisFile(config.root) ?: file("../../") def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"] def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ; void runBefore(String dependentTaskName, Task task) { Task dependentTask = tasks.findByPath(dependentTaskName); if (dependentTask != null) { dependentTask.dependsOn task } } gradle.projectsEvaluated { // 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) { group = "react" 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") } // Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process currentBundleTask.dependsOn("merge${targetName}Resources") currentBundleTask.dependsOn("merge${targetName}Assets") 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) } } } 复制代码
阅读以上gradle脚本能够发现,主要执行打包JsBundle的就是下面的这段脚本代码android
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) } 复制代码
而这段脚本是在被动态建立的Task任务里执行的ios
def currentBundleTask = tasks.create( name: bundleJsAndAssetsTaskName, type: Exec) { group = "react" 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() } ...... 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 isEnableBuildJsBundle(targetName) && (config."bundleIn${targetName}" || config."bundleIn${variant.buildType.name.capitalize()}" ?: targetName.toLowerCase().contains("release")) } 复制代码
那么咱们也就找到了优化的点,咱们能够执行这个任务以前,打断这个Task的执行,不去调用JsBundle的脚本,从而也就节省了JSBundle的扫描编译时间,而这一部分的时间是至关可观的,至少有2Min!!!apache
(1)拷贝react.gradle到Android 工程主模块内,保持和主Gradle文件同级目录,以下图所示。 react-native
project.ext.react = [ root : "../..", bundleInDebug: true, ] apply from: "react.gradle" 复制代码
(3)新增isEnableBuildJsBundle方法api
/** * 注释:是否开启编译JsBundle任务 * 时间:2019/4/18 0018 10:18 * 做者:郭翰林 * @return */ boolean isEnableBuildJsBundle(String targetName) { if (targetName.toLowerCase().contains("release")) { return true } File jsBundle = file("$buildDir/intermediates/assets/debug/index.android.bundle") if (!jsBundle.exists()) { return true } else { println("【跳过编译JsBundle】JsBundle已存在,无需再次编译") return false } } 复制代码
(4)改写currentBundleTask()markdown
def currentBundleTask = tasks.create( name: "bundle${targetName}JsAndAssets", type: Exec) { group = "react" description = "bundle JS and assets for ${targetName}." ..................... 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 isEnableBuildJsBundle(targetName) && (config."bundleIn${targetName}" || config."bundleIn${variant.buildType.name.capitalize()}" ?: targetName.toLowerCase().contains("release")) } 复制代码
最后看一下效果,Android工程增量编译的时间,基本都在1Min之内,能够用很快来形容!!! app