Organizing your Android Studio Gradle dependencies like a Pro.

Why do we need to organize our Gradle dependencies?

As we love to use dependencies to our Android studio projects but when you are having a high no of dependencies you will suffer to manage dependencies versions. Your Gradle file seems like a Bloated file.

Normal Approach

The normal approach to organize is to make variables of dependencies versions and use them in Gradle.

This is very simpler way to manage dependencies but you had managed dependencies version not dependencies.

Best way to organize dependencies

So we are going to make separate dependencies.gradle file to make our Gradle file clean and simple. In this Gradle file we will make variables of dependencies version and dependencies itself. Let’s take an example of Retrofit and Dagger 2 dependencies implementation.

Steps to Organize your Gradle Dependencies

  • Create dependencies.gradle file.
  • Make a constant version’s variables.

dependencies.gradle

ext {
    final def vJwRetrofitRxJavaAdapter = '1.0.0'
    final def vOkHttp = '3.11.0'
    final def vRetrofit = '2.3.0'
}
  • Now we have to add dependencies in this Gradle file. Make a Linked Hashmap for better Understanding and readability.
ext {

    final def vJwRetrofitRxJavaAdapter = '1.0.0'
    final def vOkHttp = '3.11.0'
    final def vRetrofit = '2.3.0'
    final def vDagger = '2.21'
    
    networkDependencies = [
            retrofit               : "com.squareup.retrofit2:converter-gson:$vRetrofit",
            retrofitRxJavaAdapter  : "com.squareup.retrofit2:adapter-rxjava2:$vRetrofit",
            retrofitGsonConverter  : "com.squareup.retrofit2:converter-gson:$vRetrofit",
            jwRetrofitRxJavaAdapter: "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$vJwRetrofitRxJavaAdapter",
            okHttp3                : "com.squareup.okhttp3:okhttp:$vOkHttp",
            okHttp3Logging         : "com.squareup.okhttp3:logging-interceptor:$vOkHttp"
    ]

    dagger = [
            dagger                 : "com.google.dagger:dagger:$vDagger",
            daggerCompiler         : "com.google.dagger:dagger-compiler:$vDagger",
    ]
    
}
  • To use dependencies in your APP’s Gradle first import this dependencies.gradle file and use its variables.

APP Gradle File

apply plugin: 'com.android.application'

apply from: 'dependencies.gradle'
///
////
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    
    implementation networkDependencies.values()
    
    implementation dagger.values()
}
  • Now we are done. Our app Gradle file looks very clear and understandable. but we can organize do a little bit more by doing this.

dependencies.gradle

ext {

    final def vJwRetrofitRxJavaAdapter = '1.0.0'
    final def vOkHttp = '3.11.0'
    final def vRetrofit = '2.3.0'
    final def vDagger = '2.21'

    networkDependencies = [
            retrofit               : "com.squareup.retrofit2:converter-gson:$vRetrofit",
            retrofitRxJavaAdapter  : "com.squareup.retrofit2:adapter-rxjava2:$vRetrofit",
            retrofitGsonConverter  : "com.squareup.retrofit2:converter-gson:$vRetrofit",
            jwRetrofitRxJavaAdapter: "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$vJwRetrofitRxJavaAdapter",
            okHttp3                : "com.squareup.okhttp3:okhttp:$vOkHttp",
            okHttp3Logging         : "com.squareup.okhttp3:logging-interceptor:$vOkHttp"
    ]

    dagger = [
            dagger                 : "com.google.dagger:dagger:$vDagger",
            daggerCompiler         : "com.google.dagger:dagger-compiler:$vDagger",
    ]

    allImplementation = [
            networkDependencies  : networkDependencies.values(),
            dagger               : dagger.values()
    ]

}
  • And in APP Gradle file we can only implement a single variable
apply plugin: 'com.android.application'

apply from: 'dependencies.gradle'
///
////
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar']
    implementation allImplementation.values()
}

You can have multiple dependencies Gradle files for testing, productions, multiple modules, etc. I hope this blog post will help you a little bit in organizing your dependencies Gradle files.

Here is the Demo Project link for better understanding of Organizing Your Gradle file.

https://github.com/happysingh23828/Androchef-Movies-Android-App-TMDB

Thank you for reading this blog post. If you have any suggestion, feedback please comment below.