用过Android Studio搞大型项目的同学应该知道,构建一次的时间很长,我的项目快的需要4分钟左右,慢的时候要5-8分钟,不能忍...
查了Android Developers官方,才知道了Android Plugin For Gradle 2.1 有一个能提升构建速度的特性。这面摘自官方:
Added support for dexing-in-process which performs dexing within the build process rather than in a separate, external VM processes. This not only makes incremental builds faster, but also speeds up full builds. The feature is enabled by default for projects that have set the Gradle daemon's maximum heap size to at least 2048 MB. You can do this by including the following in your project'sgradle.properties
file:
org.gradle.jvmargs = -Xmx2048m
If you have defined a value for javaMaxHeapSize
in your module-level build.gradle
file, you need to set org.gradle.jvmargs
to the value of javaMaxHeapSize
+ 1024 MB. For example, if you have set javaMaxHeapSize
to "2048m", you need to add the following to your project's gradle.properties
file:
org.gradle.jvmargs = -Xmx3072m
To disable dexing-in-process, add the following code to your module-level build.gradle
file:
android { ... dexOptions { dexInProcess false } }于是顺便查了dexOptions的其他属性:
Properties
Property | Description |
dexInProcess | Whether to run the |
javaMaxHeapSize | Specifies the |
jumboMode | Enable jumbo mode in dx ( |
maxProcessCount | The maximum number of concurrent processes that can be used to dex. Defaults to 4. |
preDexLibraries | Whether to pre-dex libraries. This can improve incremental builds, but clean builds may be slower. |
threadCount | Number of threads to use when running dx. Defaults to 4. |
dexOptions {
javaMaxHeapSize "4g"
incremental true
preDexLibraries = false
dexInProcess false
maxProcessCount = 4
threadCount = 4
}
gradle.properties主要是将几个地方的注释打开,分别是:
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
上面这么配置之后发现构建的速度没提升多少,网上查了很多资料,感觉挺散,又或许我没配置对,于是翻阅了一些资料之后,发现如果勾选setting一些其他配置,就可以有明显的速度提升:
// dexOptions {
// javaMaxHeapSize "4g"
// incremental true
// preDexLibraries = false
// dexInProcess false
// maxProcessCount = 4
// threadCount = 4
// }
这里建议试试在完成其他配置设置的条件下,通过观察注释掉跟不要注释这段配置来看看哪种情况构建速度快,我电脑。。。额,配置不想说了,所以我毅然注释掉了。