1、新建项目NDKDemo3
2、新建com.example.shixm.ndkdemo3.MyNdk.java
3、右键main文件夹,New->Folder->JNI Folder
4、MainActivity中调用getMyString
TextView textView = (TextView) findViewById(R.id.textView);
MyNdk myNdk = new MyNdk();
textView.setText(myNdk.getMyString());
5、打开Module: app build.gradle,在defaultConfig中添加
android {
xxxxx
defaultConfig {
xxxxx
ndk {
moduleName "MyLibrary"
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
}
}
6、点击File->Setting->Tools->External Tools,点击“+”,添加如下:
注意:windows用ndk-build.cmd
,MAC/Linux用ndk-build
7、右键com.example.shixm.ndkdemo3.MyNdk.java,NDK->java.h,可以看到控制台,同时jni文件夹下生成了文件com_example_shixm_ndkdemo4_MyNdk.h
8、jni目录下新建MyLibrary.cpp文件,并编辑
9、jni目录下分别新建Android.mk与Application.mk
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyLibrary
LOCAL_SRC_FILES := MyLibrary.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_MODULES := MyLibrary
APP_ABI := all
10、右键jni文件夹,ndk->ndk-build,此时控制台
11、运行项目,至此操作完毕。