简体   繁体   中英

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. 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. Nfc/jni/ dir has 2 files, viz mycustomlib.c and Android.mk which are as follows

mycustomlib.c at /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

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 at /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. 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. 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.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM