Here’s how to test new versions of the Android Gradle Plugin while shipping your app with a stable version. You can use this pattern to test any new version of a Gradle plugin or a library. This is a great way to allow developers to use new build performance features and IDE improvements in the Android Gradle plugin while simultaneously shipping your app with a stable version of the Android Gradle Plugin.

First we can define a boolean in our Gradle build script.

def useAgpRC = (project.properties['useAgpRC'] ?: 'false').toBoolean()

This sets the property disabled by default. Use true instead to enable it by default.

You can enable the property by running a build with -PuseAgpRC=true

You can also add useAgpRC=true to your gradle.properties .

Individual developers on your team can opt in by adding useAgpRC=true to ~/.gradle/gradle.properties .

This special Gradle Properties file affects all Gradle projects on your machine but has a lower precendence than the gradle.properties in the project.

When enabled, we switch to use the Android Gradle Plugin 3.5 Release Candidate.

if (useAgpRC) {

classpath 'com.android.tools.build:gradle:3.5.0-rc02'

} else {

classpath 'com.android.tools.build:gradle:3.4.2'

}

This is what our buildscript dependency block looks like putting all that together.

buildscript { dependencies { def useAgpRC = (project.properties['useAgpRC'] ?: 'false').toBoolean() // To enable AGP 3.5 add `useAgpRC=true` to your

// `~/.gradle/gradle.properties`. if (useAgpRC) {

println('Using AGP 3.5 RC')

classpath 'com.android.tools.build:gradle:3.5.0-rc02'

} else {

println('Using AGP 3.4 Stable')

classpath 'com.android.tools.build:gradle:3.4.2'

}

}

If you are using the Gradle Remote Build Cache and seeding it on CI machines. Make sure to seed the cache with -PuseAgpRC=true and -PuseAgpRC=false .

If using Build Scans, you can also tag your build with

buildScan {

value "AGP RC", useAgpRC.toString()

}

Was that cool? Fund my chocolate addiction.

I’m also available for Gradle help.