版權聲明:本文為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

