Answer the question
In order to leave comments, you need to log in
Threads in android ndk development?
I am developing an android application using ndk. I read in the book that if a thread is created in the low-level part of the application, this thread must be attached to the JVM (it is clear that in Davlik android, but not the essence) so that you can access the JVM through a pointer, respectively, you need to call the AttachCurrentThread (Jnienv * env ) and, accordingly, use this function to get a pointer to the JVM.
For these purposes, I wrote two methods that attach a stream if it is not attached, and then detach it.
JavaVM *javaVM;
boolean GetJniEnvAndAttach(JNIEnv *jniEnv){
int resault = (javaVM->GetEnv((void **) &jniEnv, JNI_VERSION_1_6));
if (resault == JNI_EDETACHED){
if (javaVM->AttachCurrentThread(&jniEnv,0) == JNI_OK){
LOGI("Thread attach to JVM");
return true;
}
} else {
LOGI("Thread wasn`t attach");
}
return false;
}
}
void Detach(boolean isAttach){
if (isAttach){
javaVM->DetachCurrentThread();
}
}
JNIEXPORT jint JNICALL JNI_OnLoad(
JNIEnv *env) {
JavaVM *jmv = NULL;
env->GetJavaVM(&jmv);
javaVM = jmv;
return JNI_OK;
}
Answer the question
In order to leave comments, you need to log in
Perhaps someone will be interested, but I figured out the problem.
It turned out that I am not very friendly with pointers in C, as an argument it was necessary to pass not just a pointer to jnienv, but a pointer to a pointer, but it didn’t quite work, so I added the local variable localenv * Jnienv inside the GetJniEnvAndAttach method; which I used further inside the function, and before returning from the function assigned it to the pointer passed in the argument: &jnienv = localenv;
boolean GetJniEnvAndAttach(JNIEnv **jniEnv){
JNIEnv *localEnv = NULL;
if (javaVM->GetEnv((void **) &localEnv, JNI_VERSION_1_6) == JNI_EDETACHED){
if (javaVM->AttachCurrentThread(&localEnv,0) == JNI_OK){
*jniEnv = localEnv;
LOGI("Thread attach to JVM");
return true;
}
} else {
*jniEnv = localEnv;
LOGI("Thread wasn`t attach");
return false;
}
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question