Dependencies

Versions managing of Android dependencies

Alex Zhukovich 1 min read
Versions managing of Android dependencies
Table of Contents

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.

Thank you for your time.
Have a nice day.


Mobile development with Alex

A blog about Android development & testing, Best Practices, Tips and Tricks

Share

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Mobile development with Alex.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.