Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Add dependency to all modules

I have read tons of posts like this one, but they only tell you that you should not add dependencies to the project root build.gradle, which I know.
My situation is: I have a lot of modules that all need the same library. All of them, so I need to configure all of them to have the same library. Is it possible to add this somehow to the root build.gradle or I have add to each project build.gradle the dependency?

like image 798
Chaoz Avatar asked Sep 13 '15 09:09

Chaoz


2 Answers

You can do something like this.
It doesn't mean to add a dependency for all modules, but in this way you can centralize a dependency.

In top-level build.gradle

    ext {
        //Version
        supportLibrary = '23.0.1'

        //Support Libraries dependencies
        supportDependencies = [
                appCompat     : "com.android.support:appcompat-v7:${supportLibrary}",
                design        : "com.android.support:design:${supportLibrary}",
        ]
    }

In each module add to build.gradle:

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}

In this way, when you have to update the library, you can simply change only the top-level file.

like image 71
Gabriele Mariotti Avatar answered Nov 18 '22 22:11

Gabriele Mariotti


I know the question is a bit old but I started using this which I think would be cleaner if you do not care about what libraries being injected into your module.

root build.gradle

ext {
    commonDependencies = [
            // Layout Libraries
            constraintLayoutApi     : "androidx.constraintlayout:constraintlayout:$constraintlayout_version",
            materialApi             : "com.google.android.material:material:$material_version",
            flexboxApi              : "com.google.android:flexbox:$flexbox_version",

            // Image library (Picasso is alternative)
            glideImpl               : "com.github.bumptech.glide:glide:$glide_version",
            glideCompilerAnno       : "com.github.bumptech.glide:compiler:$glide_version",

            // Network library (Volley is alternative)
            retrofitImpl            : "com.squareup.retrofit2:retrofit:$retrofit_version",
            retrofitJacksonImpl     : "com.squareup.retrofit2:converter-jackson:$retrofit_version",
            retrofitScalarsImpl     : "com.squareup.retrofit2:converter-scalars:$retrofit_version",
            retrofitJava8Impl       : "com.squareup.retrofit2:converter-java8:$retrofit_version",

            // Dependency Injection
            daggerImpl              : "com.google.dagger:dagger:$dagger_version",
            daggerCompilerAnno      : "com.google.dagger:dagger-compiler:$dagger_version",
            // we add this so we can use the android support libraries
            daggerAndroidSupportImpl: "com.google.dagger:dagger-android-support:$dagger_version",
            daggerProcessorAnno     : "com.google.dagger:dagger-android-processor:$dagger_version"
    ]
}

and in app build.gradle:

dependencies {
    api "androidx.appcompat:appcompat:$app_compact_version"
    api "com.google.android.gms:play-services-auth:$play_service_version"

    commonDependencies.each { key, value ->
        if (key.endsWith('Anno')) {
            annotationProcessor value
        } else if (key.endsWith('Impl')) {
            implementation value
        } else if (key.endsWith('Api')) {
            api value
        }
    }
}

As you can see I am checking for API, Implementation, and Annotation by the suffix of the key in the root gradle file.

like image 4
navid_gh Avatar answered Nov 18 '22 21:11

navid_gh