How to reduce Gradle build time of the Android app?

Gradle is a very powerful build system. It handles the very complex process of building .dx files from java source code, merging all the resources and assets into the application package (.apk), and signing the application.

Photo by Matam Jaswanth on Unsplash

GRADLE BUILD CYCLE

Gradle has three distinct phases in the build cycle:

  • Initialization: In this stage of the build life cycle, gradle picks the project and decides what things to build.
  • Configuration: Here, Gradle will evaluate your build.gradle script, configure all the plugins, and evaluate the task graph.
  • Execution: In this phase Gradle actually runs all the tasks that were evaluated in the previous phase to get the work done and build the application.
Photo by hannah joshua on Unsplash

A few commands we can add to Gradle.properties files:

  • org.gradle.configureondemand=true – This command will tell Gradle to only build the projects that it really needs to build.
  • Use Daemon – org.gradle.daemon=true – Daemon keeps the instance of the Gradle up and running in the background even after your build finishes. This will remove the time required to initialize the Gradle and decrease your build timing significantly.
  • org.gradle.parallel=true – Allow Gradle to build your project in parallel. If you have multiple modules in your project, then by enabling this, gradle can run build operations for independent modules parallelly.
  • Ensure that dynamic dependency is not used. i.e. do not use implementation ‘com.android.support:appcompat-v7:27.0.+’. This command means Gradle will go online and check for the latest version every time it builds the app. Instead use fixed versions i.e ‘com.android.support:appcompat-v7:27.0.2'

The better we use Gradle, the more it can improve studio performance. The more we avoid understanding it, the worst we do to ourselves.

Founder at Inside Android | Youtuber | App Developer