从ndk中的不同线程调用java方法

时间:2017-06-14 08:09:42

标签: android multithreading android-ndk

我正在尝试使用android的NDK从c ++中的分离线程调用java静态方法。

到目前为止我已经:

    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);

我将这个帖子附加到jvm,名为AttachJava,它是:

 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;
}

我在运行时遇到以下错误:

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)

如果您还需要更多我处理的话

此致

1 个答案:

答案 0 :(得分:1)

我的问题的解决方案是在启动我的线程并在这些线程中使用它们之前,在ndk调用开始时保持对我在不同线程中所需的所有类的引用。我也可以将它们作为参数提供给线程。

根据此文档发现它为pskink:https://developer.android.com/training/articles/perf-jni.html#faq_FindClass