When programming with the Android NDK, you use the. so file, so files are written in the C language. When I call the Java class in the C file, the first call is fine, but the second call fails. Internet search a lot of information, presumably because in the JNI, using the pointer to a Java object, because of the Android garbage collection mechanism (garbage Collector), if the Java object is recycled, then the pointer to the object will be empty or not exist, This suggests that the JNI error:accessed stale (stale, backward) local reference probably means that the variable is no longer there. Therefore, to solve this problem, it is required to define the Java object as static, so as to avoid being recycled (after Android4.0, the static variables will be recycled, but the probability is small), resulting in the creation of errors. Here is my example:
Jobject Mtestprovider;
Mtestprovider = (*jnienv)->newobject (jnienv, Obj_class, construction_id);
After modification:
Static Jobject Mtestprovider;
Jobject obj = jobject obj = (*jnienv)->newobject (jnienv,obj_class,construction_id);
Mtestprovider = (jobject) (*jnienv)->newglobalref (jnienv,obj);
The above is only personal experience, hope to solve your problem!
JNI ERROR (App bug): accessed stale local reference 0xbc00021