第三方库的JNI GetMethodID返回NULL

时间:2015-07-13 06:53:21

标签: android-ndk java-native-interface

我正在尝试将构造函数添加到第三方库类,例如“android.util.Size”。但由于某种原因,当我尝试从android核心库查询字段或方法ID时,我返回NULL。任何想法为什么?

#include <stdio.h>
#include <android/log.h>
#include <jni.h>

#define LOG(LOG_TAG,...)  ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGE(LOG_TAG,...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define TAG "TEST"

#define CLS_SIZE "android/util/Size"
int register_SizeStruct(JNIEnv* env){
    LOG(TAG, "Registering Size");
    jclass clazz   = (jclass) env->NewGlobalRef(env->FindClass(CLS_SIZE));
    jmethodID init = env->GetMethodID(clazz, "<init>", " (II)V");

    if (clazz == NULL) LOGE(TAG, "Couldn't Register class " CLS_SIZE);
    if (init  == NULL) LOGE(TAG, "Couldn't Register Contructor for " CLS_SIZE);

    if((clazz  == NULL) || (init   == NULL)) {
        LOGE(TAG, "ERROR-Couldn't register Size struct");
        return -1;
    }
    return 0;
}

jint JNI_OnLoad(JavaVM* aVm, void* aReserved)
{
    JNIEnv* env;
    if (aVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK){
        LOGE (TAG, "Failed to get JNI_1_6 environment");
        return -1;
    }
    register_SizeStruct(env);
    return JNI_VERSION_1_6;
}

这是日志

I/TEST( 2532): Registering Size
E/TEST( 2532): Couldn't Register Contructor for android/util/Size
E/TEST( 2532): ERROR-Couldn't register Size struct
D/AndroidRuntime( 2532): Shutting down VM
E/AndroidRuntime( 2532): FATAL EXCEPTION: main
E/AndroidRuntime( 2532): Process: com.example.testapp, PID: 2532
E/AndroidRuntime( 2532): java.lang.NoSuchMethodError: no non-static method "Landroid/util/Size;.<init> (II)V"
E/AndroidRuntime( 2532):    at java.lang.Runtime.nativeLoad(Native Method)
E/AndroidRuntime( 2532):    at java.lang.Runtime.doLoad(Runtime.java:428)
E/AndroidRuntime( 2532):    at java.lang.Runtime.loadLibrary(Runtime.java:369)
E/AndroidRuntime( 2532):    at java.lang.System.loadLibrary(System.java:989)
E/AndroidRuntime( 2532):    at com.example.testapp.MainActivity.<clinit>(MainActivity.java:38)
E/AndroidRuntime( 2532):    at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime( 2532):    at java.lang.Class.newInstance(Class.java:1572)
E/AndroidRuntime( 2532):    at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
E/AndroidRuntime( 2532):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
E/AndroidRuntime( 2532):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
E/AndroidRuntime( 2532):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime( 2532):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime( 2532):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2532):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 2532):    at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime( 2532):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 2532):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 2532):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

1 个答案:

答案 0 :(得分:1)

您的签名字符串中有一个不应该存在的空格:

jmethodID init = env->GetMethodID(clazz, "<init>", " (II)V");   
                                                    ^-- RIGHT THERE

删除该空格。此外,请确保您在与21级或更高级别的API相对应的Android版本上运行此版本,因为那里添加了android.util.Size