10 ideas to improve your Gradle build times [Part I]

Reduce your Gradle build times on android & non-android projects with these great ideas

Maxi Rosson
Apr 7 · 5 min read

Is your Gradle build taking too long time? These 10 ideas can help you to optimize your Gradle build on android and non-android projects.

You can read Part II of this article here:

1. Use the latest dependencies versions

If you stay up to date with your dependencies, you will get all the enhancements and optimizations they have to offer. Using the latest Gradle, Android Gradle plugin & Kotlin Gradle plugin versions can help a lot. But other dependencies upgrades, mainly Gradle plugins, are also important.

Keeping your Gradle project dependencies up to date can be a huge manual task if you have a big project. It’s a bit tedious for developers to manually check for dependencies upgrades, causing a lot of waste of time. The Releases Hub Gradle Plugin helps developers to keep their dependencies up to date.

You can read more about the plugin, in this post:

2. Enable Gradle Build scans

A build scan is a shareable record of a build that provides insights into what happened and why.

Enable Gradle Build scans for all your builds (local & ci builds), so you can analyze the reports and find potential issues on your build.

3. Avoid mixing Java & Kotlin code

Mixing Java & Kotlin code in the same module is bad for build times.

Here you can find a Uber post that talks about the topic.

You should pick any of both languages (I would vote for Kotlin) and migrate 100% of your code.

4. Stop generating the BuildConfig on your Android modules [Android only]

Generating the BuildConfig class for Android library modules is a bad idea. It causes build time penalties and also stimulates bad code patterns.

You can read this post for more details:

5. Deactivate Android Gradle Plugin unused features [Android only]

Android Gradle plugin 4.0.0 introduced a way to control which build features you want to enable and disable, such as View Binding and Data Binding. When new features are added, they will be disabled, by default.

You can enable only the features you want, and it helps you optimize the build performance for your project.

You can specify the default setting for these features across all modules in a project by including one or more of the following in your project’s gradle.properties file, as shown below.

# Flag to enable AIDL compilation. Determines whether to generate binder classes for your AIDL files
android.defaults.buildfeatures.aidl=true
# Flag to enable/disable generation of the BuildConfig class
android.defaults.buildfeatures.buildconfig=true
# Flag to enable Compose feature
android.defaults.buildfeatures.compose=false
# Flag to enable Data Binding
# Note that the dataBinding.enabled property is now deprecated
android.defaults.buildfeatures.dataBinding=false
# Flag to enable/disable import of Prefab dependencies from AARs
android.defaults.buildfeatures.prefab=false
# Flag to enable RenderScript compilation
android.defaults.buildfeatures.renderscript=true
# Flag to enable Resource Values generation. Determines whether to support injecting custom variables into the module’s R class.
android.defaults.buildfeatures.resvalues=true
# Flag to enable Shader AOT compilation
android.defaults.buildfeatures.shaders=true
# Flag to enable View Binding
# Note that the dataBinding.enabled property is now deprecated
android.defaults.buildfeatures.viewBinding=false

You can use the buildFeatures block in the module-level build.gradle file to override these project-wide default settings.

android {
// The default value for each feature is shown below.
buildFeatures {
aidl = true
buildConfig = true
compose = false
dataBinding = false
prefab = false
renderscript = true
resvalues = true
shaders = true
viewBinding = false
}
}

6. Remove unused code

If you have unused code in your project, each developer is wasting time understanding and compiling it.

Removing your unused code should be one of your main technical priorities.

7. Remove unused resources [Android only]

Similar to having unused code, if you have unused resources in your project, each developer computer is wasting time processing them.

This incredible plugin can help you automatically remove unused android resources from your project.

You can configure your CI tool to regularly send a pull request with all the removed resources.

8. Apply Gradle Doctor plugin

The Gradle Doctor plugin analyzes your Gradle configuration and automatically detects issues on build time.

If you apply it, you can get some useful warnings related to build speed problems.

9. Disable Jetifier [Android only]

Having Jetifier enabled on your project (android.enableJetifier = true on your gradle.properties) impacts on the build speed, because the Android Gradle Plugin has to transform all the support-library-dependent artifacts to AndroidX. So, disabling jetifier is a good idea for your android project if you want to speed up your build speed. But doing that is not always a trivial task. In this article, you will find a list of 6 steps to follow, so you can fully migrate your app to AndroidX and then safely disable Jetifier.

10. Apply Android cache fix Gradle plugin [Android only]

Some Android plugin versions have issues with Gradle’s build cache feature. When applied to an Android project, the Android cache fix Gradle plugin applies workarounds for these issues based on the Android plugin and Gradle versions.

You can read Part II of this article here:

Follow us for more productivity tools & ideas for Android, Kotlin & Gradle projects.

Dipien

Boost your Productivity

Maxi Rosson

Written by

Developer Productivity Engineer | Android | Productivity tools & ideas for Android, Kotlin & Gradle developers on medium.dipien.com

Dipien

Dipien

Productivity tools & ideas for Android, Kotlin & Gradle developers.

Maxi Rosson

Written by

Developer Productivity Engineer | Android | Productivity tools & ideas for Android, Kotlin & Gradle developers on medium.dipien.com

Dipien

Dipien

Productivity tools & ideas for Android, Kotlin & Gradle developers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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