AndroidPub
Published in

AndroidPub

Android DTT #17 — Make Your Dependencies Consistent Between Modules

One For All, All For One

Google provides a lot of things as a library that we can use so we don’t have to reinvent the wheel. This is the common way to add those dependencies in the build.gradle:

compile 'com.android.support:design:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'

While there’s nothing wrong with this approach, it can become a hassle when there’s an update to the library. We have to edit the version for all of them. There’s also a warning in Android Studio if we are using multiple different versions of support library, since mixing version can lead to runtime crashes. So we have to make sure that every library coming from support library group has the same version.

Creating a Variable

In your app’s build.gradle you can save the version in a variable. Then use that variable in the compile command.

android { ... }ext {
supportLibVersion = '25.3.1'
playServiceLibVersion = '10.2.1'
}
dependencies {
compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:appcompat-v7:$supportLibVersion" compile "com.android.support:cardview-v7:$supportLibVersion" compile "com.google.android.gms:play-services-analytics:$playServiceLibVersion" compile "com.google.android.gms:play-services-maps:$playServiceLibVersion" compile "com.google.android.gms:play-services-auth:$playServiceLibVersion"}

So now we can make sure that every library under the same group will have the same version. And also when we want to update the version, we only need to modify it in one place.

Variable Across Different Modules

As a good Android developer, you might separate your code into multiple modules. Each of that modules might also need those support or play service libraries. How to make sure they all have the same version?

We just need to move our variable into the top-level build.gradle:

allprojects {...}

ext {
supportLibVersion = '25.3.1'
playServiceLibVersion = '10.2.1'
}

Note: You can also have a variable to store the minSdkVersion, targetSdkVersion etc. Then use that variable in all build.gradle inside each module.

And change how we reference the variable into:

compile "com.google.android.gms:play-services-analytics:${rootProject.ext.playServiceLibVersion}"compile "com.google.android.gms:play-services-maps:${rootProject.ext.playServiceLibVersion}"compile "com.google.android.gms:play-services-auth:${rootProject.ext.playServiceLibVersion}"

Actually, adding rootProject is optional. It can work without it. It’s just to make sure that we’re using the one defined in the top-level build.gradle instead of the one in the current build.gradle (if any).

You can also add the library name to the top-level build.gradle if you want to:

ext {
supportLibVersion = '25.3.1'
playServiceLibVersion = '10.2.1'


supportLib = [
appCompat: "com.android.support:appcompat-v7:${supportLibVersion}",
design: "com.android.support:design:${supportLibVersion}",
cardview: "com.android.support:cardview-v7:${supportLibVersion}"
]

playServiceLib = [
analytics: "com.google.android.gms:play-services-analytics:${playServiceLibVersion}",
maps: "com.google.android.gms:play-services-maps:${playServiceLibVersion}",
auth: "com.google.android.gms:play-services-auth:${playServiceLibVersion}"

]
}

Then in the other build.gradle you can just write it like this:

compile rootProject.ext.supportLib.design
compile rootProject.ext.supportLib.appCompat
compile rootProject.ext.supportLib.cardview

But going this way, you won’t receive that yellow warning notification when there’s an update to the library.

Now your dependencies are managed well. You will save a couple of minutes in the future when you’re adding or updating those libraries.

If you like this, hit that heart button and recommend it to your friends.

ADTT (Android Development Tips and Tricks) is a 31 series of blog posts that I’m trying to finish in throughout May. Click here for index.

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store