简体   繁体   English

Gradle依赖关系编译,apk项目,编译项目,提供,实现项目之间的区别

[英]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? compileapk projectcompile projectprovided 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的范围非常相似:

  1. compile : Compile dependencies are available in all classpaths of a project. compile :编译依赖项在项目的所有类路径中都可用。 Furthermore, those dependencies are propagated to dependent projects. 此外,这些依赖项将传播到依赖项目。 A compile-time dependency is generally required at runtime. 运行时通常需要编译时依赖项。
  2. apk : Defines a runtime dependency. apk :定义运行时依赖项。 A dependency with this scope will not be required at compile time, but it will be for execution. 在编译时不需要具有此作用域的依赖项,但它将用于执行。 This means that you can save time while compiling and still have the dependency available when your project actually runs. 这意味着您可以在编译时节省时间,并在项目实际运行时仍具有依赖性。 This is a good example of when to use an apk dependency. 是何时使用apk依赖项的一个很好的例子。
  3. 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. 因此,此范围仅在编译和测试类路径中可用,并且不可传递。 It is not supported on Android projects, though you can workaround it by defining your own configuration as discussed here . 它不支持在Android项目,但您可以通过所讨论定义自己的配置解决办法是在这里

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: 假设您有一个包含三个模块( producerconsumershared )的项目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. 现在假设consumerproducer都以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! 总而言之,它是配置的组合,使您能够将依赖关系声明为compilecompile projectapk project等等!

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

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