美文网首页
2019-04-08 Android 运用第三方库总结:Jar包

2019-04-08 Android 运用第三方库总结:Jar包

作者: YuQiao0303 | 来源:发表于2019-04-08 09:42 被阅读0次

前言:
Android开发过程中,常常会用到别人写好的开源代码,可能包括写好的各种类,接口,广播,服务等。
有两种常用的方式导入第三方源码:jar包和module

jar包

我们先把jar包放到app/libs下
然后在Android Studio中,右键,add as a library

此时你会发现你import 里面的东西,不会再报错了


image.png

module

我们可以把每个项目分为多个module,如果不使用第三方module,则整个项目只有一个module,就是app。
而使用第三方源码的另一种方式就是作为module添加。
分两步:

  1. 把module加到项目中
  2. 给app 这个module 添加依赖

把module加到项目中

首先下载第三方库(注意此时要放在项目外,不要放在项目文件夹中的任何地方),然后File, New, import module ,选择要用的库,此时默认的名字是冒号加上该文件夹的名称:


不用改,就这样就行

点finish。

此时如果无误的话,左边已经会出现与app并列的新加的module,但是我这里Android studio说遇到了一个error,并没有出现新的module。

解决办法是手动添加:在settings.gradle 中,include那一行的最后,加上逗号,单引号括起来刚才的module名称,重新sync即可


给app 这个module 添加依赖

右键app module,open module settings ,出现一个对话框
最左边选中app,然后选dependencies 选项卡,点右边绿色的加号,选module dependency,选你刚加的module即可。
这样操作成功之后,你会发现你的app 的build.gradle 中,dependencies中出现了
compile project(':xxxLib') //引号里是module名称

implement project(':xxxLib') //引号里是module名称

然后就可以直接调用该module所在的包里面的代码了

例如:
github项目:SeekBarPreference https://github.com/DreaminginCodeZH/SeekBarPreference
这个demo中有两个module,sample 和 library. 其中sample 依赖于library.
在sample/res/sml/settings.xml中,就直接用了library中,me.zhanghai.android.seekbarpreference这个包当中的SeekBarPreference这个类。

image.png

相关文章

网友评论

      本文标题:2019-04-08 Android 运用第三方库总结:Jar包

      本文链接:https://www.haomeiwen.com/subject/zegyiqtx.html