Gradle 中的实现、api 和编译之间有什么区别? [英] What's the difference between implementation, api and compile in Gradle?

查看:22
本文介绍了Gradle 中的实现、api 和编译之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新到 Android Studio 3.0 并创建一个新项目后,我注意到在 build.gradle 中有一种新的方法来添加新的依赖项而不是 compileimplementation 而不是 testCompiletestImplementation.

示例:

 实现 'com.android.support:appcompat-v7:25.0.0'testImplementation 'junit:junit:4.12'

代替

 编译 'com.android.support:appcompat-v7:25.0.0'testCompile 'junit:junit:4.12'

它们之间有什么区别,我应该使用什么?

解决方案

tl;dr

只需替换:

  • compile 使用 implementation(如果不需要传递性)或 api(如果需要传递性)
  • testCompiletestImplementation
  • debugCompiledebugImplementation
  • androidTestCompileandroidTestImplementation
  • compileOnly 仍然有效.它是在 3.0 中添加的以替换提供的而不是编译的.(provided 当 Gradle 没有该用例的配置名称并以 Maven 提供的范围命名时引入.)

这是 Android Gradle 插件带来的重大变化之一3.0 Google 在 IO17 上宣布.

compile 配置现已弃用替换为 implementationapi

来自 Gradle 文档:

<块引用>

依赖项{api 'commons-httpclient:commons-httpclient:3.1'实现 'org.apache.commons:commons-lang3:3.5'}

出现在 api 配置中的依赖项将是传递地暴露给图书馆的消费者,因此将出现在消费者的编译类路径上.

implementation 配置中找到的依赖项将,在另一方面,不会暴露给消费者,因此不会泄漏到消费者的编译类路径.这有几个好处:

  • 依赖不再泄漏到消费者的编译类路径中,因此您永远不会意外地依赖传递依赖
  • 由于减少了类路径大小,编译速度更快
  • 在实现依赖项改变时减少重新编译:消费者不需要重新编译
  • 更干净的发布:当与新的 maven-publish 插件结合使用时,Java 库会生成 POM 文件准确区分编译所需的内容库以及在运行时使用库所需的条件(在其他话说,不要混淆编译库本身需要什么和什么需要针对库进行编译).

编译配置仍然存在,但不应使用,因为它不会提供apiimplementation配置提供的保证.


注意:如果您只在应用模块中使用库(常见情况),您不会注意到任何区别.
如果您有一个包含相互依赖的模块的复杂项目,或者您正在创建一个库,您只会看到不同之处.

After updating to Android Studio 3.0 and creating a new project, I noticed that in build.gradle there is a new way to add new dependencies instead of compile there is implementation and instead of testCompile there is testImplementation.

Example:

 implementation 'com.android.support:appcompat-v7:25.0.0'
 testImplementation 'junit:junit:4.12'

instead of

 compile 'com.android.support:appcompat-v7:25.0.0'
 testCompile 'junit:junit:4.12'

What's the difference between them and what should I be using?

解决方案

tl;dr

Just replace:

  • compile with implementation (if you don't need transitivity) or api (if you need transitivity)
  • testCompile with testImplementation
  • debugCompile with debugImplementation
  • androidTestCompile with androidTestImplementation
  • compileOnly is still valid. It was added in 3.0 to replace provided and not compile. (provided introduced when Gradle didn't have a configuration name for that use-case and named it after Maven's provided scope.)

It is one of the breaking changes coming with Android Gradle plugin 3.0 that Google announced at IO17.

The compile configuration is now deprecated and should be replaced by implementation or api

From the Gradle documentation:

dependencies {
    api 'commons-httpclient:commons-httpclient:3.1'
    implementation 'org.apache.commons:commons-lang3:3.5'
}

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.

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. This comes with several benefits:

  • dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
  • faster compilation thanks to reduced classpath size
  • less recompilations when implementation dependencies change: consumers would not need to be recompiled
  • cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).

The compile configuration still exists, but should not be used as it will not offer the guarantees that the api and implementation configurations provide.


Note: if you are only using a library in your app module -the common case- you won't notice any difference.
you will only see the difference if you have a complex project with modules depending on each other, or you are creating a library.

这篇关于Gradle 中的实现、api 和编译之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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