Call Method for NDK development and call for ndk Development
As described in the access domain of NDK development, Java methods are classified into two types: instance methods and static methods. JNI provides functions to access two types of methods. Let's take a look at how to access methods in Java in C.
Our MainActivity has two methods:
private String instanceMethod(){ return "Instance Method"; } private static String staticMethod(){ return "static Method"; }
So how can we access these two methods in C?
The procedure is the same as the access domain:
1. Get the class through Object Reference
2. Obtain the method ID through the class
3. Call a method by method ID
Follow the steps below
1. Get the class through Object Reference
clazz = (*env)->GetObjectClass(env, thiz);
2. Obtain the method ID through the class
JNI provides two methods by using method ID. You can obtain the method ID using the class Object of the given instance, and use the GetMethodID function to obtain the method ID of the instance method. Like the method for obtaining the field ID, the last parameter of the two functions represents the method descriptor, and they represent the method signature in Java. (To improve application performance, we can cache the method ID)
instanceMethodID = (*env)->GetMethodID(env, clazz, "instanceMethod", "()Ljava/lang/String;"); staticMethodID = (*env)->GetStaticMethodID(env, clazz, "staticMethod", "()Ljava/lang/String;");
3. Call a method by method ID
instanceMethodResult = (*env)->CallObjectMethod(env,thiz,instanceMethodID); staticMethodResult = (*env)->CallStaticObjectMethod(env,clazz,staticMethodID);
To check whether the call is successful, we need to print the call result:
const jbyte* str1; const jbyte* str2; str1 = (*env)->GetStringUTFChars(env, instanceMethodResult, 0); str2 = (*env)->GetStringUTFChars(env, staticMethodResult, 0); LOGI("the string is :%s", str1); LOGI("the string is :%s", str2);
The complete code is as follows:
Void Merge (JNIEnv * env, jobject thiz) {jclass clazz; jmethodID merge; jmethodID staticMethodID; jstring comment; jstring staticMethodResult; clazz = (* env)-> GetObjectClass (env, thiz); // clazz = (* env)-> FindClass (env, "com/example/jni/JavaClass"); instanceMethodID = (* env)-> GetMethodID (env, clazz, "instanceMethod", "() Ljava/lang/String;"); staticMethodID = (* env)-> GetStaticMethodID (env, clazz, "staticMethod ","() ljava/lang/String; "); encoding = (* env)-> CallObjectMethod (env, thiz, algorithm); staticMethodResult = (* env)-> CallStaticObjectMethod (env, clazz, staticMethodID); // convert jstring to C string and print const jbyte * str1; const jbyte * str2; str1 = (* env)-> GetStringUTFChars (env, instanceMethodResult, 0 ); str2 = (* env)-> GetStringUTFChars (env, staticMethodResult, 0); LOGI ("the string is: % s", str1); LOGI ("the string is: % s ", str2 );}
Print result:
The conversion between Java and C is expensive. In actual development, we should try our best to avoid this situation.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. I am very grateful if you have any mistakes.