Maintain Library Versions Across Android App Modules

Adam McNeilly - May 8 '17 - - Dev Community

It is tough enough maintaining your app by updating the support library version numbers every time a new version is out, let alone factoring in any other third party libraries you may use. This is especially painful if you have multiple modules, as you have to update the version in each build.gradle file. Thankfully, we can make use of the project level gradle file to make this more maintainable.

Define Variables In Project Gradle File

In your root build.gradle file for the project, you can define some ExtraPropertyExtensions that will be reused for various modules. That would look something like this:

    allprojects {
        repositories {
            jcenter()
        }

        ext {
            //Android
            androidBuildToolsVersion = "25.0.2"
            androidMinSdkVersion = 16
            androidTargetSdkVersion = 25
            androidCompileSdkVersion = 25

            //Libraries
            supportLibraryVersion = "25.3.1"
            playServicesVersion = "10.2.1"
        }
    }
Enter fullscreen mode Exit fullscreen mode

Now we've got some reusable variables that we want to be consistent across our various modules.

Define Global Configuration Reference In Module

In the build.gradle file of each module, inside the "android" block, we'll need to define a reference to the global configuration we just created. We can do so with the following single line of code:

    android {
        def globalConfiguration = rootProject.extensions.getByName("ext")

        ...
    }
Enter fullscreen mode Exit fullscreen mode

With that, there are two ways you can use these properties. When trying to reference them individually, for properties such as build tools or compile sdk version, you can reference them using the property key:

android {
    ...
    compileSdkVersion globalConfiguration["androidCompileSdkVersion"]
    buildToolsVersion globalConfiguration["androidBuildToolsVersion"]
    ...
}
Enter fullscreen mode Exit fullscreen mode

If you want to include version numbers as part of your compile statements, you can simply use gradle's string interpolation:

    compile "com.android.support:appcompat-v7:${supportLibraryVersion}"
Enter fullscreen mode Exit fullscreen mode

That's it! That one simple little trick is all you need to have consistent library versions across modules. Some notes on further studying this:

  • A gist of all above code can be found here.
  • A project that goes even deeper on this topic and reusing compile statements (beyond just build numbers), can be found here. This came up as a result of a Facebook discussion I was a part of.
. . . . . . . . . . . . . . . . . . . . . . . .