1.NEW一个新的Project
2.app->New->Folder->JNI Folder
3.app/src/main/jni ->New ->Android.mk,Application.mk,hello.cpp
Android.mk:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-module LOCAL_SRC_FILES := hello.cpp include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := armeabi APP_PLATFORM := android-19
hello.cpp:
#include <jni.h> #include "string.h" extern "C" JNIEXPORT jstring JNICALL Java_com_movitech_myapplication_MainActivity_hello(JNIEnv *env, jclass type) { return env->NewStringUTF("hello world From C.ndk-build"); }
4.build.gradle(app)
defaultConfig { ndk { moduleName "hello-module" stl "stlport_static" ldLibs "log" abiFilters "armeabi" } sourceSets{ main{ jni.srcDirs = [] jniLibs.srcDir "src/main/libs" } } }
5.根据cpp文件生成so文件
6. 调用
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView des = findViewById(R.id.des); des.setText(hello()); } public static native String hello(); static { System.loadLibrary("hello-module"); }
7.show