How to share dependencies between Android modules

I have an Android application (application) and an Android library module (library). Both applications and libraries contain the same dependencies:

dependencies { compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.squareup.retrofit:retrofit:1.9.0' compile 'io.reactivex:rxjava:1.0.13' compile 'io.reactivex:rxandroid:0.25.0' } 

However, when I try to add this block to the build.gradle project, it complains that it does not know to "compile" DSL.

EDIT: I ask for the blocking of this block of dependencies in PROJECT build.gradle to avoid repetition in each build.gradle module.

+6
source share
5 answers

You can define common gradle dependencies in the library module, and if there is a library in the application module as a dependency, you will not need to specify all two times. Taking this further, you can create a β€œcommon” module that requires common gradle dependencies, and a common module is required for the application module and the library.

0
source

According to Gradle, the plugin version 3.0.0 is a more convenient way to do this. We can control whether each dependency is available only for the current module or for the current module and any modules that depend on it. This will allow us to easily share dependencies between modules within the project.

Here we used to declare dependencies:

  • compile 'example.dependency: 1.0.0'

Here are the new configurations that should replace compilation:

  • implementation . example.dependency: 1.0.0 '-> this dependency is used only in this module
  • api 'example.dependency: 1.0.0' β†’ this dependency will also be available in any assemblies that depend on this module.

Here's how to do this with the architecture that you talked about in the question. Assuming we have a module called 'library' that is consumed by the application module, we can use the api configuration to declare that the dependency should be shared with any module that depends on it.

build.gradle library module

 dependencies { // dependencies marked 'implementation' will only be available to the current module implementation 'com.squareup.okhttp:okhttp:2.4.0' // any dependencies marked 'api' will also be available to app module api 'com.squareup.retrofit:retrofit:1.9.0' api 'io.reactivex:rxjava:1.0.13' api 'io.reactivex:rxandroid:0.25.0' } 

build.gradle application module:

 dependencies { // declare dependency on library module implementation project(':library') // only need to declare dependencies unique to app implementation 'example.dependency:1.0.0' } 
+6
source

You could do something like this when the build.gradle project defines the dependencies needed as variable names, and then in the build.radry.exe files that you need to include in the variable names. This is very useful when you have many modules and you do not want to edit all when changing the version number!

build.gradle project

 buildscript { ext { googlePlayServicesVersion = '7.5.0' supportLibVersion = '22.2.0' } ... (the rest of your repositories/dependency info here) ... } ext { minSdkVersion=16 targetSdkVersion=21 buildToolsVersion='22.0.1' compileSdkVersion=21 //Android Dependencies supportV4 = 'com.android.support:support-v4:' + supportLibVersion supportAnnotations = 'com.android.support:support-annotations:' + supportLibVersion recyclerView = 'com.android.support:recyclerview-v7:' + supportLibVersion cardView = 'com.android.support:cardview-v7:' + supportLibVersion palette = 'com.android.support:palette-v7:' + supportLibVersion appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion multidex = 'com.android.support:multidex:1.0.1' appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion supportDesign = 'com.android.support:design:' + supportLibVersion playServicesAnalytics = 'com.google.android.gms:play-services-analytics:' + googlePlayServicesVersion } 

build.gradle application file

 dependencies { compile rootProject.ext.supportV4 compile rootProject.ext.appCompat compile rootProject.ext.supportAnnotations compile rootProject.ext.recyclerView compile rootProject.ext.cardView compile rootProject.ext.palette compile rootProject.ext.appCompat compile rootProject.ext.multidex compile rootProject.ext.supportDesign compile rootProject.ext.playServicesAnalytics } 

Hope this helps!

+3
source

Dependency Block (Close) DependencyHandler Required as Delegate

You need to pass the DependencyHandler of each project to common dependencies in the gradle.build project.

build.gradle project

 ext.sharedGroup = {dependencyHandler-> delegate = dependencyHandler compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.squareup.retrofit:retrofit:1.9.0' compile 'io.reactivex:rxjava:1.0.13' compile 'io.reactivex:rxandroid:0.25.0' } 

build.gradle application

 dependencies { sharedGroup dependencies } 

ref. https://github.com/b1uec0in/DependencyVersionResolver

(see 2. Using the default dependency group. This example explains a lot of other tips for sharing the library version, sdk versions ... for a large project with many modules.)

+2
source

Based on @SMKS answer, I would prefer this solution for transitive option and simplicity

build.gradle project

 buildscript { ... (the rest of your repositories/dependency info here) ... } ext { googlePlayServicesVersion = '7.5.0' supportLibVersion = '22.2.0' } 

build.gradle application file

 dependencies { compile 'com.android.support:support-v4:' + supportLibVersion compile ' com.android.support:support-annotations:' + supportLibVersion compile = 'com.android.support:recyclerview-v7:' + supportLibVersion { transitive = true // do not know if this make sens/interest just for example } ... } 
0
source

All Articles