I am very new to JNI and I am trying to figure out how certain things work before I port my C++ iOS code to it. I was successful in getting one of the NDK samples working in Android studio and I can see how Java is able to call C++ functions.
I've been searching around and taking chunks of code, but I haven't been able to get it to work in my specific implementation.
Just to test how things worked I set up a simple text log function in java, and I am trying to call it from my native code but I've run into issues.
Here is my Java function:
public static void log(String s){
Log.d("Native", s);
}
And C++:
void Log(std::string s){
JNIEnv *env;
g_JavaVM->GetEnv((void**)&env, JNI_VERSION_1_6);
jstring jstr1 = env->NewStringUTF(s.c_str());
jclass clazz = env->FindClass("com/android/gl2jni/GL2JNILib");
jmethodID mid = env->GetStaticMethodID(clazz, "log", "(Ljava/lang/String;)V");
jobject obj = env->CallStaticObjectMethod(clazz, mid, jstr1);
}
From what I've seen with different examples this should work, but it throws an error:
29835-29849/com.android.gl2jni A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 29849 (Thread-17371)
Am I missing something?
EDIT:
I've changed it to GetStaticMethodID. But after logging the progress of the function I've found out that the line that fails is:
g_JavaVM->GetEnv((void**)&env, JNI_VERSION_1_6);
Which I figure is because g_JavaVM is set as static JavaVM* g_JavaVM = NULL;
and then never touched again. I'm guessing I need to set this variable, but how?