1.下载安装ndk-bundle
2.gradle.properties中加android.useDeprecatedNdk=true
3.build.gradle(app)中加
android {
defaultConfig {
ndk{
moduleName "ndk_test"
}
}
}
4.写接口文件如
public class NDKTest {
public native static String hello();
static {
System.loadLibrary("ndk_test");
}
}
5.build工程,在./build/intermediates/classes/debug下打开终端,执行javah -jni 包名.NDKTest,编译出h文件
6../src/main下新建jni文件夹,把h文件移过来
7.jni文件夹下新建cpp文件如
#include "com_candy1126xx_opengl01_NDKTest.h"
#include <string.h>
JNIEXPORT jstring JNICALL Java_com_candy1126xx_opengl01_NDKTest_hello
(JNIEnv *env, jclass)
{
return env->NewStringUTF("我是来自NDK的C++");
}
8.在工程中调用NDKTest.hello()