轉載請聲明:原文轉自: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");//導入生成的連接庫文件 }