Android JNI 開發入門

前言

本文主要介紹如何在Android Studio上進行Native開發和調試,通過本文希望讀者可以瞭解到以下幾個方面:

  1. 搭建Native相關環境,主要包括NDK、CMake、LLDB
  2. 創建一個新的Native項目
  3. CMake命令和腳本編寫

 

環境說明

本文基於MAC系統和Android Studio 3.5,不同系統版本和AS版本可能不一樣,具體參考官方文檔

 

搭建Native相關環境

編譯和調試native代碼,需要

  • NDK(Android 原生開發套件):這套工具使您能在 Android 應用中使用 C 和 C++ 代碼,鏈接

  • CMake:一款外部編譯工具,可與 Gradle 搭配使用來編譯原生庫。如果您只計劃使用 ndk-build,則不需要此組件。
  • LLDB:Android Studio 用於調試原生代碼的調試程序。

安裝路徑如下:

Android Studio -> Preferences -> Appearance & Behavior -> System Settings -> Android SDK -> SDK Tools 

選擇 LLDB、CMake、NDK,點擊Apply,然後在彈出的對話框點擊OK,AS會默認安裝NDK的最新版本。安裝成功後我們就可以嘗試創建Native項目,請看下一步。

 

創建Native項目

選擇File -> New Project -> Native C++,然後Next,填寫項目名稱和包名後Next,選擇C++標準,沒有特別需求可以直接默認,點finish,官方文檔

 

至此Native項目創建完成,在android視圖下可以看到在app目錄下有一個與java目錄平級的cpp目錄,在該目錄下可以找到項目中的所有原生源代碼文件、頭文件、CMake 或 ndk-build 的編譯腳本,以及項目中的預編譯庫。

對於新項目,Android Studio 會默認創建一個示例 C++ 源代碼文件 ​native-lib.cpp​,並將其置於應用模塊的 ​src/main/cpp/​ 目錄中。此示例代碼提供了一個簡單的 C++ 函數 ​stringFromJNI()​,它可以返回字符串“Hello from C++”。

我們先來編譯一下項目,點擊Build -> Make Project,完成後在app/build/outputs/apks目錄下可以看到生成的apk,通過apk分析窗口查看如下

可以看到生成了原生庫libnative-lib.so,這個是怎麼生成的呢?具體跟cpp目錄下的編譯腳本CMakeList.txt有關,請看下一節。

 

CMake命令和腳本編寫

默認生成的CMake腳本內容如下,這個腳本主要是用來指示CMake編譯native源代碼(native-lib.cpp)並創建原生庫(libnative-lib.so)

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

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).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

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

上面用到了4個命令:

  1. cmake_minimum_required:指定最低要求版本
  2. add_library:添加要編譯的native源代碼,
    1. native-lib:第一個參數指定了編譯生成的庫名稱,
    2. SHARED:第二個參數指定了爲STATIC或是SHARED,
    3. native-lib.cpp:第三個參數提供了native源代碼文件的相對路徑
  3. find_library:讓CMake找到指定的 NDK 庫(log)並將其路徑存儲爲一個變量,在編譯腳本的其他部分可以使用該變量
  4. target_link_libraries:關聯要使用的NDK庫(log),讓我們的原生庫能夠調用NDK庫( log) 庫中的函數

除了上面這幾個命令,還有其他的一些命令可能用到,具體參考 CMake腳本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章