Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I share dependencies between Android modules

I have an Android application module (app) and an Android library module (library). Both app and library contain these 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 that block to the project build.gradle, it complains about not knowing the "compile" DSL.

EDIT: I'm asking about putting this dependencies block in the PROJECT build.gradle, to avoid repeating in each module's build.gradle.

like image 645
Andrew Avatar asked Aug 05 '15 19:08

Andrew


People also ask

CAN modules have dependencies?

Module dependencies Modules can depend on SDKs, JAR files (libraries) or other modules within a project. When you compile or run your code, the list of module dependencies is used to form the classpath for the compiler or the JVM.

Where are module dependencies declared in an Android project?

To use your new Android library's code in another app or library module within the same project, add a project-level dependency: Navigate to File > Project Structure > Dependencies. Select the Module in which you'll use the library. In the Declared Dependencies tab, click and select Module Dependency in the dropdown.

What are Android dependencies?

In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project. For example: Suppose I want to show some images in ImageView. But I'm using Glide Library to enhance the smoothness of application.


2 Answers

As of Gradle Plugin version 3.0.0 there is a nicer way to do this. We can control whether each dependency is available for only the current module, or for the current module AND any modules which depend on it. This will allow us to easily share dependencies across modules within a project.

Here's how we used to declare dependencies:

  • compile 'example.dependency:1.0.0'

Here are the new configurations which should replace compile:

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

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

library module build.gradle

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' } 

app module build.gradle:

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

Please see this guide for further information and diagrams.

like image 122
Jules Avatar answered Sep 18 '22 12:09

Jules


The dependencies block(closure) needs DependencyHandler as delegate

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

project build.gradle

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' } 

app build.gradle

dependencies {     sharedGroup dependencies } 

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

(see 2. Using default dependency group. This sample explains many other tips for sharing library version, sdk versions ... for large project that have many modules.)

like image 21
UnknownStack Avatar answered Sep 20 '22 12:09

UnknownStack