I have an Android application which consists of some native threads (not attached to JVM) which need to be able to call methods of a Java object.
The way in which I was intending to do this was to create a JNI function which I call from the relevant Java object which allows me to obtain and cache the required java object method ID's, JNIEnv and object references in a static native data structure so that my native threads can (thread safely) access the required methods (e.g. using (*env)->CallVoidMethod(env, this, JavaMethodID, ...), etc;
I'm not convinced this approach is going to work, since I read that the JNIEnv pointer can't be shared between threads, and that only threads which are attached to the JVM can do this kind of thing...
Is this a viable approach?
JNIEnv
is valid only for the current thread, butJavaVM
is global. So, save a pointer to that, as well as global references to your classes, objects, method IDs, and that will work. – Samuel Audet