Android NDK 在使用 ART 调用静态方法的 CallObjectMethod 中崩溃

Android NDK crash in CallObjectMethod calling static method with ART

在 Android 应用程序中,我有一些调用 java 静态方法的 JNI 代码。

jbyteArray response = (jbyteArray)pEnv->CallObjectMethod(handlerClass, mid, jstrServiceUrl, jstrRequest);

ART 环境中的 Android 5 中执行它,我得到一个 check jni 错误:

JNI DETECTED ERROR IN APPLICATION: calling static method byte[] x.y.z(java.lang.String, java.lang.String) with CallObjectMethodV in call to CallObjectMethodV...

我在 Android 4 和 Dalvik 环境中没有收到此错误。

java方法是这个:

public static byte[] z(String serviceURL, String request) 

并且之前是这样绑定的:

jclass handlerClass = pEnv->FindClass("x/y/z");
if (handlerClass == NULL) {
    return -1;
}

mid = pEnv->GetStaticMethodID(handlerClass, "z", "(Ljava/lang/String;Ljava/lang/String;)[B");
if (mid == NULL) {
    return -2;
}

// Construct Strings
jstring jstrServiceUrl = pEnv->NewStringUTF(szServiceURL);
jstring jstrRequest = pEnv->NewStringUTF(szRequest);

我不知道为什么你的代码与 Dalvik 一起工作,但是给 Call<type>Method 的方法 id 必须用 GetMethodID 获得。如果你有一个通过 GetStaticMethodID 获得的方法 id,你应该使用 CallStatic<type>Method.

参见 the JNI functions documentationCall<type>MethodCallStatic<type>Method 的描述。