compile, apk project, compile project,provided,implementation project之间的Gradle依赖区别 [英] Gradle dependencies difference between compile, apk project, compile project,provided,implementation project

查看:49
本文介绍了compile, apk project, compile project,provided,implementation project之间的Gradle依赖区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gradle 之间的依赖差异.

Gradle dependencies difference between.

 compile
 apk project 
 compile project
 provided project
 implementation

我的问题是

这里的compileapk projectcompile projectprovided project有什么区别?

What's the difference between compile ,apk project, compile project,provided project here?

推荐答案

这里有两件单独的事情需要讨论:依赖配置和依赖源.

There's two separate things to discuss here: Dependency Configurations and Dependency Sources.

依赖配置

配置有助于定义依赖项的传递性,这反过来又消除了必须发现和指定您自己的项目/库所需的库(自动包括它们)的痛苦.gradle 中的这种配置概念与 Maven 的概念非常相似范围:

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:

  1. compile:编译依赖项在项目的所有类路径中都可用.此外,这些依赖关系会传播到依赖项目.运行时通常需要编译时依赖项.
  2. apk:定义运行时依赖.编译时不需要具有此范围的依赖项,但需要执行.这意味着您可以在编译时节省时间,并且在项目实际运行时仍然可以使用依赖项.这是一个很好的例子,说明何时使用 apk 依赖项.
  3. provided:表示此依赖项在运行时环境中可用.因此,此范围仅在编译和测试类路径上可用,并且不可传递.Android 项目不支持它,但您可以通过定义自己的配置来解决它,如此处所述.
  1. compile: Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. A compile-time dependency is generally required at runtime.
  2. apk: Defines a runtime dependency. 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.
  3. provided: It means that this dependency is available on the runtime environment. 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上遇到更多的配置,比如testCompile,它允许你指定一个只用于测试的编译时依赖,比如你想在你的测试,然后你会做如下:

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:

testCompile 'junit:junit:4.12'

依赖来源

一旦您了解了可用的配置,您就需要指定一个实际的依赖项.依赖关系可能是内部的或外部的,您可能依赖于您正在使用的另一个库,以及公开可用的库.这是 project 关键字的用武之地,它允许您指定对内部模块或库的依赖项.通过将依赖项定义为 compile project,您将该模块或库作为传递依赖项添加到您的项目中.

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. By defining a dependency as compile project, you are adding that module or library as a transitive dependency to your project.

假设您有一个包含三个模块(producerconsumershared)的项目 messages,该项目结构如下:

Assume you have a project messages with three modules (producer, consumer and shared), the project structure would look as follows:

messages/
    build.gradle
    settings.gradle
    consumer/
        build.gradle
    producer/
        build.gradle
    shared/
        build.gradle

现在假设 consumerproducer 都以 json 格式存储消息,并且您想使用 google-gson 用于此目的.假设两个项目都有一些它们所依赖的公共源代码,即您的 shared 模块.consumer 的 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. Assume that both projects have some common source code that they depend on, your shared module. consumer's build.gradle could then define the following dependencies:

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

总结起来,就是配置的结合,让你可以将依赖声明为compilecompileprojectapk project 等等!

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, apk project, compile project,provided,implementation project之间的Gradle依赖区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆