网上不少NDK开发入门都是基于Ecplise,ndk-build方式的开发教程,本文主要介绍在现有项目中用CMake脚本方式添加C/C++代码。java
右键点击app,选择New > File,建立名为CMakeLists.txt做为文件名并点击OK(名字不能看心情💢发挥了)
接下来咱们添加CMake如下命令,开始构建脚本(其余相关操做配置,,根据相关需求参考官方文档)android
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
#定义Cmake 版本号,每一个版本新增功能,参考CMake官方文档
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
#定义library名字(生成的so库名:lib库名称.so)和定义library相对路径
add_library( # Specifies 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/main/cpp/native-lib.cpp )
#引用库
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
# 关联相关lib
target_link_libraries( # Specifies the target library.
native-lib
# Links the log library to the target library.
${log-lib} )
复制代码
public class NativeDemo {
//静态native方法
public static native String helloFromJNI();
}
//非静态native方法
public class Hello {
public int property;
public int function(int foo, Date date, int[] arr) {
System.out.println("function");
Log.d("Yxjie","jni输入数字:"+foo);
return foo;
}
public boolean func(int a ,double b,char c){
return true;
}
public native void test();
}
复制代码
java类型 | 相应签名格式 |
---|---|
boolean | Z |
byte | B |
char | C |
short | S |
int | I |
long | L |
float | F |
double | D |
void | V |
object | L 用/分隔包完整类型,如:Ljava/lang/String |
Array | [ 签名 ,如:[I |
Method | (参数类型...)返回值类型 |
注:char类型参数要在前面加个L,如L'a',java 中字符在Unicode是双字节的,而C++中的字符是单字节,须要加宽编程
#include "jni.h"
extern "C"
JNIEXPORT jstring JNICALL Java_com_example_ndktest_NativeDemo_helloFromJNI(JNIEnv *env, jclass jclz) {
//静态native方法 须要两个参数JNIEnv,以及jclass
return env->NewStringUTF("Hello From JNI");
}
extern "C"
JNIEXPORT void Java_com_example_ndktest_Hello_test(JNIEnv *env, jobject obj) {
//非静态方法须要JNIEnv以及jobject(java中Hello,class对象引用)
jclass hello_clazz = env->GetObjectClass(obj);//获取java端 hello.class
jfieldID fieldId_prop = env->GetFieldID(hello_clazz, "property", "I");//获取属性
jint prop_int = env->GetIntField(hello_clazz, fieldId_prop);//获取java prop默认值
jmethodID methodId_function = env->GetMethodID(hello_clazz, "function",
"(ILjava/util/Date;[I)I");//获取对应方法
//批量传入参数
jmethodID methodId_func = env->GetMethodID(hello_clazz, "func", "(IDC)Z");
jvalue *args = new jvalue[3];
args[0].i = 10L;
args[1].d = 3.14;
args[3].c = L'j';//Java 中字符双字节,C++字符为单子接须要编程器宽字节
env->CallBooleanMethod(hello_clazz, methodId_func, args);
delete[] args;
//直接传值
env->SetIntField(hello_clazz, fieldId_prop, 100L);//设置java类中props的值
env->CallIntMethod(obj, methodId_function, 11L, NULL, NULL);//调用方法
}
复制代码
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt=findViewById(R.id.txt);
txt.setText(NativeDemo.helloFromJNI());
Hello hello=new Hello();
hello.test();
}
}
复制代码