Call Method for NDK development and call for ndk Development

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.