项目中需要引入几个 so文件,但APP一直崩溃报错
java.lang.UnsatisfiedLinkError: Couldn’t load ad from loader dalvik.system.PathClassLoader…
修改 app 下的 build.gradle 文件,添加下方代码:
// 将 .so 文件打包为 Native_Libs.jar 包文件
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
sourceSets.main{
jniLibs.srcDirs = ['libs'];
}
另外在 dependencies 中加入一行
compile files('libs/Native_Libs.jar') // 加载上方 .so 生成的该文件
至于这个包的名字 Native_Libs 是可以通过 baseName 自定义的
下面贴上完整的 build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.xxx.xxx.xxx"
minSdkVersion 15
targetSdkVersion 23
versionCode 2
versionName "2.0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// 将 .so 文件打包为 Native_Libs.jar 包文件
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
sourceSets.main{
jniLibs.srcDirs = ['libs'];
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.1'
compile 'com.joanzapata.iconify:android-iconify-ionicons:2.2.1'
compile files('libs/Chance_Ad_SDK_Android_v5.3.5.jar')
compile files('libs/Native_Libs.jar') // 加载上方 .so 生成的该文件
}