Calling java method from different thread in ndk

倖福魔咒の 提交于 2021-01-28 07:08:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!