[英]Gradle dependencies difference between compile, apk project, compile project,provided,implementation project
Gradle dependencies difference between. Gradle依赖关系的区别。
compile
apk project
compile project
provided project
implementation
My questions are 我的问题是
What's the difference between compile
, apk project
, compile project
, provided project
here? compile
, apk project
, compile project
, provided project
什么区别?
There's two separate things to discuss here: Dependency Configurations and Dependency Sources. 这里有两个独立的事情要讨论:依赖配置和依赖源。
Dependency Configurations 依赖配置
Configurations help define the transitivity of a dependency, which in turn removes the pain of having to discover and specify the libraries your own project/library requires, including them automatically. 配置有助于定义依赖关系的传递性,这反过来消除了必须发现和指定您自己的项目/库所需的库的痛苦,包括它们自动包含它们。 This notion of configurations in gradle is very similar to that of Maven's scopes :
gradle中的配置概念与Maven的范围非常相似:
compile
: Compile dependencies are available in all classpaths of a project. compile
:编译依赖项在项目的所有类路径中都可用。 Furthermore, those dependencies are propagated to dependent projects. apk
: Defines a runtime dependency. apk
:定义运行时依赖项。 A dependency with this scope will not be required at compile time, but it will be for execution. provided
: It means that this dependency is available on the runtime environment. provided
:这意味着此依赖项在运行时环境中可用。 As a consequence, this scope is only available on the compilation and test classpath, and is not transitive. There are more configurations that you can encounter on Android, such as testCompile
, which allows you to specify a compile-time dependency that will only be used for testing, say you want to use junit in your tests, then you would do as follows: 您可以在Android上遇到更多配置,例如
testCompile
,它允许您指定仅用于测试的编译时依赖项,比如您想在测试中使用junit,然后您将执行以下操作:
testCompile 'junit:junit:4.12'
Dependency Source 依赖性来源
Once you understand the configurations available for you, you need to specify an actual dependency. 一旦了解了可用的配置,就需要指定实际的依赖关系。 Dependencies might be internal or external, you may rely on another library you are working on, as well as on publicly available libraries.
依赖关系可能是内部的或外部的,您可能依赖于您正在处理的另一个库,以及公共库。 Here's where the
project
keyword comes in, allowing you to specify a dependency to an internal module or library. 这是
project
关键字的来源,允许您指定对内部模块或库的依赖关系。 By defining a dependency as compile project
, you are adding that module or library as a transitive dependency to your project. 通过将依赖项定义为
compile project
,您将该模块或库添加为compile project
的传递依赖项。
Assume you have a project messages
with three modules ( producer
, consumer
and shared
), the project structure would look as follows: 假设您有一个包含三个模块(
producer
, consumer
和shared
)的项目messages
,项目结构将如下所示:
messages/
build.gradle
settings.gradle
consumer/
build.gradle
producer/
build.gradle
shared/
build.gradle
Now assume that both consumer
and producer
store messages in json format and that you want to use google-gson for that purpose. 现在假设
consumer
和producer
都以json格式存储消息,并且您希望将google-gson用于此目的。 Assume that both projects have some common source code that they depend on, your shared
module. 假设两个项目都有一些共同的源代码,它们依赖于您的
shared
模块。 consumer
's build.gradle could then define the following dependencies: 然后,
consumer
的build.gradle可以定义以下依赖项:
dependencies {
// Internal dependency to project shared
compile project (':shared')
// External dependency to publicly available library,
// through public repositories such as jcenter() or mavencentral()
compile 'com.google.code.gson:gson:1.7.2'
}
To sum up, it is the combination of both configurations and sources that enables you to declare dependencies as compile
, compile project
, apk project
and more! 总而言之,它是配置和源的组合,使您能够将依赖关系声明为
compile
, compile project
, apk project
等等!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.