版权声明:本文为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