improving Gradle build performance

Gradle, the new build system for Android projects, has been designed for scalability and efficiency. However, there are a few things you, as a developer, can do to significantly improve the performance of Gradle.

1. Incremental Builds. Gradle supports full incremental builds out of the box, which means that it only runs tasks that are not up to date and skips others. Gradle looks at the inputs and outputs of each task to see if any build artifacts have changed and only runs a task if necessary—this significantly increases build performance.

2. Gradle Daemon. [0] The Gradle daemon is a great way to speed up your builds, which is extremely useful when you have to build your application repeatedly. The basic idea is to fork a “daemon” process [1] which can be reused on subsequent builds, rather than launching a new JVM on every build.

The good news is that Android Studio always keeps a Gradle daemon around for you. However, if you build from the command line, you need to tell gradle that you want to use the Gradle daemon. You can use command line arguments [2] to interact with Gradle daemon, but the easiest way is to add a daemon property to your gradle.properties file:

org.gradle.daemon=true

It is also good practice to set this property in $HOME/.gradle/gradle.properties rather than putting it in project root. The reasoning behind this is that you want to avoid using the Gradle daemon on your build servers, where startup time is less important than memory consumption.

3. Parallel Project Execution. [3] Parallel execution can make a significant difference if you are building a very complex project with many sub-projects (for example, Android library projects). Gradle has an in-development Parallel Mode which enables parallel execution of sub-projects that are decoupled. “Decoupled” means that these projects do not access each other's project model. Cross project configurations are a good example of this. Any use of the allprojects {} and subprojects {} closures will result in the projects being coupled. Android Studio does not use Parallel Mode by default, but you can enable it in Preferences > Compiler > Gradle.

Again, if you are using the command line you need to explicitly tell Gradle that you want to use Parallel Mode. To enable it, add the parallel property to your gradle.properties file:

org.gradle.parallel=true

4. Disable pre-dexing on build servers. When running your build on a build server, you may always want to start with a clean build. But this means that you cannot take advantage of Gradle’s incremental nature. To improve incremental builds, the Android Gradle Plugin uses an optimization that pre-dexes all dependencies at first compilation. While this optimization makes subsequent builds faster it makes the first build slower. As this is not what you want, it makes sense to disable pre-dexing to improve performance by setting the preDexLibraries property of the Android Gradle Plugin  to false [4]:

project.android.dexOptions.preDexLibraries=false

For more details on how to disable pre-dexing only on your build server, refer to this tip from +Xavier Ducrohet  goo.gl/2G6hcj

With Android projects becoming increasingly complex and modular development practices becoming more popular, build performance is critical. Only a few seconds per build can make a big difference in productivity.

Do you have any other tips and tricks? Please let us know in the comments.

[0] Gradle Daemon - http://goo.gl/fVoeGC
[1] This process will automatically expire after 3 hours
[2] Gradle Command Line - http://goo.gl/UrDjOA
[3] Parallel Project Execution - http://goo.gl/QuAPoK
[4] Advanced Build Customization - http://goo.gl/3QiCil

#AndroidTools

时间: 2024-10-04 01:54:55

improving Gradle build performance的相关文章

执行gradle build一直报错

问题描述 执行gradle build一直报错 一直报错 multi-project-build:core:unspecified 跟项目配置文件: build.gradle subprojects { apply plugin: 'java' apply plugin: 'eclipse' repositories { mavenCentral() } } settings.gradle include 'app' include 'core' 子项目app的: apply plugin: '

eclipse+gradle build过程中出现此问题 求高手帮忙解决下

问题描述 eclipse+gradle build过程中出现此问题 求高手帮忙解决下 Execution failed for task ':PunkHoo:packageAllDebugClassesForMultiDex'. java.util.zip.ZipException: duplicate entry: android/support/v4/internal/view/ SupportMenu.class

eclipse环境下使用gradle build modle时卡住

问题描述 eclipse环境下使用gradle build modle时卡住 一直卡在这里,这是怎么回事啊 有知道的吗 解决方案 是不是下不下来,在浏览器上执行一下'http://services.gradle.org/distributions/gradle-2.5-bin.zip'试试 解决方案二: 使用浏览器是可以下载的

gradle build出错,不知道这是为什么

问题描述 gradle build出错,不知道这是为什么 谁能告知一下! 解决方案 http://tieba.baidu.com/p/3677955068 解决方案二: 为什么我看的像V4找不到- -. 解决方案三: 不好意思,手机太小,看不清错误. 解决方案四: gradle即使更新的 , 看看你是不是没有链接到它服务器 解决方案五: 执行命令出错 了. 找不到对应的文件.

jdk-Android Studio 一直处于 Gradle Build Running 状态

问题描述 Android Studio 一直处于 Gradle Build Running 状态 起因是系统提示出现渲染器问题: 新建工程后就一直处于 Gradle Build Running 状态: 已挂VPN...我的 Android Studio 还没有成功运行过,求大神指教是哪里配置有问题? JDK的版本是1.8.0.65,gradle2.4. 解决方案 你工程错误原因是未导入v7包和design包 快捷建ctrl+; 打开 关于gradle 在gradle/wrapper/gradle

gradle build的时候显示成功,但是出现-Xlint:unchecked重新编译,

问题描述 gradle build的时候显示成功,但是出现-Xlint:unchecked重新编译, 我是不是要删了cache再编译呢 解决方案 http://blog.csdn.net/gongweijiao/article/details/6990379 解决方案二: 你可以clean下工程然后在进行编译gradle

Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法_Android

错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Error: java.lang.RuntimeException: Crunch

Android Gradle Build Error:Some file crunching failed, see logs for details解决办法

Android Gradle Build Error:Some file crunching failed, see logs for details解决办法 错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed

Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法

错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Error: java.lang.RuntimeException: Crunch