Improve your Gradle build times by disabling plugins

A 5 steps guide to reduce your Gradle build times by only applying the plugins you need for each environment

Maxi Rosson
Apr 11 · 3 min read

Every time you apply a plugin to a project, you are increasing the Gradle configuration time.

Configuration Phase

During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed.

Given that lots of plugins are only needed by certain environments, applying them wisely is a good idea to speed up your build times.

For example, you don’t need to enable Releases Hub, Bye Bye Jetifier, SonarQube orJacoco plugins on development/production environments. Enabling them in your CI environment should be enough. TheFirebase Performance plugin could be enabled on CI and production but disabled on development.

This 5 steps guide, shows you how to easily enable/disable plugins for each environment, so you can improve your Gradle configuration time.

For more ideas about how to improve your Gradle build times, you can also read this article:

1. Configure buildSrc Gradle directory

The first step is to use the buildSrc Gradle directory to define your plugin flags.

“The directory buildSrc is treated as an included build. Upon discovery of the directory, Gradle automatically compiles and tests this code and puts it in the classpath of your build script.”

For more info about buildSrc, click here.

If you are not already using the buildSrc dir on your project, just create it and then add the following file on /buildSrc/build.gradle.kts

plugins { 
`kotlin-dsl`
}
repositories {
mavenCentral()
}

2. GradleProperty enum

The idea is to have one Gradle property for each plugin you want to enable/disable for certain environments. To simplify that, I propose to create a GradleProperty enum with a constant for each property. So you can define a default value and also use the Gradle property across your project without hardcoding its name.

The default values should be the ones you expect to use for the development environment.

For example:

/buildSrc/src/main/kotlin/com/dipien/gradle/GradleProperty.kt

3. Create a Property resolver

Add a PropertyResolver class with the logic to read the Gradle properties from the project, or as a system property if it is not found.

/buildSrc/src/main/kotlin/com/dipien/gradle/PropertyResolver.kt

4. Use the GradleProperty when applying your plugin

Now you can verify if the plugin is enabled before applying it to the project.

For example:

or using the Plugins DSL

5. Override the default flags

In your development environment, all the default values defined on the GradleProperty enum will be used (unless you have overridden them on your gradle.properties file).

For the other environments, you should override them. Enabling them on your CI scripts is a good idea. For example, if you want to run a task of the Bye Bye Jetifier plugin in your CI environment, you need to enable it. You can pass the BYE_BYE_JETIFIER_ENABLED parameter on command-line.

./gradlew canISayByeByeJetifier -PBYE_BYE_JETIFIER_ENABLED=true
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