網上不少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();
}
}
複製代碼