Android Studio - 包含并使用.so库 [英] Android Studio - include and consume .so library
问题描述
我需要在我的android项目中使用一些本地库(.so)。根据StackOverflow中关于这个主题的一些答案,我在 app / src / main
中创建了一个 jniLibs
文件夹,有我的文件:
pre $ $ $ c $ ar $ b $ armeabi / my_lib.so $ b $ armeabi-v7a / my_lib.so
x86 / my_lib.so
然后,在我的活动类中使用:
static {
System.loadLibrary(my_lib);
}
但是当我运行应用程序时, UnsatisfiedLinkError
异常生成。如果这很重要,那么我没有一个Android.mk文件,并且在我的 gradle
文件中没有改变任何与此有关的事情。所以,我唯一想做的就是在 jniLibs
中复制粘贴我的 .so
文件并写上面的代码在我的活动中。那么这个问题的原因是什么?我缺少什么?
编辑
这是我的gradle: p>
apply plugin:'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion 23.0.3
compileOptions.encoding ='ISO-8859-1'
defaultConfig {
applicationIdmy.package
minSdkVersion 4
targetSdkVersion 4
ndk {
moduleNamemy_so_lib
}
}
sourceSets {
main {
jni.srcDirs = [libs]
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard- ','proguard-rules.txt'
}
调试{
可调试真实
}
}
split {
abi {
e nable true
reset()
include'x86','armeabi-v7a','mips','armeabi'
universalApk false
}
}
}
谢谢大家的帮助,愚蠢的问题。当我在 jniLibs
下导入我的 .so
文件时,它们被命名为 libSONAME.so
。在这些代码行中:
static {
System.loadLibrary(libSONAME);
}
我们不应该使用 System.loadLibrary(libSONAME );
,但只是 System.loadLibrary(SONAME);
。
然后,建立这个项目,一切正常。
谢谢大家的帮助。我希望这会节省时间给其他人。
I need to use some native libraries(.so) in my android project. According to some answers here in StackOverflow about this topic, I created a jniLibs
folder in app/src/main
and put there my files:
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. 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. 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 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
}
}
}
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
. In these lines of code:
static {
System.loadLibrary("libSONAME");
}
we should not use System.loadLibrary("libSONAME");
, but just 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.
这篇关于Android Studio - 包含并使用.so库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!