问题
I'm trying to call a java static method from a detached thread in c++ with android's NDK.
So far I've :
JNIEnv *env =AttachJava();
jclass cls2 = env->FindClass("com/actvt/showdown/pluggin/Utils"); // try to find the class
//jmethodID mid2 = env->GetStaticMethodID(cls2, "AddSound", "(Landroid/app/Activity;Ljava/lang/String;I)V"); // find method
jmethodID mid = env->GetStaticMethodID(cls2, "addFrame", "(Landroid/app/Activity;JIIII)V"); // find method
jclass dataClass = env->FindClass("android/app/Activity");
jobject javaClassRef = env->AllocObject(dataClass);
env->CallStaticVoidMethod(cls2, mid,javaClassRef,(long)frame,0,footageIndex,frameNumber,width,height);
And I'm attaching this thread to the jvm with the called AttachJava wich is :
JNIEnv* Wrapper::AttachJava()
{
JNIEnv* myNewEnv;
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6; // choose your JNI version
args.name = nullptr; // you might want to give the java thread a name
args.group = nullptr; // you might want to assign the java thread to a ThreadGroup
Wrapper::jvm->AttachCurrentThread(&myNewEnv, &args);
return myNewEnv;
}
And I'm having the following error at the runtime :
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "com.actvt.showdown.pluggin.Utils" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib, /vendor/lib, /system/lib, /vendor/lib]]
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
06-14 10:04:31.548 32423-484/com.learnopengles.airhockey A/art: art/runtime/java_vm_ext.cc:470] at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
If you need anything more I'm at your disposals
Regards,
回答1:
The solution to my problem was to keep a reference to all the classes I will need in my different threads at the begginning of the ndk call before starting my threads and to use them inside those threads. I could also gave them to the thread as parameters.
Found it out thans to pskink and according to this documentation : https://developer.android.com/training/articles/perf-jni.html#faq_FindClass
来源:https://stackoverflow.com/questions/44539053/calling-java-method-from-different-thread-in-ndk