Search code examples
androidandroid-ndkjava-native-interface

Pass jobjectArray from JNI to Java crash on Android API 21/22 but work on API 19


I use follow code to handle JNI array in Android and JNI.

However I found return "jobjectArray" cannot complete on API 21/22 (Android 5.0) but works on API 19. (Android 4.4) (cannot complete mean it return on JNI part but it hang and no response on Java)

Here is the pseudo code I try to implement in my Android App.

jobjectArray Java_com_test_Simplejni(JNIEnv* env, jobject thisObj)
    jclass localClass = env->FindClass("java/lang/Object");
    jclass objClass = reinterpret_cast<jclass>(env->NewGlobalRef(localClass));
    args = env->NewObjectArray(len, objClass, 0);
    return args;
}

The java part function as follow:

     String[] Simplejni();

The error message as follow:

JNI DETECTED ERROR IN APPLICATION: attempt to return an instance of java.lang.Object[] from com.test.Simplejni                

Please advise any suggestion how to investigate this issue, thank you.

Update: 20150427

  • I try to simpreturn empty jobjectArray which works in Android 4.4 but failed in Android 5.0 (with the same code)
  • My IDE is Android Studio

Solution

  • I found a way to solve this: you just replace java/lang/Object with your java object class on API 21+, for example, jclass localClass = env->FindClass("com/example/YourLocalClass");, From the logs, we can know the object class is not the instance of yourJavaLocalObject class.