android gradle 3.0.0 中依赖指令implementation、api 的区别

AndroidStudio升级到3.0之后,gradle版本也随之升级到了3.0.0版本。

classpath ‘com.android.tools.build:gradle:3.0.0’
1
在新建一个Android工程的时候,build.gradle中的依赖默认为implementation,而不是之前的compile。另外,gradle 3.0.0版本以上,还有依赖指令api。本文主要介绍下implementation和api的区别。

新建工程默认生成的app的build.gradle文件中的依赖:

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

}
1
2
3
4
5
6
7
8
api 指令

完全等同于compile指令,没区别,你将所有的compile改成api,完全没有错。

implementation指令

这个指令的特点就是,对于使用了该命令编译的依赖,对该项目有依赖的项目将无法访问到使用该命令编译的依赖中的任何程序,也就是将该依赖隐藏在内部,而不对外部公开。

简单的说,就是使用implementation指令的依赖不会传递。例如,有一个module为testsdk,testsdk依赖于gson:

implementation ‘com.google.code.gson:gson:2.8.2’
1
这时候,在testsdk里边的java代码是可以访问gson的。

另一个module为app,app依赖于testsdk:

implementation project(‘:testsdk’)
1
这时候,因为testsdk使用的是implementation 指令来依赖gson,所以app里边不能引用gson。

但是,如果testsdk使用的是api来引用gson:

api ‘com.google.code.gson:gson:2.8.2’
1
则与gradle3.0.0之前的compile指令的效果完全一样,app的module也可以引用gson。这就是api和implementation的区别。

建议

在Google IO 相关话题的中提到了一个建议,就是依赖首先应该设置为implementation的,如果没有错,那就用implementation,如果有错,那么使用api指令。使用implementation会使编译速度有所增快。

参考:

http://blog.csdn.net/soslinke…

https://zhuanlan.zhihu.com/p/…

https://stackoverflow.com/que…

http://blog.csdn.net/JF_1994/…

    原文作者:Android
    原文地址: https://segmentfault.com/a/1190000016700076
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞