版权声明:本文为HaiyuKing原创文章,转载请注明出处!

概述

在Android开发过程,经常需要用到第三方库以及jar、so、arr文件,那么如何引用到项目中呢?下面简单介绍下。

引用第三方库

一般按照第三方库作者提供的引用方式进行引用即可。

比如,以引用okhttp-utils开源库为例:

在module(比如app)的build.gradle文件中添加下面的代码

注意:旧版本Android studio的写法是compile 'com.zhy:okhttputils:2.6.2'

新版本Android Studio的写法略有不同:implementation 'com.zhy:okhttputils:2.6.2'

不过在新版本Android studio中是兼容旧版本的写法的。

然后Sync Now 或者 Sync Project with Gradle Files

引用jar文件

其实当你在新建项目的时候就默认可以编译libs目录下的jar了,因为所有的module的build.gradle文件中含有下面的依赖:

所以只需要将引用的jar文件复制到module(为什么是module,而不是app,因为jar文件不一定是放到app中,可能是任意module中;而且app也是module的一种)的libs目录中即可。

将jar包复制到libs目录下

Sync Project with Gradle Files

引用成功的效果

至此,只能算是将jar成功引用到baselibrary中了,也就是说只能在baselibrary中使用这个jar,而不能在app中使用(app中可是依赖这个baselibrary了)。那么如何实现依赖baselibrary的module中也能使用jar文件的方法呢?

在当前module的build.gradle文件的dependencies{}中添加以下代码【适用依赖当前module的其他module也需要使用这个jar文件的情况】

apply plugin: 'com.android.library'

android {

compileSdkVersion 26

defaultConfig {

minSdkVersion 16

targetSdkVersion 26

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

postprocessing {

removeUnusedCode false

removeUnusedResources false

obfuscate false

optimizeCode false

proguardFile 'proguard-rules.pro'

}

}

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:26.1.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//jsoup 必须compile,否则app那里找不到相关类

compile files('libs/gson-2.2.4.jar')

}

Sync Project with Gradle Files

引用arr包

将arr包复制到module的libs目录下

在build.gradle中添加下面的代码

apply plugin: 'com.android.application'

android {

compileSdkVersion 27

defaultConfig {

applicationId "com.why.project.helloworld"

minSdkVersion 16

targetSdkVersion 27

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

//在layout文件夹下建立子文件夹

sourceSets {

main{

res.srcDirs = [

'src/main/res/layout/home',

'src/main/res/layout',

'src/main/res'

]

}

}

}

repositories{

flatDir {

dirs 'libs'

}

}

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation 'com.android.support:appcompat-v7:27.1.1'

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation project(':baselibrary')

compile 'com.zhy:okhttputils:2.6.2'

compile(name: 'ijkplayer', ext: 'aar')

compile(name: 'libp', ext: 'aar')

}

Sync Project with Gradle Files

如果该arr文件是在module中引用的,那么app又依赖了这个module,那么如果不做任何处理的话,编译后会出现failed to resolve的错误提示

比如,我在baselibrary中引用了libnewurl.arr文件。

解决方案,在app(因为app依赖了这个baselibrary)的build.gradle文件中添加以下代码:

repositories{

flatDir {

dirs 'libs'

dirs project(':baselibrary').file('libs')

}

}

上面代码中的baselibrary是module的名字,libs是module依赖的aar库所在的目录。

引用so包

方案一【建议使用这个方案】

在 src/main/ 目录下创建文件夹 jniLibs(如果有就不需要创建了),将so文件复制到这个目录下即可,工程会自动加载src/main/jniLibs目录下的so动态库。

将so文件复制到jniLibs目录中

方案二

在libs目录下放入对应不同CPU架构的so文件,通过在build.gradle文件的android{}中加入代码: jniLibs.srcDir 'libs' 来说明so的路径为该libs路径。

sourceSets {

main {

jniLibs.srcDir 'libs'

}

}

如果想要控制使用的CPU平台,则在build.gradle的defaultConfig下添加

ndk {

abiFilters "armeabi", "armeabi-v7a" //选择要使用的平台 ,"x86", "mips"

}

如果编译不通过,在项目的gradle.properties中添加

android.useDeprecatedNdk=true

参考资料

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐