The Startup
Published in

The Startup

ANDROID

Gradle Dependencies: A Mesh🤯

Manage your Gradle dependencies in a smart way!!

Cover Image

With every increasing dependency, it becomes hard for us to manage the Gradle file. In this blog, we will try to manage our dependencies by moving them to another file and grouping similar dependencies.

How am I planning to do this?

We often include dependencies for a specific purpose like some dependencies for networking, some for performing background tasks, etc. So my idea here is why shouldn't we group them according to that?

For example:

I am using these dependencies for cameraX.

implementation "androidx.camera:camera-camera2:1.0.0-rc01"
implementation "androidx.camera:camera-lifecycle:1.0.0-rc01"
implementation "androidx.camera:camera-view:1.0.0-alpha20"

Wouldn’t it be nice if instead of these dependencies, I only have to write this:

implementation cameraX

Isn’t it be cool? 😉

It’s not a big deal for most of you, it’s just a matter of 2 extra lines.

And you are right actually, for 2 lines you should not care. But think it for a big project where multiple dependencies are used in the multiple modules. So, when it comes to managing dependencies will you update every dependency at every place?

And this might be how your Gradle file will lookalike: 😫

android {
....

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

testImplementation 'junit:junit:4.+'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation platform('com.google.firebase:firebase-bom:26.2.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.firebaseui:firebase-ui-auth:6.4.0'
implementation 'com.google.firebase:firebase-storage-ktx'
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-inappmessaging-display-ktx'

implementation "com.squareup.retrofit2:retrofit:2.6.2"
implementation "com.squareup.retrofit2:converter-gson:2.6.2"
implementation "com.squareup.retrofit2:converter-scalars:2.3.0"
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.11.0"

}

Now think what if it changes to this:

android {
....

dependencies {

implementation base
androidTestImplementation testing
implementation firebaseLibraries
implementation retrofit
implementation coroutines
implementation utils
kapt kapts

}

Let’s learn how can we do this?

In this approach, we will make use of the extension function of the Gradle file. Like in some dependencies, you have might have seen something like $kotlin_version.

In the above expression, the $ sign is used to access a variable into the current text from the outside. Android Studio generally keeps the kotlin_version as an extension in the project’s Gradle file.

Similarly, we will use extensions to access our dependencies which we will place in our project’s Gradle file.

ext {

kotlin_version = "1.4.21"

firebaseLibraries = [
'com.google.firebase:firebase-analytics-ktx',
'com.firebaseui:firebase-ui-auth:6.4.0',
'com.google.firebase:firebase-storage-ktx',
'com.google.firebase:firebase-crashlytics-ktx',
'com.google.firebase:firebase-messaging-ktx',
'com.google.firebase:firebase-inappmessaging-display-ktx'
]

retrofit = [
"com.squareup.retrofit2:retrofit:2.6.2",
"com.squareup.retrofit2:converter-gson:2.6.2",
"com.squareup.retrofit2:converter-scalars:2.3.0",
"com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"
]

coroutines = [
"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version",
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5",
"org.jetbrains.kotlin:kotlin-reflect:$kotlin_version",
]

utils = [
"com.github.bumptech.glide:glide:4.11.0"
]

kapts = [
"com.github.bumptech.glide:compiler:4.11.0"
]

base = [
"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version",
'androidx.core:core-ktx:1.3.2',
'androidx.appcompat:appcompat:1.2.0',
'com.google.android.material:material:1.2.1',
'androidx.constraintlayout:constraintlayout:2.0.4'
]

testing = [
'androidx.test.ext:junit:1.1.2',
'androidx.test.espresso:espresso-core:3.3.0'
]
}

We had arranged all our dependencies in the groups and you can name a group anything you like, which suits the purpose of grouping.

Let’s call them in the app’s Gradle file

dependencies {

implementation base
androidTestImplementation testing
implementation firebaseLibraries
implementation retrofit
implementation coroutines
implementation utils
kapt kapts

}

While grouping, we have separated the glide’s kapt dependency from the implementation dependency because we only group those dependencies who have same prefix keyword like implementation, kapt, annotationProcessor, etc.

And since we can access project Gradle level extensions from any module’s Gradle file. So, we can also use the same dependencies across multiple modules Gradle files.

Important Note:

With this approach, we will not get any update warning on the dependencies as we get when we put them directly in the app’s Gradle file. You will have to manually update them.

Facing some errors while implementing? Check out the video now!

YouTube Video

That is all for this blog.

Hit the 👏🏼 icon, if this piece had helped you in some way.

Any suggestions? Comment them below.

Happy Coding!!

--

--

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