[英]Android Studio - include and consume .so library
I need to use some native libraries(.so) in my android project. 我需要在我的android项目中使用一些本机库(.so)。 According to some answers here in StackOverflow about this topic, I created a
jniLibs
folder in app/src/main
and put there my files: 根据StackOverflow中有关此主题的一些答案,我在
app/src/main
创建了一个jniLibs
文件夹并将其放在我的文件中:
armeabi/my_lib.so
armeabi-v7a/my_lib.so
x86/my_lib.so
Then, in my activity class I use: 然后,在我的活动类中,我使用:
static {
System.loadLibrary("my_lib");
}
But when I run the app, an UnsatisfiedLinkError
exception is generated. 但是当我运行应用程序时,会生成一个
UnsatisfiedLinkError
异常。 If this is important to be noticed, I don't have an Android.mk file, and I haven't changed anything that has to do with this in my gradle
files. 如果重要的是要注意,我没有Android.mk文件,并且我没有在
gradle
文件中更改任何与此gradle
内容。 So, the only think I did is to copy-paste my .so
files in jniLibs
and to write the code above in my activity. 因此,我唯一想到的是将我的
.so
文件复制粘贴到jniLibs
并在我的活动中编写上面的代码。 So what might be the cause of this problem? 那么这个问题可能是什么原因呢? Am I missing something?
我错过了什么吗?
EDIT 编辑
This is my gradle: 这是我的傻瓜:
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "23.0.3"
compileOptions.encoding = 'ISO-8859-1'
defaultConfig {
applicationId "my.package"
minSdkVersion 4
targetSdkVersion 4
ndk {
moduleName "my_so_lib"
}
}
sourceSets {
main {
jni.srcDirs = ["libs"]
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable true
}
}
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a', 'mips', 'armeabi'
universalApk false
}
}
}
Solution 1 解决方案1
Create Folder "jniLibs" inside "src/main/" Put all your .so libraries inside "src/main/jniLibs" folder Folder structure looks like : 在“src / main /”中创建文件夹“jniLibs”将所有.so库放在“src / main / jniLibs”文件夹中文件夹结构如下所示:
|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|--.so Files
Can you please confirm that you have this hierarchy ? 你能否确认你有这种等级?
No extra code requires just sync your project and run your application. 没有额外的代码只需同步您的项目并运行您的应用程序。
Reference https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main 参考https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main
Solution 2 解决方案2
Add both code snippets in your module gradle.build file as a dependency: 将模块gradle.build文件中的两个代码段添加为依赖项:
compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
How to create this custom jar: 如何创建这个自定义jar:
task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
Thank you guys for helping but it was a stupid problem. 谢谢你们的帮助,但这是一个愚蠢的问题。 When I imported my
.so
files under jniLibs
, they were named like libSONAME.so
. 当我在
jniLibs
下导入我的.so
文件时,它们被命名为libSONAME.so
。 In these lines of code: 在这些代码行中:
static {
System.loadLibrary("libSONAME");
}
we should not use System.loadLibrary("libSONAME");
我们不应该使用
System.loadLibrary("libSONAME");
, but just System.loadLibrary("SONAME");
,但只是
System.loadLibrary("SONAME");
. 。
Then, just build the project and everything was OK. 然后,只需构建项目,一切都很好。
Thank you all for helping. 谢谢大家的帮助。 I hope this will save time to someone else.
我希望这会节省别人的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.