简体   繁体   English

Gradle包含依赖项的依赖项

[英]Gradle include dependencies of a dependency

Scenario 脚本

I have two Android libraries: 我有两个Android库:

  • Library A 图书馆A.
  • Library B 图书馆B.

Library B depends on Library A and includes it from its build.gradle. 库B依赖于库A,并从其build.gradle中包含它。

My Android app needs to use Library B and some base classes from Library A. The only success I've had in doing this is including both libraries in my Android app's build.gradle and setting transitive to false. 我的Android应用程序需要使用库B和库A中的一些基类。我这样做的唯一成功是在我的Android应用程序的build.gradle中包含两个库,并将传递设置为false。

Question : 问题

Is there a way for me to only add Library B and have access to its dependency from my Android app? 有没有办法让我只添加库B并从我的Android应用程序访问其依赖项?


Android Studio Android Studio

Android Studio 3.1 Canary
Build #AI-171.4415322, built on October 24, 2017
JRE: 1.8.0_152-release-1012-b01 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12.6

Gradle 摇篮

3.1.0-alpha01

That's called a transitive dependency. 这被称为传递依赖。

Gradle, and all other dependency systems handle this automatically. Gradle和所有其他依赖系统自动处理此问题。 If B needs A, you get A. If you are not getting A, that means B's dependencies are not correct. 如果B需要A,你得到A.如果你没有得到A,那就意味着B的依赖关系不正确。

Whether you should explicitly call out A in your dependencies is a debated topic. 是否应该在依赖项中明确调用A是一个有争议的话题。 I'm of the camp that you should not. 我是你不应该的营地。 My reasoning is that you can end up with something like this, 我的理由是你最终会得到这样的东西,

include B(v1.0) -depends on-> A(1.0)
include A(1.1)

Now, which version of A gets compiled into your app and used at runtime? 现在,哪个版本的A被编译到您的应用程序中并在运行时使用? A 1.0 or A 1.1? A 1.0或A 1.1? The answer depends on the implementation of the build system. 答案取决于构建系统的实现。

  • If it's A 1.0, then YOU are compiling against 1.1 but running against 1.0. 如果它是A 1.0,那么正在编译1.1而不是1.0。
  • If it's 1.1, then B is compiled against 1.0 but is using 1.1 at runtime. 如果它是1.1,那么B是针对1.0编译的,但是在运行时使用1.1。

From comments if you need to use library A inside your app and your library A included into B using implementation project(':LibA') , then it won't be a transitive to your main app . 如果您需要在应用程序中使用库A并使用implementation project(':LibA')将库A包含到B中 ,则可以从注释中implementation project(':LibA') ,那么它将不会对您的主应用程序进行传递。

You have to include it into B using api , api project(':LibA') . 你必须使用apiapi project(':LibA')将它包含在B中

The api configuration should be used for dependencies that are exported to external modules (transitive dependency). api配置应该用于导出到外部模块的依赖项(传递依赖项)。 Vice-Versa implementation configuration should be used for dependencies that are internal to the component (not transitive dependency) . Vice-Versa implementation配置应该用于组件内部的依赖项(不是传递依赖项)

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

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