Android JNI exception handling

I need exception handling implemented in JNI code. I am not good at jni and cannot find a good example. Therefore, please provide a complete example of this.

This is what I do:

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    jint result = -1;
    g_JavaVM = vm;

    if (vm->GetEnv((void **) &envLocal, JNI_VERSION_1_6) != JNI_OK) {

        return -1;
    }

    jclass clazz;
    **clazz = envLocal->FindClass("com/graphics/myclass/MyClass");**
    if (clazz == NULL)
        __android_log_print(ANDROID_LOG_ERROR, "MyClass",
                "clazz value is null");
    g_clazz = (jclass) envLocal->NewGlobalRef(clazz);

    // STEP 3/3 : Delete the no longer needed local reference
    envLocal->DeleteLocalRef(clazz);
    result = JNI_VERSION_1_6;
    return result;
}

Now I need if this MyClass is not available (because the application developer does not have the corresponding jar file), then there should not be any application crashes. JNI_OnLoad will be called when the library is loaded using System.LoadLibrary ("libmyclass.so") and this class is "com / graphics / myclass / MyClass".

Currently, if this bank is not included in the application, this leads to a crash and application with the exception below.

F/art     (14708): sart/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception 'java.lang.ClassNotFoundException' thrown in unknown throw location
F/art     (14708): sart/runtime/check_jni.cc:65]     in call to NewGlobalRef
F/art     (14708): sart/runtime/check_jni.cc:65]     from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String)
F/art     (14708): sart/runtime/check_jni.cc:65] "main" prio=5 tid=1 Runnable

, -, JNI_OnLoad, . java.

, , "clazz = envLocal- > FindClass (" com/graphics/systemOp/SystemOp "); JNI_OnLoad.

- (), . . , .

+4
1

jni-, :

bool checkExc(JNIEnv* env) {
 if(env->ExceptionCheck()) {
  env->ExceptionDescribe(); // writes to logcat
  env->ExceptionClear();
  return true;
 }
 return false;
}
+2

Source: https://habr.com/ru/post/1616350/


All Articles