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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android中DNS解析
  2. Android中线程同步之Mutex与Condtion的用
  3. eclipse无法编译android故障排除
  4. Android(4)---Android 控件布局常用属性
  5. Android Studio V3.12环境下TV开发教程(一
  6. Android EditText/TextView使用Spannable
  7. Android多媒体开发 android mediaplayer
  8. android往文件中保存和读取数据
  9. Android加载Html的方法
  10. 11.22 java,android,classLoder,插件式开