【构建工具】gradle中api、implementation和compile的区别

Configuration nameRoleConsumable?Resolvable?Description

api

Declaring API dependencies

no

no

This is where you should declare dependencies which are transitively exported to consumers, for compile.

implementation

Declaring implementation dependencies

no

no

This is where you should declare dependencies which are purely internal and not meant to be exposed to consumers.

compileOnly

Declaring compile only dependencies

no

no

This is where you should declare dependencies which are only required at compile time, but should not leak into the runtime. This typically includes dependencies which are shaded when found at runtime.

runtimeOnly

Declaring runtime dependencies

no

no

This is where you should declare dependencies which are only required at runtime, and not at compile time.

testImplementation

Test dependencies

no

no

This is where you should declare dependencies which are used to compile tests.

testCompileOnly

Declaring test compile only dependencies

no

no

This is where you should declare dependencies which are only required at test compile time, but should not leak into the runtime. This typically includes dependencies which are shaded when found at runtime.

testRuntimeOnly

Declaring test runtime dependencies

no

no

This is where you should declare dependencies which are only required at test runtime, and not at test compile time.

 

看起来这三个关键字的作用是一样的,那么,他们到底有什么区别呢?
     api和compile关键字作用效果是一样的,使用时可以互相替换。实际上,api关键字是用来替代compile关键字的,因为compile关键字将来会被弃用。在高版本的gradle,使用compile关键字会报错并提示使用api关键字代替 。api或compile关键字引用的包对于其他module来说是可见的,而implementation关键字引用的包对于其他module来说是不可见的。

https://docs.gradle.org/current/userguide/java_library_plugin.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
gradle implementationcompile都是在项目使用的依赖管理关键字。 在旧版本的Gradle,使用的是compile关键字来引入项目所需要的依赖库。例如,使用compile 'com.android.support:appcompat-v7:28.0.0'来引入Android Support库的appcompat库。 然而,在Gradle 3.0及以上的版本compile已经被implementation所取代。这是由于implementation可以更好地处理依赖的传递性问题。所谓的传递性问题是指当一个库依赖另外一个库时,是否需要将被依赖的库也自动地引入到项目。 通过使用implementation关键字,Gradle能够更好地优化编译过程,只将直接使用的库引入项目,而不会将间接使用的库也引入项目。这可以有效减少项目的编译时间,并减少最终应用包的大小。 例如,假设库A依赖库B,而项目只直接使用了库A。如果使用compile关键字,则会将库B也引入到项目,即使项目并没有直接使用库B。但如果使用implementation关键字,则只会将库A引入项目,不会引入库B,从而减少了项目的依赖。 除了implementation之外,还有另外两个依赖管理关键字:api和testImplementationapi关键字可以将依赖库引入到项目的编译路径,并可以传递给依赖项目。而testImplementation关键字则是专门用于引入测试时所需的库。 总而言之,compile和implementation都是用于项目的依赖管理,但Gradle 3.0及以上的版本推荐使用implementation来更好地管理项目的依赖关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明天争取

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值