I’m stuck with this, I need to call a Java Function from c/c++.
In the examples and tutorials i only see a java app calling a c method, and in this same method calling another java method, but what I want to do is to call a java method from any part of the code. This is what I have:
static JNIEnv mEnv;
static jclass mClassAndroidActivity;
static mMethodSayHello;
JNIEXPORT void JNICALL JNI_FUNCTION(AndroidActivity_nativeInit)(JNIEnv* env, jobject obj, int width, int height)
{
mEnv = env;
jclass cls = (*env)->GetObjectClass(env, obj);
mClassAndroidActivity = (*env)->NewGlobalRef(env, cls);
mMethodSayHello = (*env)->GetMethodID (env, mClassAndroidActivity, "SayHello", "(Ljava/lang/String;)V");
}
//this method is called from a cpp
void nativeSayHello(char* msg)
{
jstring string = (*mEnv)->NewStringUTF(mEnv, msg);
(*mEnv)->CallVoidMethod(mEnv, mClassAndroidActivity, mMethodSayHello, string);
}
but is always crashing, I’ve tried without the NewGlobalRef, using mEnv instead of env in the JNI_Function, I’ve tried getting the method id from the JNI_OnLoad, but always crashes.
This is the log i get:
02-15 18:09:48.520: W/dalvikvm(27904): JNI WARNING: threadid=1 using env from threadid=0
You can’t reuse
JNIEnvbecause it is specific to the calling thread. To call (non-static) Java method from the native code, you need something like this:This code snippet is not tested. To prevent memory leak, don’t forget to call
nativeRelease()method in your Java code when the reference to the object will not needed any more.See The Java Native Interface documentation for more details.