First of all, let me list the best result I could fetch. jni call java method which take a custom java interface as parameter
This does not answer mine. Let me explain my problem. I want to make a call to NDK as follows.
(1)Java -> (2)CPP -> (3)C (new thread) -> (4)CPP -> (5)Java
Code is below.
(1) Java
public interface Callback<T> {
void success(T result);
}
private native void jniUploadAsync(String imagePath, Callback<String> callback);
jniUploadAsync(file.getAbsolutePath(), new Callback<String>() {
@Override
public void success(final String result) {
Log.v("MyClass: result:: ", result);
}
});
(2) CPP
static JavaVM *jvm;
void imageUploadCallback(char *json, void *completionCallback) {
JNIEnv *env;
jint rs = jvm->AttachCurrentThread(&env, NULL);//create JNIEnv from JavaVM
jclass cbClass = env->FindClass("org/winster/test/Callback");
jmethodID method = env->GetMethodID(cbClass, "success", "(Ljava/lang/String;)V");
env->CallVoidMethod(static_cast<jobject>(completionCallback), method, "abcd");
}
void Java_org_winster_test_MyClass_jniUploadAsync(JNIEnv * env, jobject obj, jstring imagePath, jobject completionCallback) {
jint rs = env->GetJavaVM(&jvm); //Cache JavaVM here
CallMyCMethod((char *)filePath, &imageUploadCallback, &completionCallback);
}
(3) C
CallMyCMethod() //please assume that it works. The reason I need void* as the type for completionCallback is because, in ObjC implementation I use this
(4) CPP
//Call comes back to imageUploadCallback()
(5) Java
//I expect this Log.v("MyClass: result:: ", result); to be executed
Please note that, this is not a basic question about how to call Java from C++. The 2 specific points I want to resolve is, how to call the "callback" and how to invoke a method in a Java Interface implementation. I have done this for Obj-C where it is straight forward.