版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!android
在Android開發過程,常常須要用到第三方庫以及jar、so、arr文件,那麼如何引用到項目中呢?下面簡單介紹下。架構
通常按照第三方庫做者提供的引用方式進行引用便可。app
好比,以引用okhttp-utils開源庫爲例:post
注意:舊版本Android studio的寫法是compile 'com.zhy:okhttputils:2.6.2'
gradle
新版本Android Studio的寫法略有不一樣:implementation 'com.zhy:okhttputils:2.6.2'
ui
不過在新版本Android studio中是兼容舊版本的寫法的。url
其實當你在新建項目的時候就默承認以編譯libs目錄下的jar了,由於全部的module的build.gradle文件中含有下面的依賴:spa
因此只須要將引用的jar文件複製到module(爲何是module,而不是app,由於jar文件不必定是放到app中,多是任意module中;並且app也是module的一種)的libs目錄中便可。.net
至此,只能算是將jar成功引用到baselibrary中了,也就是說只能在baselibrary中使用這個jar,而不能在app中使用(app中但是依賴這個baselibrary了)。那麼如何實現依賴baselibrary的module中也能使用jar文件的方法呢?3d
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')
}
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')
}
好比,我在baselibrary中引用了libnewurl.arr文件。
解決方案,在app(由於app依賴了這個baselibrary)的build.gradle文件中添加如下代碼:
repositories{
flatDir {
dirs 'libs'
dirs project(':baselibrary').file('libs')
}
}
上面代碼中的baselibrary是module的名字,libs是module依賴的aar庫所在的目錄。
在 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'
}
}
ndk {
abiFilters "armeabi", "armeabi-v7a" //選擇要使用的平臺 ,"x86", "mips"
}
android.useDeprecatedNdk=true