关于android:Android-导入第三方库的三种方式

1次阅读

共计 2761 个字符,预计需要花费 7 分钟才能阅读完成。

Android 导入第三方库的三种形式

一:介绍
三种形式:Gradle 抓取,libs 导入,源码导入
二:Gradle 抓取
Gradle 抓取的办法最简略,只须要在 Project->app 文件夹下的 build.gradle 文件增加依赖:

在 build.gradle 文件中增加依赖

Android Studio 默认依赖库是 google(),jcenter(), 咱们还能增加 Maven 仓库的依赖

allprojects {
    repositories {google()
        jcenter()
        mavenCentral()
   maven {url "http://mvn.gt.igexin.com/nexus/content/repositories/releases/"}// 个推的 maven 镜像依赖
        maven {url 'http://maven.aliyun.com/nexus/content/repositories/releases/'}// 阿里云的 maven
        }
        }

下面应用 implementation , 还有 compile 和 api 的区别
首先是 2.x 版本的依赖形式:

再来看看 3.0 的:

比照能够看到,Android 3.0 中,compile 依赖关系已被弃用,被 implementation 和 api 代替,provided 被 compile only 代替,apk 被 runtime only 代替。
implementation:应用了该命令编译的依赖,它仅仅对以后的 Module 提供接口。

LibraryA 中援用了 LibraryC 的库,如果 LibraryA 中对 LibraryC 的依赖用的是 implementation 关键字。如下:

dependencies {
    . . . . 
    implementation project(path:':libraryC')}

那么 LibraryC 中的接口,仅仅只能给 LibraryA 应用,而咱们的 App Module 是无法访问到 LibraryC 提供的接口的,也就是将该依赖暗藏在外部,而不对外部公开。这就是 implementation 关键字的作用。
api(相似于之前的 compile)能够提供给内部应用
等同于原有的 compile,此依赖配置,使 Gradle 意识到,其引入的依赖模块,无论在编译期还是在运行时,都对其余下层模块可见,即通过 api 配置引入的依赖模块,在依赖关系上具备传递性。这将会使得其余下层模块能够间接应用依赖模块的 Api,但这同时意味着一旦依赖模块产生 Api 的改变,将导致所有曾经应用了此依赖模块改变了的 Api 的下层模块都须要从新执行编译

问题:
Android 依赖会产生依赖抵触问题,jar 包中曾经增加了某个依赖,咱们我的项目有增加某个依赖
例如:
api ‘cn.bmob.android:bmob-sdk:3.6.6’
初见 Unable to merge dex
改了 Gradle 必然要编译,于是顺手点了 Sync 后就去打水上厕所了,美滋滋的回来后发现,纳尼?一片血红色在我的 Build 栏目下,认真一看:

Caused by: com.android.dex.DexException: Multiple dex files define Lcom/google/gson/internal/bind/TypeAdapters;

这里的意思就是说,反复的 dex 文件呈现在了 TypeAdapters 这个类了,而后就利用 AndroidStudio 的弱小的查问性能,

能够看到,这个类在 com.google.code.gson 和 cn.bmob.android 这两个 Gradle 的依赖里都呈现了,这就是所谓的反复依赖或者依赖抵触或者 Jar 包抵触了,其实就是 Bmob 的 SDK 里曾经本人集成了 gson 和 okio 这两个 jar 了,而我本人在我的项目中也用到了这两个 jar 包,从而导致了依赖抵触。
解决方案:
除了删除抵触包外,咱们还能够用 Gradle 的 exclude group 将指定的包名排除到编译范畴外

 //bmob-sdk:Bmob 的 android sdk 包,蕴含了 Bmob 的数据存储、文件等服务,以下是最新的 bmob-sdk:
   implementation ('cn.bmob.android:bmob-sdk:3.5.5'){ // gson-2.6.2
        exclude group: 'com.squareup.okhttp3'// 等价于 exclude group: 'com.squareup',module:'okhttp3'
        exclude group: 'com.squareup.okio'
        exclude group: 'com.google.code.gson'
//        exclude(module: 'gson')     // 避免版本抵触
    }

三:libs 导入
将网上下载对应的第三方库的 jar 包放入 Project–>app 文件夹下的 libs 文件夹下:

而后在单击 jar 包右键,抉择“Add as library…”可实现导入;或者在咱们上述的 build.gradle 文件中增加依赖:

dependencies {implementation fileTree(dir: 'libs',includes: ['*.jar','*.aar'])
 implementation files('libs/cglib-for-android.jar')
 }

形式二:
在 module 的 build.gradle 先增加

android {。。。。。。。}
// 与 dependencies 同一级别 
// 而后在 dependencies 中增加
repositories {
    flatDir {dirs 'libs'}
}

dependencies {implementation(name:'cglib-for-android',ext:'jar')
 implementation(name:'xx', ext:'aar')// 这是 arr 包
}

四:源码导入
源码导入的办法最大的长处受害于开源,咱们能够通过我的项目中的需要,对第三方库的源码进行间接批改。首先咱们下载第三方库的源码到本地,将源码解压到咱们我的项目的根目录:

利用其余模块的间接

dependencies {implementation project(path: ':common_base')
}

在 settings.gradle 增加新导入的项目名称:

include ':module_red'
include ':module_goods'
include ':module_free'
include ':module_me'
include ':module_login'
include ':common_base'// 依赖
include ':app'
rootProject.name = "KuaiLaiTao"

结尾:兴许起点只有失望和失败,但这绝不是进行前行的理由。

正文完
 0