Static registration of Android JNI

Source: Internet
Author: User

This is said static registration, so-called static registration, is the native method is directly through the method name of the specified format and the Java side of the Declaration code corresponding to the rules as follows:

Jniexport < return value > Jnicall java_< package path (. Change _) >_< class name >_< method name > (JNIENV *, jobject<, method parameters >)

Suppose the method Staticload () is declared in Hellojni, as follows:

 Package Com.example.zhanghaiqiang.hellojni;  Public class Hellojni {    static{        system.loadlibrary ("main");    }      Public native void staticload ();}

The corresponding native method is named

void Jnicall java_com_example_zhanghaiqiang_hellojni_androidjni_dynamiclog   *, Jobject)

Of course, this rule does not need to rote, the JDK Javah automatically help us do this, CD to the Java directory, execute Javah < package path >.< class name >, the current directory will be generated in the corresponding Natvie class header file, For example, the above Hellojni, the com_example_zhanghaiqiang_hellojni_hellojni.h file content after executing Javah will look like this:

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_example_zhanghaiqiang_hellojni_hellojni*/#ifndef _included_com_example_zhanghaiqiang_hellojni_hellojni#define_included_com_example_zhanghaiqiang_hellojni_hellojni#ifdef __cplusplusextern "C" {#endif/** Class:com_example_zhanghaiqiang_hellojni_hellojni * method:staticlog * Signature: () V*/Jniexportvoidjnicall Java_com_example_zhanghaiqiang_hellojni_hellojni_staticlog (jnienv*, jobject); #ifdef __cplusplus}#endif#endif

The file is then copied to the default JNI directory, copy one and rename the. C or. cpp source file (you can also change the. h file to a source file directly), and then you can start writing the native method.

Static registration of Android JNI

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.