Android Studio提升Gradle的构建

用过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 dx compiler as a separate process or inside the Gradle daemon JVM.

javaMaxHeapSize

Specifies the -Xmx value when calling dx. Example value is "2048m".

jumboMode

Enable jumbo mode in dx (--force-jumbo).

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.

查阅相关文档后,build.gradle做出了如下配置:
    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一些其他配置,就可以有明显的速度提升:



这里,所有的配置设置就完成了,就这么简单,这样设置之后,发现构建速度真的是大幅度提升,我的项目变成了1-2分钟。
希望对需要的人有用,这里有个注意点,由于每个人的电脑配置不同,所以下面的配置设置建议先注释掉
//    dexOptions {
//        javaMaxHeapSize "4g"
//        incremental true
//        preDexLibraries = false
//        dexInProcess false
//        maxProcessCount = 4
//        threadCount = 4
//    }
这里建议试试在完成其他配置设置的条件下,通过观察注释掉跟不要注释这段配置来看看哪种情况构建速度快,我电脑。。。额,配置不想说了,所以我毅然注释掉了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值