Gradle中implementation、api、compileOnly区别详解

一、implementation和api的区别

1.implementation:

只能在内部使用此模块,比如我在一个libiary中使用implementation依赖了gson库,然后我的主项目依赖了libiary,那么,我的主项目就无法访问gson库中的方法。这样的好处是编译速度会加快,推荐使用implementation的方式去依赖,该依赖方式所依赖的库不会传递,只会在当前module中生效。

2.api

该依赖方式会传递所依赖的库,当其他module依赖了该module时,可以使用该module下使用api依赖的库。

二、其他依赖方式

1.provided(compileOnly)

只在编译时有效,不会参与打包
可以在自己的moudle中使用该方式依赖一些比如com.android.support,gson这些使用者常用的库,避免冲突。

2.apk(runtimeOnly)

只在生成apk的时候参与打包,编译时不会参与,很少用。

3.testCompile(testImplementation)

testCompile 只在单元测试代码的编译以及最终打包测试apk时有效。

4.debugCompile(debugImplementation)

debugCompile 只在debug模式的编译和最终的debug apk打包时有效

5.releaseCompile(releaseImplementation)

Release compile 仅仅针对Release 模式的编译和最终的Release apk打包。

猜你喜欢

转载自blog.csdn.net/weixin_42600398/article/details/123813694
今日推荐