Configure Gradle in different files

Kevin Schweitzer
ProAndroidDev
Published in
3 min readNov 8, 2018

Many times when programming in Android, we need to add lots of dependencies to our project, and maybe that project also have lots of modules that are interconnected, so, as the project gets bigger, it’s important to keep the Gradle files organized.

The naive way to define a dependency in Gradle is for example:

dependencies {

implementation 'com.android.support:support-compat:28.0.0'

}

This is not bad for a very little project, but a better approach would be to keep the library version (in this case 28.0.0) in a variable to have a better management of dependencies. Gradle provides a way to do this by defining an extension to keep variables:

ext {
supportCompatVersion = '28.0.0'
}

dependencies {
implementation "com.android.support:support-compat:$supportCompatVersion"

}

Check that here we changed the simple quotation marks for double ones, and accessed the variable with $ symbol.

We can do more with ext, not only save the library version but also the entire dependency name:

ext {
supportCompatVersion = '28.0.0'

generalDependencies = [
supportCompat : "com.android.support:support-compat:$supportCompatVersion"
]
}

dependencies {

implementation generalDependencies.supportCompat

}

Here we defined a variable called “generalDependencies” that save a list of the dependencies (in JSON style) and using also the version variable as before.

At this point we have a pretty good organized Gradle, but it is all in the same Gradle file, and this is a problem if we have many modules and we need the same library for many modules, because we have to remember that this Gradle file is inside a single module. So, how we can manage to have a different file that keeps track of all the dependencies we have in the project?

My approach is to have a project folder called “buildsystem” and create a Gradle file called “dependencies.gradle” and save all the ext stuff inside that file:

dependencies.gradle

ext {
supportCompatVersion = '28.0.0'

generalDependencies = [
supportCompat : "com.android.support:support-compat:$supportCompatVersion"
]
}

By now we have access to that file across all our project and the way to achieve that is:

dependencies {

implementation rootProject.ext.generalDependencies.supportCompat

}

As we see here, inside rootProject we can access ext and the repective list of dependencies. But we have to tell Gradle that this file was created, and we do that by adding this line at the top of the project’s Gradle file:

build.gradle (project)

apply from: 'buildSystem/dependencies.gradle'

So that’s all. We have now a general file to manage all our dependencies and we can find and access them easily, giving us a better way to update our libraries.

This is my first post, hope you liked it!

Sign up to discover human stories that deepen your understanding of the world.

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Kevin Schweitzer

Ingeniero en Sistemas, Agente Inmobiliario y Emprendedor

Responses (4)

What are your thoughts?