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?
Asked
Active
Viewed 4,504 times
12
-
You can use subprojects https://proandroiddev.com/reducing-boilerplate-in-gradle-multi-module-projects-2ff2dde5bf95 – onmyway133 May 25 '19 at 17:55
3 Answers
16
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.

Gabriele Mariotti
- 320,139
- 94
- 887
- 841
4
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.

navid_gh
- 1,863
- 3
- 17
- 30
-
It helps me. Thanks! Just for others' reference: `root build.gradle` means the `build.gradle` of the Android Project (others are Module. – LiuWenbin_NO. Jan 05 '19 at 05:18
-
How will Gradle know when to sync and when not to in case you have added an extra dependency in the `ext` list – m'hd semps Apr 25 '22 at 15:22
-
Basically it is not automatic at least I didn't work on it to become automatic but it is simple refresh button in the intellij that you can do whenever you add one dependency – navid_gh Apr 26 '22 at 15:18
0
- First you have to add chart library project (module) in Android Studio (your current workspace) File -> Import Module
- To add library project (module) in build path, click File -> Project Structure
- On the left hand side click on app -> Dependencies tab -> green + button -> Module dependency Now select the library project you already added. For more details refer this link

Community
- 1
- 1

Naveen Kumar M
- 7,497
- 7
- 60
- 74
-
I know how to add a dependency to my project. I want to know if it's possible to (yes, I'm quoting myself) `add this somehow to the root build.gradle or do I have add to each project build.gradle the dependency?` – Chaoz Sep 13 '15 at 09:59
-