基于CMake Android NDK学习
网上很多NDK开发入门都是基于Ecplise,ndk-build方式的开发教程,本文主要介绍在现有项目中用CMake脚本方式添加C/C++代码。
Android NDK 安装教程参考官网
官网开发教程
NDK开发教程
一.创建源文件
- IDE左侧,切换项目为Project试图
- 导航到src,右键点击main目录,选择New > Directory
- 在目录中给文件夹取名(官网取名cpp,随便取)并点击OK
- 右键点击上一步创建目录,然后选择New > C/C++ Source File
- 为源文件输入一个名称,如native-lib(爱咋取咋取),然后点击OK
二.创建CMake脚本
右键点击app,选择New > File,创建名为CMakeLists.txt作为文件名并点击OK(名字不能看心情💢发挥了)
接下来我们添加CMake以下命令,开始构建脚本(其他相关操作配置,,根据相关需求参考官方文档)
# 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} )
三.使用Android GUI关联CMake
- 左侧IDE打开Project选择app模块
- 右键app文件夹,从菜单中选择Link C++ Project with Gradle
- 在弹框中,Build System选择CMake方式,Path选择你的CMakeLists.txt文件,点击OK等待gradle构建完成
四.开始在java中定义native方法
- 定义native方法
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();
}
- 开始写native-lib.cpp文件
开始编写之前我们需要了解一下签名格式:
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 | (参数类型...)返回值类型 |
<font color=red>注:char类型参数要在前面加个L,如L'a',java
中字符在Unicode是双字节的,而C++中的字符是单字节,需要加宽</font>
#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);//调用方法
}
- 加载load lib
我是直接放在我的入口Activity里面,直接静态代码块加载lib
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();
}
}