Contents

Library versions of Android projects can become complicated to manage

Tips to manage them more efficiently

The number of Android libraries increases days by days, and it happens often to see Android projects using a lot of them.

As consequence, your build.gradle can become full of duplicated library versions, as below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61"
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.61"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0-rc1"
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0-rc1"
    implementation "com.squareup.retrofit2:retrofit:2.4.0"
    implementation "com.squareup.retrofit2:converter-moshi:2.4.0"
    implementation "com.squareup.retrofit2:converter-gson:2.4.0"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
    ...
}

Update a version of a library or easily check all used library versions can be quickly painful.

Improve things by defining versions in one place

A cleaner way to manage all these versions is to create a file named versions.gradle at the root of your Android Project and to define all versions into it by using the Extra Properties Extension provided by Gradle. A sample of versions.gradle could looks like to :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Define all versions in one place
ext {
    versions = [
            android_gradle_plugin              : '3.2.0-rc03',
            androidx_appcompat                 : '1.0.0-rc02',
            androidx_cardview                  : '1.0.0-rc02',
            androidx_core                      : '1.0.0-rc02',
            androidx_constraintlayout          : '2.0.0-alpha2',
            androidx_recyclerview              : '1.0.0-rc02',
            androidx_test                      : '1.1.0-alpha4',
            androidx_test_expresso             : '3.1.0-alpha4',
            glide                              : '4.8.0',
            junit                              : '4.12',
            kotlin                             : '1.2.61',
            kotlin_coroutines                  : '0.25.3',
            kotlin_coroutines_retrofit2_adapter: '1.0.0',
            lifecycle                          : '2.0.0-rc01',
            moshi                              : '1.6.0',
            okhttp                             : '3.11.0',
            persistence_cookie_jar             : 'v1.0.1',
            retrofit2                          : '2.4.0'
    ]
}

Now, it is easy to modify a library version or to see all used library versions by your project. The build.gradle file that define dependencies, now looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$versions.lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-extensions:$versions.lifecycle_version"
    implementation "com.squareup.retrofit2:retrofit:$versions.retrofit2"
    implementation "com.squareup.retrofit2:converter-moshi:$versions.retrofit2"
    implementation "com.squareup.retrofit2:converter-gson:$versions.retrofit2"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$versions.retrofit2"
    ...
}

!!! Do not forget to call the versions.gradle script by adding apply from: 'versions.gradle' into the buildscript block of your build.gradle project configuration file. Without this call, properties will not be accessible to your application.

The main adavantage to do it into the buildscript block is that you can also uses defined versions into this block. Thus, the dependency to the Android plugin version can aslo use them as below:

1
2
3
dependencies {
        classpath "com.android.tools.build:gradle:$versions.android_gradle_plugin"
}