Mutual calls between Android NDK JNI and Java

1. Jni calls Java code


jni can call methods and member variables in java, so JNIEnv defines a series of methods to help us call methods and member variables in java.


The above are most of the methods that jni calls java classes. If it is a static member variable and static method, you can use GetStaticMethodID, CallStaticObjectMethod, etc. Just add static to the corresponding method in the above table.

An important point in the above is: to obtain the method id of the constructor method, the second parameter of GetMethodID is passed ***"< init >"***. This is a fixed writing method and cannot be changed.

Another important point in the above is: method signature (GetFieldID, the required sig parameter in GetMethodID). This thing needs to be remembered. If you can’t remember it, you can get it through the javap -s -p command. Regarding the javap command Without further ado, here is an example command line:

javap -s -p E:\1_Study_Space\6_JNI\2_JNI\app\build\intermediates\classes\debug\tsw\demo\a2_jni\Student.class The following is an example code for jni to call java class

method

//jni calls java method
public native void jniCallJava();

public class Student {
    public String name;
    public String sex;

    public Student() {

    }

    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_tsw_demo_a2_1jni_TestJni_jniCallJava(JNIEnv *env, jobject instance) {     //------------------Get the object of the Student class through no-parameter construction and call setName at the same time Method to assign value to object ------------------     jobject jobj_student;     jmethodID jmid_tostring;     //1. Get the jclass of java class Student jclass     jcla_student = env->FindClass("tsw/demo /a2_jni/Student");     //2. Get the parameterless constructor id of Student. The second parameter of the construction method is fixed to <init> and cannot be changed; the third parameter is the method signature     jmethodID jmid_student = env->GetMethodID(jcla_student, "<init>", "()V");     //3. Get the student object     jobj_student = env->NewObject(jcla_student, jmid_student);     //4. Get the setName method id of the Student class. The second parameter is the method name; the third parameter is the method signature.










    jmethodID jmid_setname = env->GetMethodID(jcla_student, "setName", "(Ljava/lang/String;)V");
    //5. Call the setName method of the Student class. Because Student's setName is of void type, CallVoidMethod is used.
    //What type does the Java class method return? Use the Call<Type>Method method of the corresponding type. Call<Type>Method represents CallVoidMethod, CallLongMethod, CallObjectMethod, etc.
    env->CallVoidMethod(jobj_student, jmid_setname, env->NewStringUTF("zhangsan"));

    //6. Call Student's toString method
    jmid_tostring = env->GetMethodID(jcla_student, "toString", "()Ljava/lang/String;"); jstring j_tostring
    = (jstring) env->CallObjectMethod(jobj_student, jmid_tostring);
    LOGE("setName method assigns value to object: %s", env->GetStringUTFChars(j_tostring, JNI_FALSE));

    //-------------Construct with parameters

Guess you like

Origin blog.csdn.net/s_nshine/article/details/132438064