一、在app buidlgradle中的android如下代码
externalNativeBuild {
cmake {
path “src/main/cpp/CMakeLists.txt”
version “3.10.2”
}
}
二、如图操作添加ndk版本,点击工程项目中的右键,点击open module sttings,右侧中间添加ndk版本
三、在路径src/main/cpp/CMakeLists.txt 创建cmakelist.txt,添加内容
cmake_minimum_required(VERSION 3.10.2)
#project(“jniPra1”)
#aux_source_directory(dir VAR) 发现一个目录下所有的源代码文件并将列表存储在一个变量中。
aux_source_directory(. src_file_list)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${src_file_list} )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
四、创建一个main.cpp文件,如
#include <jni.h>
#include
extern “C” JNIEXPORT jstring JNICALL
Java_com_iflytek_jnipra1_MainActivity_getNativeString(
JNIEnv* env,
jobject /* this */) {
std::string hello = “happy,jni is easy”;
return env->NewStringUTF(hello.c_str());
}
注意需要将Java_com_iflytek_jnipra1_MainActivity修改自己当前所在的类
五、编译运行测试即可