解决Android Studio添加依赖库后找不到依赖库类问题(implementation、api区别)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lkjfyy/article/details/82465892

说在前面:本菜今天下午从GitHub上down下了一个库,欢天喜地的导入到了自己的项目中,定睛一看依赖库的build.gradle中竟然还是用的compile,版本号也才24,婶儿能忍叔也不能忍啊,果断三下五除二compile换成implementation,版本号升级到27…眼观鼻,鼻观心,静静等待building…Great!building完成,迅速运行,程序完美跑起,一个界面优美的启动页出现在我的面前……en~~,不对不对,剧本拿错了0..0,实际是喵的,我添加的依赖库的类找不到,莫非本菜太过嚣张跋扈,独断专行,简单粗暴???就此开始了本菜一下午的打怪之旅~~~

老老实实把版本改了回来,building不行?那肯定是姿势不对,又把implementation改回了compile,就像下面这样:
implementation ‘com.squareup.okhttp3:logging-interceptor:3.11.0’
————》
compile ‘com.squareup.okhttp3:logging-interceptor:3.11.0’
en~~真香!!果断程序可以跑了!!!

想到这样也不行啊,人家都说要淘汰compile了,这样也不好啊,突然想到还有api一直没用过,果断又把compile改成了api,就像下面这样:
compile ‘com.squareup.okhttp3:logging-interceptor:3.11.0’
————》
api ‘com.squareup.okhttp3:logging-interceptor:3.11.0’
en~~还是香!!!这是吃了基础的亏啊。看来就是implementation、api区别啊!!!自抽两巴掌,开始查implementation、api的区别,介绍如下:

api 指令

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

implementation指令

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

就像我添加的依赖库中添加了’com.squareup.okhttp3:logging-interceptor:3.11.0’依赖,如果使用api指令则与compile指令完全一样,在我的项目中也可以使用logging-interceptor,而如果是使用implementation指令,则在我的项目里会找不到logging-interceptor

建议

依赖首先应该设置为implementation的,如果没有错,那就用implementation,如果有错,那么使用api指令。使用implementation会使编译速度有所增快。

参考:
(https://blog.csdn.net/Next_Second/article/details/78428086)

猜你喜欢

转载自blog.csdn.net/lkjfyy/article/details/82465892