硬件
我很抱歉,但是将开发站升级到SSD和数以吨计的RAM的影响可能比下面的分数加起来还要大.
工具版本
提高构建性能是开发团队的首要任务,所以确保使用最新的Gradle和Android Gradle Plugin.
配置文件
在任何适用的目录中创建一个名为gradle.properties
的文件:
/home/<username>/.gradle/
(Linux)
/Users/<username>/.gradle/
(Mac)
C:\Users\<username>\.gradle
(Windows)
附加:
# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
# TODO: disable daemon on CI, since builds should be clean and reliable on servers
org.gradle.daemon=true
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.krd1mm27v
org.gradle.jvmargs=-Xmx5120m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true
# Set to true or false to enable or disable the build cache.
# If this parameter is not set, the build cache is disabled by default.
# http://tools.android.com/tech-docs/build-cache
android.enableBuildCache=true
如果您将Gradle属性放置在projectRoot\gradle.properties
,则Gradle属性在本地有效;如果您将它们放置在user_home\.gradle\gradle.properties
,则Gradle属性全局有效.从Console或直接从IDEA运行Gradle任务时应用的属性:
IDE设置
可以从IDE设置GUI调整Gradle-IntelliJ集成.启用"离线工作"(判断下面yava的答案)将禁用每个"sync Gradle文件"上的真实网络请求.
本机多索引
apk构建最慢的步骤之一是将java字节码转换为单个dex文件.启用本机multidex(minSdk 21仅适用于调试构建)将有助于工具减少工作量(从下面的Aksel Willgert项中 Select 答案).
依赖项
与库子项目相比,更喜欢@aar
个依赖项.
在mavenCentral、jCenter上搜索aar包,或者使用jitpack.io从github构建任何库.如果您没有编辑依赖项库的源,那么不应该每次都使用项目源来构建依赖项库.
杀毒软件
考虑从防病毒扫描中排除项目和缓存文件.这显然是与安全性的权衡(不要在家里try !)但是如果您经常在分支之间切换,那么AntiVirus会在允许Gradle进程使用它之前重新扫描文件,这会减慢构建时间(特别是AndroidStudio与Gradle文件和索引任务的同步项目).测量构建时间,并在启用和不启用防病毒的情况下处理CPU,以查看它们是否相关.
分析生成
Gradle内置了对profiling projects的支持.不同的项目使用不同的插件和定制脚本组合.使用--profile
将有助于找到瓶颈.