原文:https://blog.csdn.net/yuzhiqiang_1993/article/details/78366985
Android Studio3.0正式版已经出来了,相比2.x的版本,Gradle版本也升级为了3.x,编译速度提高了不少。
在gadle3.0之后,默认的依赖由之前的compile更改为implementation了。
如果我们依然使用compile的话会有如下提示
Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
告诉我们配置 compile已过时,已被 implementation 和api 取代。
下面我们来看看他们之前的差异:
首先是2.x版本的依赖方式:
再来看看3.0的:
可以看到在gradle3.0中,compile依赖关系已被弃用,被implementation和api替代,provided被compile only替代,apk被runtime only替代,剩下的看名字就知道了。
我们先来看看implementation和api的区别:
api:跟2.x版本的 compile完全相同
implementation:只能在内部使用此模块,比如我在一个libiary中使用implementation依赖了gson库,然后我的主项目依赖了libiary,那么,我的主项目就无法访问gson库中的方法。这样的好处是编译速度会加快,推荐使用implementation的方式去依赖,如果你需要提供给外部访问,那么就使用api依赖即可
还不熟悉2.x版本依赖的可以看看下面的说明,括号里对应的是3.0版本的依赖方式。
compile(implementation,api)
这种是我们最常用的方式,使用该方式依赖的库将会参与编译和打包。
implementation:该依赖方式所依赖的库不会传递,只会在当前module中生效。
api:该依赖方式会传递所依赖的库,当其他module依赖了该module时,可以使用该module下使用api依赖的库。
当我们依赖一些第三方的库时,可能会遇到com.android.support冲突的问题,就是因为开发者使用的compile或api依赖的com.android.support包与我们本地所依赖的com.android.support包版本不一样,所以就会报All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes这个错误。
解决办法可以看这篇博客:com.android.support冲突的解决办法
provided(compileOnly)
只在编译时有效,不会参与打包
可以在自己的moudle中使用该方式依赖一些比如com.android.support,gson这些使用者常用的库,避免冲突。
apk(runtimeOnly)
只在生成apk的时候参与打包,编译时不会参与,很少用。
testCompile(testImplementation)
testCompile 只在单元测试代码的编译以及最终打包测试apk时有效。
debugCompile(debugImplementation)
debugCompile 只在debug模式的编译和最终的debug apk打包时有效
releaseCompile(releaseImplementation)
Release compile 仅仅针对Release 模式的编译和最终的Release apk打包。