Android JNI Java calls the C + + layer

Source: Internet
Author: User

Summary: Reprint please declare: the original translation from: Http://www.cnblogs.com/xiezie/p/5929996.html A, Java calls the development of local functions steps: 1. The class that writes the local method (which can be said to describe the local method):

2. Compile this class to generate a. class file

There are a number of ways to do this under Android studio:

    • Click Make Project

    • Clean Project + Rebuild Project

    • Run or debug apps

3. Use the Javah command to generate the. h header file for the. class file

In Android Studio, you can open a command terminal and switch the directory to App/build/intermediates/classes/debug (the storage path of the compiled. class file):

commands on the terminal:

CD App/build/intermediates/classes/debug

Click Enter, and then type the package + the class name that you want to >javah-jni the class, as in the following example:

Javah-jni COM.X.MP4PLAYER.TESTNDK

4. Create the Jni folder under Src/main and copy the generated. h file to this directory

5. Create a. cpp file with the same name (used to write C + + code)

Generated. h File code:

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class COM_X_MP4PLAYER_TESTNDK*/#ifndef _INCLUDED_COM_X_MP4PLAYER_TESTNDK#define_included_com_x_mp4player_testndk#ifdef __cplusplusextern "C" {#endif/** CLASS:COM_X_MP4PLAYER_TESTNDK * method:getstringfromnative * Signature: () I*/jniexport jint jnicall java_com_x_mp4player_testndk_getstringfromnative (jnienv*, Jclass); #ifdef __cplusplus}#endif#endif

Write the. cpp file code:

#include <jni.h><com_x_mp4player_TestNdk.h>jniexport jint jnicall java_com_x_mp4player_ Testndk_getstringfromnative        *env, Jclass CLS) {    return1;}
    • To import the generated header files under the. cpp file: #include <com_x_mp4player_TestNdk.h>
    • The. h file and the. cpp file have the same name, and their method names are consistent
6. Add the NDK module under Build.gradle

    • The module name is the same as the name of the link library file imported by the class where the local method resides, as follows
    Static {        system.loadlibrary ("Myjni");   Import the generated link library file    }

7. Compile and run
    • If an error occurs, try creating an empty util.c file under the Jni folder, or debugging yourself

Android JNI Java calls the C + + layer

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.