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:
- Clean Project + Rebuild Project
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