Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/31841259/how-do-i-share-dependencies-between-android-modules

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...