NDK入門(一) 在Android Studio中創建並運行第一個ndk程序hello world

在Android Studio中創建並運行第一個ndk程序hello world

簡介

最近開始自學NDK,所有做一個筆記用來記錄。另外本文適合沒有ndk基礎的朋友學習使用。
感謝官方中文文檔的翻譯者。
官方文檔: https://developer.android.com/studio/projects/add-native-code.html#existing-project

前言

因爲使用的是ndk-bundle,根據官方介紹推薦搭配使用 Android Studio 2.2 或更高版本與 Android Plugin for Gradle 版本 2.2.0 或更高版本。

下載 NDK 和構建工具

  • 需要安裝的組件(這段是官網的解釋)
Android 原生開發工具包 (NDK): 這套工具集允許您爲 Android 使用 C 和 C++ 代碼,並提供衆多平臺庫,
讓您可以管理原生 Activity 和訪問物理設備組件,例如傳感器和觸摸輸入。
CMake: 一款外部構建工具,可與 Gradle 搭配使用來構建原生庫。如果您只計劃使用 ndk-build,則不需要此組件。
LLDB: 一種調試程序,Android Studio 使用它來調試原生代碼。
  • 使用sdk-manager安裝組件

    這裏寫圖片描述

  • 配置NDK環境變量
    在環境變量中新建NDK_ROOT,並將下載好的ndk-bundle的路徑配置進去,並在path中添加%NDK_ROOT%;

    這裏寫圖片描述

開始hello world

創建一個支持 C++ / C 的項目(重要)

  1. 在嚮導的 Configure your new project 部分,選中 Include C++ Support 複選框。
    這裏寫圖片描述

  2. 點擊 Next。

  3. 正常填寫所有其他字段並完成嚮導接下來的幾個部分。
  4. 在嚮導的 Customize C++ Support 部分,您可以使用下列選項自定義項目:
    • C++ Standard:使用下拉列表選擇您希望使用哪種 C++ 標準。選擇 Toolchain Default 會使用默認的 CMake 設置。
    • Exceptions Support:如果您希望啓用對 C++ 異常處理的支持,請選中此複選框。如果啓用此複選框,Android Studio 會將 -fexceptions 標誌添加到模塊級 build.gradle 文件的 cppFlags 中,Gradle 會將其傳遞到 CMake。
    • Runtime Type Information Support:如果您希望支持 RTTI,請選中此複選框。如果啓用此複選框,Android Studio 會將 -frtti 標誌添加到模塊級 build.gradle 文件的 cppFlags 中,Gradle 會將其傳遞到 CMake。

這裏寫圖片描述
5. 點擊 Finish。

注:如果您的現有項目使用已棄用的 ndkCompile 工具,則應先打開 build.properties 文件,在裏面添加如下代碼

android.useDeprecatedNdk = true

編譯運行

運行結果:
這裏寫圖片描述

關鍵文件和代碼

因爲添加了默認支持,所以項目裏自動生成了對應這個demo的代碼和文件 , 如果沒有添加就需要手動添加 。

MainActivity(包含native方法,native方法調用,加載so庫)

主要是native方法,native庫,還有就是使用。需要注意的是在static中使用的庫名一定要和CMakeLists中庫名字一致,也要和cpp文件夾中的c++源文件名字一致。

public class MainActivity extends AppCompatActivity {

    // 在應用開始的時候,加載CPP生成的native-lib庫
       static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        // 調用native方法,最終執行的地方是在native層中
        tv.setText(stringFromJNI());
    }

    /**
     * native 方法,用於調用cpp文件中的方法。無具體實現
     */
    public native String stringFromJNI(); 
}

native-lib.cpp (剛纔那個native方法指向的真正運行的邏輯和方法)

這個c++源文件位於app Module中,main文件夾下。因爲是自動生成的demo,所以名字默認是native-lib,如果自己寫得方法,注意名字和CMakeLists中定義的一致。

這裏寫圖片描述

代碼以及解釋:

#include <jni.h>  //jni庫
#include <string> //string ku

using namespace std; //std命名空間

extern "C" //按照C的規則翻譯函數名
JNIEXPORT jstring JNICALL
//格式是java_包名_native所在的文件_native方法
Java_cn_yefeidd_hellojni_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    string hello = "Hello from C++"; 
    return env->NewStringUTF(hello.c_str()); //返回hello from c++ 字符串
}

gradle 配置

指定CMake配置文件路徑

android {
   externalNativeBuild {
        cmake {
            path "CMakeLists.txt"  //默認用cmake進行編譯,這裏指定編譯文件路徑
        }
    }
}

CMakeList.txt CMake配置文件

這裏具體的構建方法參考此官方鏈接:

Cmake構建方式

CMakeList具體內容

# 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.設置庫的名字,名字需要跟你在cpp文件中C++的文件一樣。同時會在build文件中生成libnative-lib.so文件
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s). 設置cpp文件的路徑
             src/main/cpp/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. 使用是ndk自帶的日誌庫。
              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.  //如果奧是用log-lib,需要link到我們自己的庫native-lib
                       native-lib

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

CmakeList官方文檔的中文翻譯

創建 CMake 構建腳本

如果您的原生源文件還沒有 CMake 構建腳本,則您需要自行創建一個幷包含適當的 CMake 命令。CMake 構建腳本是一個純文本文件,您必須將其命名爲 CMakeLists.txt。本部分介紹了您應包含到構建腳本中的一些基本命令,用於在創建原生庫時指示 CMake 應使用哪些源文件。

注:如果您的項目使用 ndk-build,則不需要創建 CMake 構建腳本。提供一個指向您的 Android.mk 文件的路徑,將 Gradle 關聯到您的原生庫。

要創建一個可以用作 CMake 構建腳本的純文本文件,請按以下步驟操作:

從 IDE 的左側打開 Project 窗格並從下拉菜單中選擇 Project 視圖。
右鍵點擊 您的模塊 的根目錄並選擇 New > File。
注:您可以在所需的任意位置創建構建腳本。不過,在配置構建腳本時,原生源文件和庫的路徑將與構建腳本的位置相關。

輸入“CMakeLists.txt”作爲文件名並點擊 OK。
現在,您可以添加 CMake 命令,對您的構建腳本進行配置。要指示 CMake 從原生源代碼創建一個原生庫,請將 cmake_minimum_required() 和 add_library() 命令添加到您的構建腳本中:

# 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_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.

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 )

使用 add_library() 向您的 CMake 構建腳本添加源文件或庫時,Android Studio 還會在您同步項目後在 Project 視圖下顯示關聯的標頭文件。不過,爲了確保 CMake 可以在編譯時定位您的標頭文件,您需要將 include_directories() 命令添加到 CMake 構建腳本中並指定標頭的路徑:

add_library(...)
# Specifies a path to native header files.
include_directories(src/main/cpp/include/)

CMake 使用以下規範來爲庫文件命名:
lib庫名稱.so

例如,如果您在構建腳本中指定“native-lib”作爲共享庫的名稱,CMake 將創建一個名稱爲 libnative-lib.so 的文件。不過,在 Java 代碼中加載此庫時,請使用您在 CMake 構建腳本中指定的名稱:

static {
    System.loadLibrary(“native-lib”);
}

注:如果您在 CMake 構建腳本中重命名或移除某個庫,您需要先清理項目,Gradle 隨後纔會應用更改或者從 APK 中移除舊版本的庫。要清理項目,請從菜單欄中選擇 Build > Clean Project。

Android Studio 會自動將源文件和標頭添加到 Project 窗格的 cpp 組中。使用多個 add_library() 命令,您可以爲 CMake 定義要從其他源文件構建的更多庫

添加 NDK API

Android NDK 提供了一套實用的原生 API 和庫。通過將 NDK 庫包含到項目的 CMakeLists.txt 腳本文件中,您可以使用這些 API 中的任意一種。

預構建的 NDK 庫已經存在於 Android 平臺上,因此,您無需再構建或將其打包到 APK 中。由於 NDK 庫已經是 CMake 搜索路徑的一部分,您甚至不需要在您的本地 NDK 安裝中指定庫的位置 - 只需要向 CMake 提供您希望使用的庫的名稱,並將其關聯到您自己的原生庫。

將 find_library() 命令添加到您的 CMake 構建腳本中以定位 NDK 庫,並將其路徑存儲爲一個變量。您可以使用此變量在構建腳本的其他部分引用 NDK 庫。以下示例可以定位 Android 特定的日誌支持庫並將其路徑存儲在 log-lib 中:

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 )

爲了確保您的原生庫可以在 log 庫中調用函數,您需要使用 CMake 構建腳本中的 target_link_libraries() 命令關聯庫:

find_library(...)

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       native-lib

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

NDK 還以源代碼的形式包含一些庫,您在構建和關聯到您的原生庫時需要使用這些代碼。您可以使用 CMake 構建腳本中的 add_library() 命令,將源代碼編譯到原生庫中。要提供本地 NDK 庫的路徑,您可以使用 ANDROID_NDK 路徑變量,Android Studio 會自動爲您定義此變量。

以下命令可以指示 CMake 構建 android_native_app_glue.c,後者會將 NativeActivity 生命週期事件和觸摸輸入置於靜態庫中並將靜態庫關聯到 native-lib:

add_library( app-glue
             STATIC
             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )

# You need to link static libraries against your shared native library.
target_link_libraries( native-lib app-glue ${log-lib} )

添加其他預構建庫
添加預構建庫與爲 CMake 指定要構建的另一個原生庫類似。不過,由於庫已經預先構建,您需要使用 IMPORTED 標誌告知 CMake 您只希望將庫導入到項目中:

add_library( imported-lib
             SHARED
             IMPORTED )

然後,您需要使用 set_target_properties() 命令指定庫的路徑,如下所示。

某些庫爲特定的 CPU 架構(或應用二進制接口 (ABI))提供了單獨的軟件包,並將其組織到單獨的目錄中。此方法既有助於庫充分利用特定的 CPU 架構,又能讓您僅使用所需的庫版本。要向 CMake 構建腳本中添加庫的多個 ABI 版本,而不必爲庫的每個版本編寫多個命令,您可以使用 ANDROID_ABI 路徑變量。此變量使用 NDK 支持的一組默認 ABI,或者您手動配置 Gradle 而讓其使用的一組經過篩選的 ABI。例如:

add_library(...)
set_target_properties( # Specifies the target library.
                       imported-lib

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

爲了確保 CMake 可以在編譯時定位您的標頭文件,您需要使用 include_directories() 命令,幷包含標頭文件的路徑:

include_directories( imported-lib/include/ )

注:如果您希望打包一個並不是構建時依賴項的預構建庫(例如在添加屬於 imported-lib 依賴項的預構建庫時),則不需要執行以下說明來關聯庫。

要將預構建庫關聯到您自己的原生庫,請將其添加到 CMake 構建腳本的 target_link_libraries() 命令中:

target_link_libraries( native-lib imported-lib app-glue ${log-lib} )

在您構建應用時,Gradle 會自動將導入的庫打包到 APK 中。您可以使用 APK 分析器驗證 Gradle 將哪些庫打包到您的 APK 中。如需瞭解有關 CMake 命令的詳細信息,請參閱 CMake 文檔

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