Source: https://gradle.org/wp-content/uploads/2016/03/Build-Performance-Gradle.png

How to decrease your Gradle build time by 65%?

Why should you care about build time of your project?

APK anlyser output for Google I/O 16 app

Build without any modifications:

./gradlew android:assembleDebug --profile
  • As you can see it takes 2 minutes and 43 seconds to build the debug apk from the source on my machine. Keep this figure in your mind.

Gradle Build Cycle:

  1. Initialisation: In this stage of the build life cycle, gradle picks the project and decides what things to build.
  2. Configuration: Here, gradle will evaluate your build.gradle script, configure all the plugins and evaluate the task graph.
  3. Execution: In this phase gradle actually runs all the task those were evaluated in previous phase to get the work done and build the application.
./gradlew android:assembleDebug --dry-run --profile
Build timings for — dry-run

Configure on demand:

  • Android : That contains the source code related to the andorid application and
  • Server : This contains code related to backend server.
./gradlew android:assembleDebug --dry-run --profile --configure-on-demand
Build timimgs with — configure-on-demand

How you can enable configure-on-demand in android studio?

Enable — configure-on-demand in Android Studio

Enable daemon:

./gradlew android:assembleDebug --dry-run --profile --configure-on-demand --daemon
Build time with gradle daemon enabled

How you can enable daemon in android studio?

Always keep your gradle up-to-date:

gradle-wrapper.properties
./gradlew android:assembleDebug --dry-run --profile --configure-on-demand --daemon
Build timing with Gradle 3.3

Increase the heap size:

Multiple dx processes running on different VMs. (Source: https://www.youtube.com/watch?v=-SY5nkNVUn0)
Multiple dx processes running on single VM. (Source: https://www.youtube.com/watch?v=-SY5nkNVUn0)
org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
./gradlew android:assembleDebug --profile --configure-on-demand --daemon
Build timimg with larger heap size.

Modularise and Parallelise build:

Build timing with parallel build enabled

Other small things to remember:

  • Avoid heavy computations, like increasing the version code from git commits count or downloading some files from url while building the project. Gradle will spend time to perform those computations or network operations and that will afftect build timings.
  • Don’t use dynamic dependency like,
compile 'com.android.support:appcompat-v7:23.0.+'
compile 'com.android.support:appcompat-v7:23.0.2'

TL;DR:

Final gradle.properties file
https://paypal.me/kpatel2106?locale.x=en_GB

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Keval Patel

Keval Patel

7.9K Followers

www.kevalpatel2106.com | Android Developer | Machine learner | Gopher | Open Source Contributor