How to manage Android Plugin for Gradle version in a team

Barry
2 min readMay 11, 2016

--

If we are part of a team, each member wants to work with a different Android Studio version. Some of us want to feel safety so prefer updates from Stable Channel, and others (early adopted) are praying for that update windows to appear and get the latest changes from Canary or Beta Channel.

As you may know, (almost) all Android Studio upgrades need you to upgrade Android Plugin for Gradle version in main build.gradle file.

buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:2.1.0"
}
}

For sure, you have your project in a version control system, let’s say, git. This means that each member team have to change the plugin version in build.gradle file to fit with their Android Studio version. Do they have to commit this change? - No. Will they forget to not commit this change? - this is gonna happen soon or later.

One solution to this situation is to create a local build gradle file and overwrite the plugin version only for you. Keep in mind that this new file has to be ignored by the repository.

Let’s going to see the changes you need to do in gradle configuration.

First of all, you need to create an extra property to store the stable plugin version of the project.

buildscript {
ext.buildGradlePluginVersion = '2.1.0'

dependencies {
classpath "com.android.tools.build:gradle:$project.buildGradlePluginVersion"
}
}

It is very important to create it into buildscript{} task because this task contains the build script classpath for gradle itself.

Then, we can create the local gradle file (add it to .gitignore), for example build.local.gradle, and overwrite the property created with the version we want to upgrade to.

buildscript {
ext.buildGradlePluginVersion = '2.1.0-beta1'
}

Finally, we have to check for the new file, and apply it if exists. I prefer to check if the file exists to maintain the repository ready to clone and run.

buildscript {
ext.buildGradlePluginVersion = '2.1.0'
if (file('build.local.gradle').exists())
apply from: 'build.local.gradle'

dependencies {
classpath "com.android.tools.build:gradle:$project.buildGradlePluginVersion"
}
}

And that’s it. You can change your local plugin version in build.local.gradle when you need, and the version in build.gradle could be modified when some stable version was upgrade.

Of course, this solution could be applied to any problem related with main and local gradle configuration.

--

--