Android NDK Development article: Java and Native code communication (native method declaration and definition and data type)

Source: Internet
Author: User

Java and native code communication involves native method declarations and definitions, data types, reference data type operations, NIO operations, access domains, exception handling, native threads

1. Declaration and definition of native methods

The Declaration and definition of the native method has been said a little bit in the previous article, this time detailed analysis. Based on the Javah auto-generated header file, you can see the declaration and definition of the native method, for example:

    1. Jniexport jstring jnicall java_com_shamoo_helljni_hellojni_stringfromjni (jnienv *, jobject);

Although the native method on Java does not have any parameters, it is the native method of the Jni interface with two parameters. JNIENV * is an interface pointer to the JNI function table, and Jobject is the Java object reference that invokes the method. As can be seen from the above method, the method in Java code in the Com.shamoo.helljni package, class is Hellojni.

JNIENV * Pointers: Native methods use the functions of the virtual machine through the various functions provided by the JNIENV * pointer.

Native methods are called in different ways in C and C + +, for example:

    1. /* C */
    2. Return (*ENV)->newstringutf (env, "Hello World");
    3. /* C + + *
    4. Return Env->newstringutf ("Hello World");

In the C language, JNIEnv is a pointer to the JNINATIVEINTERFACE structure that must be dereferenced. And the first parameter is env, learned C and C + + language know, C language is a process-oriented language, NEWSTRINGUTF is just a function pointer, call the method is not clear the caller, so to pass env, and C + + is not used, because C + + is Object-oriented language, this does not explain

2. Example method and static method

The instance method and static method definition of the native method are also different, the above stringfromjni is the way of the instance method definition, the second parameter binds to the Java object reference. The second parameter of a static method is Jclass, which is bound to a class and belongs to a class method. For example:

      1. Jniexport jstring jnicall java_com_shamoo_helljni_hellojni_stringfromjni (jnienv *, jclass);

Android NDK Development article: Java and Native code communication (native method declaration and definition and data type)

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.