Global dependency resolution in android gradle

Avinash Kumar
Bobble Engineering
Published in
2 min readSep 1, 2022

--

We might have a case where we are stuck in a situation where we have a fairly large project in android and many submodules inside it, managing the dependencies is quite a tedious task for the whole project.

Gradle

To solve this problem, we came across the solution i.e, global dependency resolution in which there is a single gradle file that contains all the dependencies which are used throughout the whole project so that we can standardize it via the single gradle file.

Now, the first step is to make a dependencies.gradle file at the project level in your android project.

Inside the dependencies.gradle file creates an ext{} block where all the dependencies will go as shown below.

ext {
// Contains the core android tools version
toolsVersion = [
android : [
gradleVersion : "7.2.1",
kotlinVersion : "1.6.21",
minSdk : 23, // Android 6.0
compileSdk : 32,
targetSdk : 32, // Android 12L
buildToolVersion: "30.0.3",
ndkVersion : "22.0.7026061",
],
junit : "4.13.2",
espressoCore: "3.4.0",
extJunit : "1.1.3",
multidex : "2.0.1",
annotation : "1.4.0"
]
supportLibVersions = [
appcompat : "1.5.0",
emojiAppcompat : "1.1.0",
material : "1.6.1", // Material UI
recyclerview : "1.2.1",
constraintlayout: "2.1.4",
cardview : "1.0.0",
emoji : "1.1.0" // androidx.emoji
]
libVersions = [
firebaseBom : "25.12.0",
facebookSdk : "12.3.0",
playServicesGcm : "17.0.0",
playServicesAuth : "19.0.0",
glide : "4.13.2",
retrofit : "2.9.0",
moshi : "1.13.0",
]
}

You can categorize as many as you want at your convenience.

Now, in Project-level build.gradle you need to add one line (i.e, apply from: ‘dependencies.gradle’) to activate this gradle file for global access.

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
...
id 'com.android.application' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
...
}

apply from: 'dependencies.gradle'

task clean(type: Delete) {
delete rootProject.buildDir
}

Now, after syncing the project. Our, new dependencies.gradle file is ready to work.

Earlier we use the dependencies shown below:

implementation "com.google.android.material:material:1.6.1"

Now we access dependencies like this, in the dependencies block we can access the dependencies via kotlin string template as shown below:

implementation "com.google.android.material:material:${supportLibVersions.material}"
implementation "androidx.multidex:multidex:${toolsVersion.multidex}"
implementation "androidx.browser:browser:${libVersions.browser}"
implementation "androidx.cardview:cardview:${supportLibVersions.cardview}"
implementation "androidx.appcompat:appcompat:${supportLibVersions.appcompat}"
implementation "com.squareup.retrofit2:retrofit:${libVersions.retrofit}"
implementation "com.github.bumptech.glide:glide:${libVersions.glide}"

NOTE: For Groovy string interpolation you must use double quotes (“).

--

--