基于CMake Android NDK學(xué)習(xí)

基于CMake Android NDK學(xué)習(xí)

網(wǎng)上很多NDK開發(fā)入門都是基于Ecplise,ndk-build方式的開發(fā)教程,本文主要介紹在現(xiàn)有項(xiàng)目中用CMake腳本方式添加C/C++代碼。

Android NDK 安裝教程參考官網(wǎng)

官網(wǎng)開發(fā)教程

NDK開發(fā)教程

一.創(chuàng)建源文件
  • IDE左側(cè),切換項(xiàng)目為Project試圖
  • 導(dǎo)航到src,右鍵點(diǎn)擊main目錄,選擇New > Directory
  • 在目錄中給文件夾取名(官網(wǎng)取名cpp,隨便?。┎Ⅻc(diǎn)擊OK
  • 右鍵點(diǎn)擊上一步創(chuàng)建目錄,然后選擇New > C/C++ Source File
  • 為源文件輸入一個(gè)名稱,如native-lib(愛咋取咋?。?,然后點(diǎn)擊OK
二.創(chuàng)建CMake腳本

右鍵點(diǎn)擊app,選擇New > File,創(chuàng)建名為CMakeLists.txt作為文件名并點(diǎn)擊OK(名字不能看心情??發(fā)揮了)

接下來我們添加CMake以下命令,開始構(gòu)建腳本(其他相關(guān)操作配置,,根據(jù)相關(guān)需求參考官方文檔)

# 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 版本號(hào),每個(gè)版本新增功能,參考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相對(duì)路徑
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.
# 關(guān)聯(lián)相關(guān)lib
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the log library to the target library.
                       ${log-lib} )

三.使用Android GUI關(guān)聯(lián)CMake

  • 左側(cè)IDE打開Project選擇app模塊
  • 右鍵app文件夾,從菜單中選擇Link C++ Project with Gradle
  • 在彈框中,Build System選擇CMake方式,Path選擇你的CMakeLists.txt文件,點(diǎn)擊OK等待gradle構(gòu)建完成

四.開始在java中定義native方法

  • 定義native方法
public class NativeDemo {
    //靜態(tài)native方法
    public static native String helloFromJNI();
}
//非靜態(tài)native方法
public class Hello {
    public int property;
    public int function(int foo, Date date, int[] arr) {
        System.out.println("function");
        Log.d("Yxjie","jni輸入數(shù)字:"+foo);
        return foo;
    }
    public boolean func(int a ,double b,char c){
        return true;
    }
    public native void test();
}
  • 開始寫native-lib.cpp文件
    開始編寫之前我們需要了解一下簽名格式:
java類型 相應(yīng)簽名格式
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 (參數(shù)類型...)返回值類型

<font color=red>注:char類型參數(shù)要在前面加個(gè)L,如L'a',java
中字符在Unicode是雙字節(jié)的,而C++中的字符是單字節(jié),需要加寬</font>

#include "jni.h"

extern "C"
JNIEXPORT jstring  JNICALL
Java_com_example_ndktest_NativeDemo_helloFromJNI(JNIEnv *env, jclass jclz) {
    //靜態(tài)native方法 需要兩個(gè)參數(shù)JNIEnv,以及jclass
    return env->NewStringUTF("Hello From JNI");
}

extern "C"
JNIEXPORT void Java_com_example_ndktest_Hello_test(JNIEnv *env, jobject obj) {
    //非靜態(tài)方法需要JNIEnv以及jobject(java中Hello,class對(duì)象引用)
    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默認(rèn)值

    jmethodID methodId_function = env->GetMethodID(hello_clazz, "function",
                                                   "(ILjava/util/Date;[I)I");//獲取對(duì)應(yīng)方法

    //批量傳入?yún)?shù)
    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 中字符雙字節(jié),C++字符為單子接需要編程器寬字節(jié)
    env->CallBooleanMethod(hello_clazz, methodId_func, args);
    delete[] args;
    //直接傳值
    env->SetIntField(hello_clazz, fieldId_prop, 100L);//設(shè)置java類中props的值
    env->CallIntMethod(obj, methodId_function, 11L, NULL, NULL);//調(diào)用方法

}

  • 加載load lib
    我是直接放在我的入口Activity里面,直接靜態(tài)代碼塊加載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();
    }
}

<font color=red>注:如有錯(cuò)誤歡迎留言指正</font>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容