[英]Unsatisfiedlinkerror: Unable to load custom NDK JNI library in Nfc system app
I have written a small C program using JNI which I would like to call from inside the NFC system app, specifically the NfcDispatcher.java class. 我使用JNI编写了一个小型C程序,我想从NFC系统应用程序内部调用它,特别是NfcDispatcher.java类。 I have done the following so far: 到目前为止,我已经完成了以下工作:
Created a /jni directory inside the /AOSP/packages/apps/Nfc/ where I have written the following JNI code. 在/ AOSP / packages / apps / Nfc /中创建了一个/ jni目录,我在其中编写了以下JNI代码。 Nfc/jni/ dir has 2 files, viz mycustomlib.c and Android.mk which are as follows Nfc / jni / dir有2个文件,分别是mycustomlib.c和Android.mk,如下所示
mycustomlib.c at /AOSP/packages/apps/Nfc/jni/mycustomlib.c mycustomlib.c在/AOSP/packages/apps/Nfc/jni/mycustomlib.c
#include <string.h>
#include <jni.h>
jstring Java_com_android_nfc_NfcDispatcher_gettagkey( JNIEnv* env, jobject thiz, jstring tagKey )
{
// do something
return tagKey;
}
Android.mk at /AOSP/packages/apps/Nfc/jni/Android.mk 位于/AOSP/packages/apps/Nfc/jni/Android.mk的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mycustomlib
LOCAL_SRC_FILES := mycustomlib.c
include $(BUILD_SHARED_LIBRARY)
I am calling the native method "gettagkey" from inside the NfcDispatcher.java file as follows. 我从NfcDispatcher.java文件内部调用本地方法“ gettagkey”,如下所示。
NfcDispatcher.java at /AOSP/packages/apps/Nfc/src/com/android/nfc/NfcDispatcher.java NfcDispatcher.java在/AOSP/packages/apps/Nfc/src/com/android/nfc/NfcDispatcher.java
public class NfcDispatcher {
..
..
public static native String gettagkey(String tagKey);
..
static class DispatchInfo {
..
}
public boolean dispatchTag(Tag tag) {
..
..
Log.d(TAG, "NFC Key Tag from C code : " + gettagkey(sb.toString()));
..
}
..
static {
System.loadLibrary("mycustomlib");
}
} }
Then we compile the C code using ndk-build -C path_to_c_code and then we did a "make -j32" to compile the code changed in the Nfc system app and then we flash the new system.img to the Nexus 7 device. 然后,我们使用ndk-build -C path_to_c_code编译C代码,然后执行“ make -j32”编译在Nfc系统应用中更改的代码,然后将新的system.img闪存到Nexus 7设备。 The OS boot properly but we get the following error: 操作系统可以正常启动,但是出现以下错误:
W/dalvikvm( 767): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/android/nfc/NfcDispatcher;
E/AndroidRuntime( 767): at com.android.nfc.NfcDispatcher.<clinit>(NfcDispatcher.java:571)
E/AndroidRuntime( 767): at com.android.nfc.NfcService.<init>(NfcService.java:390)
E/AndroidRuntime( 767): at com.android.nfc.NfcApplication.onCreate(NfcApplication.java:43)
I have read all related question but I am still not sure what's happening. 我已经阅读了所有相关问题,但仍不确定发生了什么。 Does anyone have any clue? 有人有任何线索吗? Thanks! 谢谢!
I have the feeling that the native library is not being loaded before the native call to the native method. 我觉得在对本机方法进行本机调用之前未加载本机库。
I would try to replace the gettagkey body, with the library load call, and then call the native method. 我会尝试用库加载调用替换gettagkey主体,然后调用本机方法。 Something like: 就像是:
public static String gettagkey(String tagKey){
System.loadLibrary("mycustomlib");
gettagkeyNative(tagKey);
}
public static native String gettagkeyNative(String tagKey);
Don't forget to modify the native part: 不要忘记修改本机部分:
#include <string.h>
#include <jni.h>
jstring Java_com_android_nfc_NfcDispatcher_gettagkeyNative( JNIEnv* env, jobject thiz, jstring tagKey)
{
// do something
return tagKey;
}
This solution would work, unless the system call to the native method requires that the gettagkey method is native. 除非系统调用native方法要求gettagkey方法是native方法,否则此解决方案将起作用。
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.