Reprint please declare: Original translation from: http://www.cnblogs.com/xiezie/p/5930503.html
Android JNI Java and C + + objects establish symmetric Association 1. Java objects hold pointers to C + + objects
- Create a variable of type int (such as int mobj) in the Java class to store pointers to C + + objects
- In a local method that creates a C + + object (such as Mycplusobj), a pointer to a C + + object is stored in a Java variable (mobj)
jniexport void Jnicall Java_nativemethod ( jnienv *env, Jobject thiz) {mycplusobj *obj = new Mycplusobj (); Jclass clazz = (jclass) (*env). Getobjectclass (Thiz); Jfieldid FID = (jfieldid) (*env). Getfieldid (Clazz, " mobj ", " i ); ( *env). Setintfield (Thiz, FID, (jint) obj);}
2.c++ object holds a reference to a Java object
- Create a variable of type jint (such as Jint mobj) in a C + + class to store a reference to a Java object
- Storing a reference to a Java object when creating a C + + object
Jobject gthiz = (jobject) env->newglobalref (thiz); // thiz for Java objects (*obj). Javaobj = (jint) gthiz;
Android JNI Java is symmetric associated with C + + objects (JNI optimized design to ensure the stability of JNI calls)