我试图将结果返回给回调方法
onResponse
。稍后会用更复杂的内容替换"5"。我在StackOverflow上看到了类似的问题,但迄今为止没有任何改变似乎有所帮助。也许我有逻辑错误?
我的JNI(已更新#2):
void cec_debug_msg(JNIEnv *env, cec_rx_message_t* msg) {
jclass cls = (*env)->FindClass(env, "com/example/utils/CECUtils");
if(!cls) {
LOGE("Could not find the CEC class.");
} else {
jmethodID methodid = (*env)->GetStaticMethodID(env, cls, "onResponse", "(I)V");
if(!methodid) {
// Code always reaches this point, never finding the method
LOGE("Could not find the callback method.");
} else {
LOGV("Called static void method.");
(*env)->CallStaticVoidMethod(env, cls, methodid, 5);
}
}
}
这段代码包含在一个方法中,该方法从这个函数调用:
JNIEXPORT int JNICALL Java_com_example_utils_CECUtils_startListening(JNIEnv *env, jclass cls) {
...
cec_debug_msg(env, &rx_msg);
...
}
我的Java(更新#1):
public class CECUtils {
static {
System.loadLibrary("cecutils");
}
public static native int startListening();
...
public static void onResponse(int opcode) {
Utils.log("CEC", "From JNI!");
}
}
签名检查:
javap -s -p CECUtils
public static void onResponse(int);
签名:(I)V
解决方案:检查ProGuard!
-keep public class com.example.utils.CECUtils {
*;
}
Caused by: java.lang.NoSuchMethodError: no non-static method "Lcom/domain/path/to/MyClass;.javaFuncCalledFromJNI(Ljava/lang/String;)V"
。 - Al.