Reduce Android app build times by enabling Gradle configuration cache

Sagar Das
2 min readMar 8, 2023

--

Photo by Xavi Cabrera on Unsplash

Any seasoned Android app developer working on a multi modular project knows that over a course of time the build time increases drastically. The build times are exceptionally slower on intel based CPUs.

There is however a configuration cache setting in Gradle which you can enable in order to reduce the build times of your project significantly.

Configuration phase

In Gradle, the configuration phase is the one in which the build script is evaluated and the build configuration is set up. During this phase, Gradle reads the build script, applies any plugins, and determines which tasks need to be executed, what dependencies are required, and how those dependencies will be resolved. It is a crucial part of the Gradle build process because it determines how the build will be executed.

It’s important to note that the configuration phase is separate from the execution phase, which means that changes made to the build script during the execution phase will not be applied until the next build.

Configuration cache

The configuration cache is a feature that allows Gradle to cache the results of the configuration phase of a build. This can significantly speed up build times, especially for Android projects with many modules and dependencies.It can be used to speed up the build process by caching the results of the configuration phase for each module. This means that when you make changes to your code, Gradle can skip the configuration phase for modules that have not changed, which can save a significant amount of time.

Enable Configuration cache

To enable the Gradle configuration cache in an Android project, you need to add the following code to your gradle.properties file:

org.gradle.unsafe.configuration-cache=true

Sync up your project and build once. Running this for the first time, the configuration phase executes, calculating the task graph.

Go ahead and build the project second time. And that reuses the cached configuration!

If you encounter build errors with messages like :

Configuration cache state could not be cached.
Maximum number of configuration cache problems has been reached.

Then add the following two lines in gradle.properties file :

org.gradle.unsafe.configuration-cache-problems=warn
org.gradle.unsafe.configuration-cache.max-problems=2

By default, Gradle will fail the build if any configuration cache problems are encountered. It can be useful to temporarily turn problems into warnings and allow Gradle to build successfully until you improve your plugin or build logic to support the configuration cache.
When configuration cache problems are turned into warnings, Gradle will fail the build if 512 problems are found by default.

This maximum problem limit can be changed by specifying an allowed maximum number of problems on the command line.

--

--

Sagar Das

Staff Android Engineer. Google Developer Expert in Android, Lives in Boston.