简体   繁体   English

Android Gradle 在另一个模块中包含模块依赖项

[英]Android Gradle include module dependencies in another module

I am using the latest Android Studio 3.0.0-beta6 to build my Android project and there's this dependency issue.我正在使用最新的 Android Studio 3.0.0-beta6 来构建我的 Android 项目,但存在此依赖性问题。 Gradle encouraged me to replace all compile's with implementation's . Gradle鼓励我更换所有compile's使用implementation's Here's my project structure:这是我的项目结构:

Project:项目:

  • module1模块1

    • module2模块2

Module1 depends on some libraries, module2 depends on module1. Module1 依赖于一些库,module2 依赖于 module1。 However, the libraries from module1 are not visible in module2.但是,模块 1 中的库在模块 2 中不可见。 I don't want to copy-paste dependencies and would rather have the library dependencies declared only once.我不想复制粘贴依赖项,而是希望库依赖项只声明一次。 Is there a simple solution to this?有没有简单的解决方案? Thank you.谢谢。

module1's build gradle: module1 的构建等级:

dependencies {
    ....
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    ...
}

module2's build gradle: module2 的构建等级:

implementation project(':module1')

In your Root Project -> build.gradle you should have something like:在你的 Root Project -> build.gradle 你应该有这样的东西:

allprojects {
    repositories {
        jcenter()
    }
}

Add dependencies there!!在那里添加依赖项!!

UPDATE更新

If you do not want all your modules to have the common dependencies but only specific modules then do this:如果您不希望所有模块都具有公共依赖项而只希望特定模块,则执行以下操作:

  1. Create a folder in your Root Project/gradleScript在您的根项目/gradleScript 中创建一个文件夹
  2. Create a .gradle file in this folder (eg gradleScript/dependencies.gradle) that looks like:在此文件夹中创建一个 .gradle 文件(例如 gradleScript/dependencies.gradle),如下所示:

     ext { //Version supportLibrary = '22.2.1' //Support Libraries dependencies supportDependencies = [ design : "com.android.support:design:${supportLibrary}", recyclerView : "com.android.support:recyclerview-v7:${supportLibrary}", cardView : "com.android.support:cardview-v7:${supportLibrary}", appCompat : "com.android.support:appcompat-v7:${supportLibrary}", supportAnnotation: "com.android.support:support-annotations:${supportLibrary}", ] }
  3. In your root project build.gradle add this line:在您的根项目 build.gradle 中添加以下行:

     buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' } } // Load dependencies apply from: 'gradleScript/dependencies.gradle'
  4. In your modules accordingly add this:在您的模块中相应地添加以下内容:

     // Module build file dependencies { //...... compile supportDependencies.appCompat compile supportDependencies.design }

Hope this helps!!!希望这有帮助!!!

Old question, but it sounds to me like what you're after is simply to use api instead of implementation in your gradle files?老问题,但在我看来,您所追求的只是在您的 gradle 文件中使用api而不是implementation

If a module X declares an api dependency, it's transitively available to any other module that depends on X. If it however declares an implementation dependency, the classes from that dependency is put on the class path for module X, but not for any modules in turn depending on module X.如果模块 X 声明了api依赖项,则它可传递给依赖于 X 的任何其他模块。但是,如果它声明了implementation依赖项,则该依赖项中的类将放在模块 X 的类路径上,但不适用于其中的任何模块取决于模块 X。

From Gradle's documentation :从 Gradle 的文档

Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers. api 配置中出现的依赖项将传递给库的使用者,因此将出现在使用者的编译类路径上。 Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath.另一方面,在实现配置中发现的依赖项不会暴露给消费者,因此不会泄漏到消费者的编译类路径中。

So to be concrete:所以具体来说:

module 1:模块一:

dependencies {
    ....
    api 'io.reactivex.rxjava2:rxandroid:2.0.1'
    api 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    ...
}

module 2:模块二:

implementation project(':module1')

Isn't this the simplest solution to what you're trying to do?这不是您想要做的最简单的解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM