如何在Android模块之间共享依赖项

如何在Android模块之间共享依赖项

本文介绍了如何在Android模块之间共享依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序模块(应用程序)和一个Android库模块(库).应用程序和库都包含以下相同的依赖项:

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

但是,当我尝试将该块添加到 project build.gradle时,它抱怨不知道编译" DSL.

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

我要询问的是将此依赖项块放入PROJECT build.gradle中,以避免在每个模块的build.gradle中重复.

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

推荐答案

您可以在库模块中定义共享gradle依赖关系,并且如果app模块将库作为依赖项,则无需重复指定所有内容.进一步讲,您可以创建一个通用"模块,该模块需要共享的gradle依赖项,并且同时具有应用程序&库模块需要通用模块.

You can define shared gradle dependencies in the library module, and if the app module has the library as a dependency, you won't need to specify everything twice. Taking this further, you could create a 'common' module that requires the shared gradle dependencies, and have both the app & library module require the common module.

这篇关于如何在Android模块之间共享依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 13:32