Android Studio - add dependency to all modules

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

+8
source share
3 answers

You can do something like this.
This does not mean adding a dependency for all modules, but in this way you can centralize the dependency.

At the top level of 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 build.gradle :

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

Thus, when you need to update the library, you can just change only the top-level file.

+11
source
  • First you need to add the library library project (module) to Android Studio (current workspace) File -> Import module
  • To add a library project (module) to the assembly path, click File → Project Structure
  • On the left side, click the application → the Dependencies tab → green + button → module dependency Now select the library project that you have already added. See link for more details.
0
source

I know this question is a bit outdated, but I started using it, which I think will be cleaner if you don't care which libraries are introduced 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 the build.gradle application:

 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 the API, implementation, and annotation by key suffix in the Gradle root file.

0
source

All Articles