improving Gradle build performance

简介: Gradle, the new build system for Android projects, has been designed for scalability and efficiency.
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


目录
相关文章
|
缓存 Java jenkins
Gradle build 慢?可能是你使用的姿势不对
Gradle build 慢?可能是你使用的姿势不对
|
3月前
|
Android开发
[ionic]解决Could not read build file capacitor/build.gradle as it does notexist.
[ionic]解决Could not read build file capacitor/build.gradle as it does notexist.
37 1
|
Android开发 开发工具 IDE
Android gradle问题解决: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat
1. 问题描述: Android Studio在运行模拟器某些机型或者真机某些机型的时候发生闪退。 错误如下: Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.
2365 0
|
IDE Java API
Gradle | 全局配置、Log开关控制、Build Variant、meta-data等配置
Gradle是一个先进的构建系统,也是一个允许通过插件创建自定义构建逻辑先进的构建工具。
467 0
|
Java API
Gradle学习基础(3):build脚本基础知识
Gradle学习基础(3):build脚本基础知识
Gradle学习基础(3):build脚本基础知识
|
Android开发
Migrate Project to Gradle? This project does not use the Gradle build system
Migrate Project to Gradle? This project does not use the Gradle build system
103 0
|
jenkins 持续交付 开发工具
|
Java API Kotlin
Gradle Writing Build Scripts
The Gradle build language Gradle 构建语言 Gradle 提供了一种领域特定语言(DSL)来描述构建,这种构建语言在 Groovy 和 Kotlin 都可以使用。 Groovy 构建脚本可以包含任何 Groovy 语言元素。 Kotlin 构建脚本可以包含任何 Kotlin 语言元素。 Gradle 假设每个构建脚本都使用 UTF-8进行编码。
134 0
|
API
Gradle Build Lifecycle
我们之前说过,Gradle 的核心是一种基于依赖性编程的语言。 在 Gradle 术语中,这意味着您可以定义任务和任务之间的依赖关系。 Gradle 保证这些任务按照其依赖项的顺序执行,并且每个任务只执行一次。 这些任务形成了一个有向无环图。 有一些构建工具可以在执行任务时建立这样的依赖关系图。 在执行任何任务之前,Gradle 构建完整的依赖关系图。 这位于 Gradle 的心脏地带,使许多事情成为可能,否则这些事情是不可能实现的。
124 0
gradle中的build script详解
gradle中的build script详解