加速Gradle构建在Android Studio 3.2.1
你是否曾感觉在Android Studio中需要等待几分钟才能完成构建?我也是。这很烦人。
幸运的是,您可以使用一些方法来改善这一点。Android使用Gradle进行构建。最新版本4.6比以前的版本有了巨大的性能提升(详情请参阅发布说明)。
步骤1:更新Gradle版本
一个更简单的方法是:打开模块设置(你的项目)>项目结构
更新
更改到Gradle版本:4.6
而且
Android插件版本:3.2.1
从https://services.gradle.org/distributions/gradle-4.6-all.zip下载Gradle Release分发版
然后复制到Gradle文件夹:
最后一步是在设置> Gradle中添加您的发行版
不要忘记单击Apply以保存更改。
第二步:为项目启用离线模式、Gradle守护进程和并行构建
离线模式告诉Gradle忽略update-to-date检查。Gradle每次都要求依赖项,有了这个选项,它就只使用机器上已经存在的依赖项。
从android studio settings转到Gradle,点击离线工作框。
从android studio Setting进入Compiler,在命令行框中添加“- offline”,然后单击“并行编译独立模块”。
下一步是为您的项目启用Gradle守护进程和并行构建。并行构建将使您的项目具有多个模块(Gradle中的多项目构建)并行构建,这将使大型或模块化项目更快地构建。
这些设置可以通过修改一个名为gradle的文件来启用。属性在Gradle脚本目录(即、~ / .gradle / gradle.properties)。其中一些选项(例如并行编译模块)可以从Android Studio中获得,默认情况下也可以在那里启用,但将它们放在gradle中。属性文件将在从终端构建时启用它们,并确保您的同事将使用相同的设置。但如果你在一个团队中工作,有时你不能承诺这些事情。
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit org.gradle.parallel=true
# When set to true the Gradle daemon is used to run the build. For local developer builds this is our favorite property.
# The developer environment is optimized for speed and feedback so we nearly always run Gradle jobs with the daemon.
org.gradle.daemon=true
Using the daemon will make your builds startup faster as it won’t have to start up the entire Gradle application every time. The Gradle Daemon is not enabled by default, but it’s recommend always enabling it for developers’ machines (but leaving it disabled for continuous integration servers). FAQ about this mode could be found here https://docs.gradle.org/current/userguide/gradle_daemon.html.
The parallel builds setting could be unsafe for some projects. The requirement is that all your modules must be decoupled or your build could fail (see http://gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects for details).
步骤3:启用增量设计和调整内存设置
您可以通过启用增量索引来加快构建的速度。在模块的构建文件中:
添加这个选项到你的android块:
dexOptions {
incremental true
}
在dexOptions块中,你还可以为dex进程指定堆大小,例如:
dexOptions {
incremental true
javaMaxHeapSize "12g"
}
这里的“12g”是12GB的内存。更多相关信息可以在这里找到google.github.io/android-gradle-dsl/current/
你也可以在设置文件中配置Gradle参数,例如,如果你有一个大项目,增加最大堆大小:
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
查看所有参数列表:https://docs.gradle.org/current/userguide/userguide_single.html#sec:gradle_configuration_properties详细信息。
Step 4: Disable Antivirus
Consider to exclude project and cache files from antivirus scanning. This is obviously a trade off with security. But if you switch between branches a lot, then antivirus will rescan files before allowing gradle process to use it, which slows build time (in particular Android Studio sync project with gradle files and indexing tasks). Measure build time and process CPU with and without antivirus enabled to see if it is related.
I hope this helps. Leave a comment if you have any question or some other tips for improving the build performance.
有用的链接