转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5929996.html html
在Android Studio下有不少方法:java
在Android Studio中,能够打开命令终端并将目录切换到app/build/intermediates/classes/debug(编译生成的.class文件的存放路径)下:app
终端上的命令:函数
cd app/build/intermediates/classes/debug
点击回车,再输入要>javah -jni 该类所在的包+该类名称,示例以下:gradle
javah -jni com.x.mp4player.TestNdk
生成的.h文件代码:ui
/* DO NOT EDIT THIS FILE - it is 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 __cplusplus extern "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
编写的.cpp文件代码:spa
#include <jni.h> #include <com_x_mp4player_TestNdk.h> JNIEXPORT jint JNICALL Java_com_x_mp4player_TestNdk_getStringFromNative (JNIEnv *env, jclass cls) { return 1; }
static { System.loadLibrary("MyJni");//导入生成的连接库文件 }