Today I would like to talk about versions managing of Android dependency. Gradle allows us work with dependency in really simple way. However, finally you have many and many different dependencies.

You can find dependencies for some module inside build.gradle file.

apply plugin: 'com.android.application'
android {
    ...
}

...

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support:preference-v14:25.0.0'

    testCompile 'junit:junit:4.12'
}

Of course in a big project you will have much more dependencies. As you can see for first few dependencies we have the same version of dependency. And we can move it to ext block and use it to exchange version number.

apply plugin: 'com.android.application'
android {
    ...
}

...

ext {
    supportLibVersion = '25.0.0'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile "com.android.support:$supportLibVersion"
    compile "com.android.support:design:$supportLibVersion"
    compile "com.android.support:preference-v14:$supportLibVersion"

    testCompile 'junit:junit:4.12'
}

As you can see right now it looks better and we have one version for many dependencies from support library.

This solution is good, but we implemented it right now just for a module. Your project can contain more than one module. If you want to use it for all submodules you should move the ext block to root build.gradle file.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

...

ext {
    supportLibVersion = '25.0.0'
}

Finally, it will be available for each submodule.

GitHub Gist here.

Thank you for your time.
Have a nice day.

Share Share on Reddit0Share on VKTweet about this on TwitterShare on LinkedIn7Share on Google+1Share on Facebook0Flattr the authorEmail this to someoneShare on Tumblr0