使用gradle实验v0.6.0-beta6的Android不满意的链接 [英] Android Unsatisfied Link using gradle experimental v0.6.0-beta6
问题描述
我试图创建一个简单的项目来测试NDK,其中jnilib使用预建的.so库,但我一直收到UnsatisfiedLink错误:
I was trying to create a simple project to test the NDK in which a jnilib uses a prebuilt .so library but I keep getting an UnsatisfiedLink error:
使用模拟器:
java.lang.UnsatisfiedLinkError: dlopen failed: library "~/AndroidStudioProjects/HelloAndroidJni/app/src/main/jni/libs/dynamic/x86/libadd.so" not found
使用实际设备:
Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1891]: 1675 could not load needed library '~/AndroidStudioProjects/HelloAndroidJni/app/src/main/jni/libs/dynamic/armeabi/libadd.so' for 'libhello-android-jni.so' (load_library[1093]: Library '~/AndroidStudioProjects/HelloAndroidJni/app/src/main/jni/libs/dynamic/armeabi/libadd.so' not found)
.so库具有单个函数"add",该函数将2个数字相加.我使用NDK独立工具链针对不同的ABI进行了编译( doc ):
The .so library has a single function "add" which adds 2 numbers. I compiled it with the NDK standalone toolchain for different ABI (doc):
add.c
#include "add.h"
int add(int x, int y){
return x + y;
}
我的jni(.c文件):
My jni (.c file):
#include <jni.h>
#include "add.h"
JNIEXPORT jint JNICALL
Java_com_example_tomas_helloandroidjni_MainActivity_addNumbersJni(JNIEnv *env, jobject instance,
jint n1, jint n2) {
return add(n1, n2);
}
我的文件结构如下:
And my gradle file (as defined in the experimental gradle guide):
apply plugin: 'com.android.model.application'
model {
repositories {
libs(PrebuiltLibraries) {
libadd{
headers.srcDir "src/main/jni/prebuilts/include"
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("src/main/jni/libs/dynamic/${targetPlatform.getName()}/libadd.so")
}
}
}
}
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
sources {
main {
jni {
dependencies {
library "libadd" linkage "shared"
}
}
}
}
ndk {
moduleName = "hello-android-jni"
debuggable = true
}
defaultConfig.with {
applicationId = "com.example.tomas.helloandroidjni"
minSdkVersion.apiLevel = 15
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file("proguard-android.txt"))
}
}
productFlavors {
create ("x86"){
ndk.abiFilters.add("x86")
}
create("arm"){
ndk.abiFilters.add("armeabi")
}
create("arm7"){
ndk.abiFilters.add("armeabi-v7a")
}
create ("fat"){
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
}
我不确定要看哪里,因为我不断收到此错误...也许我需要添加一个标记或其他内容?
I'm not sure where to look at, as I keep getting this error... Maybe I need to add a flag or something?
提前感谢您的任何建议!
Thanks in advance for any advice!
编辑1.
我尝试使用静态库,它们可以正常工作.结果(添加了静态库),我稍稍更改了文件夹的结构(相应地更改了gradle文件).
I tried with static libraries and they work fine. As a consequence (of adding the static libs) I changed the folders' structure slightly (modifying the gradle file accordingly).
我都在做这件事:
- 实际设备: Samsung GT-I8190L Android 4.1.2,API 16-ARMv7处理器修订版1(v7l)
- 仿真器: Nexus 4 API 23-x86
- Actual device: Samsung GT-I8190L Android 4.1.2, API 16 - ARMv7 Processor rev 1 (v7l)
- Emulator: Nexus 4 API 23 - x86
编辑2.
要编译库,我正在使用 NDK独立版工具链:
To compile the libraries I'm doing it with the NDK Standalone Toolchain:
-
实际设备(ARMv7): SYSROOT = $ NDK/platforms/android-21/arch-arm 和 CC ="$ NDK/toolchains/arm-linux-androideabi -4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc-4.8 --sysroot = $ SYSROOT"
仿真器(x86): SYSROOT = $ NDK/platforms/android-21/arch-x86 和 CC ="/Users/Tomas/Library/Android/android- ndk-r10e/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-gcc-4.8 --sysroot = $ SYSROOT"
Emulator (x86): SYSROOT=$NDK/platforms/android-21/arch-x86 and CC="/Users/Tomas/Library/Android/android-ndk-r10e/toolchains/x86-4.8/prebuilt/darwin-x86_64/bin/i686-linux-android-gcc-4.8 --sysroot=$SYSROOT"
推荐答案
这可能不是预期的本机依赖项起作用的方式,但是 (更新:这是隐式编写的 doc !),即使使用插件0.7.0,gradle也不会将 libadd.so 复制到APK.不过,有一种解决方法.将结界添加到 build.gradle :
This may not be the way these native dependencies were expected to work, but (update: this is implicitly written in the doc!) even with plugin 0.7.0, gradle does not copy libadd.so to the APK. There is a workaround, though; add to build.gradle the enchantment:
model {
android.sources.main {
jniLibs.source.srcDir 'src/main/jni/libs/dynamic'
}
}
仍然,我建议将 libs/dynamic 文件夹移出 jni 源文件夹.
Still, I recommend to move libs/dynamic folder out of the jni sources folder.
这篇关于使用gradle实验v0.6.0-beta6的Android不满意的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!