入門HelloWorld
新建項目
-
Configure your new project部分選中 Include C++ Support 複選框
-
Next
-
正常填寫所有其他字段並完成嚮導接下來幾個部分
-
在嚮導的Customize C++ Support 部分,您可以使用謝列選項自定義項目:
-
C++ Standard : 使用下拉列表選擇使用的C++標準。選擇Toolchain Defalut 會使用默認的CMake設置。
-
Exception Support : 如果您希望啓用對C++異常處理的支持,請選中該複選框,Android Studio 會將 -fexception標誌添加到模塊級別build.gradle 文件的cppFlag中,Gradle會將其傳遞到CMake.
-
Runtime Type Information Support: 如果您希望支持RTTI,請選中該複選框,如果啓用Android Studio會將**-frtti標誌添加到模塊級別build.gradle** 文件的cppFlag中,Gradle會將其傳遞到CMake.
-
-
finish
項目結構
-
cpp 組
您可以找到屬於項目的所有原生源文件、標頭和預構建庫。對於新項目,Android Studio 會創建一個示例 C++ 源文件 native-lib.cpp,並將其置於應用模塊的 src/main/cpp/ 目錄中。本示例代碼提供了一個簡單的 C++ 函數 stringFromJNI(),此函數可以返回字符串“Hello from C++”。要了解如何向項目添加其他源文件,請參閱介紹如何創建新的原生源文件的部分。
-
External Build File :
您可以找到 CMake 或 ndk-build 的構建腳本。與 build.gradle 文件指示 Gradle 如何構建應用一樣,CMake 和 ndk-build 需要一個構建腳本來了解如何構建您的原生庫。對於新項目,Android Studio 會創建一個 CMake 構建腳本 CMakeLists.txt,並將其置於模塊的根目錄中。要詳細瞭解此構建腳本的內容,請參閱介紹如何創建 Cmake 構建腳本的部分。
構建過程
Gradle 調用您的外部構建腳本 CMakeLists.txt。 CMake 按照構建腳本中的命令將 C++ 源文件例如 native-lib.cpp 編譯到共享的對象庫中,並命名爲 libnative-lib.so,Gradle 隨後會將其打包到APK 中。
創建 CMake 構建腳本
如果您的原生源文件還沒有 CMake 構建腳本,則您需要自行創建一個幷包含適當的 CMake 命令。CMake 構建腳本是一個純文本文件,您必須將其命名爲 CMakeLists.txt。本部分介紹了您應包含到構建腳本中的一些基本命令,用於在創建原生庫時指示 CMake 應使用哪些源文件。
要創建一個可以用作 CMake 構建腳本的純文本文件,請按以下步驟操作
- 從 IDE 的左側打開 Project 窗格並從下拉菜單中選擇 Project 視圖。
- 右鍵點擊 您的模塊 的根目錄並選擇 New > File。
- 輸入“CMakeLists.txt”作爲文件名並點擊 OK。
要指示 CMake 從原生源代碼創建一個原生庫,請將 cmake_minimum_required() 和 add_library() 命令添加到您的構建腳本中:
# 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構建腳本版本
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
# SHARED 動態鏈接庫 STATIC 靜態鏈接庫 Sets the library as a shared library.
SHARED
# 相對路徑 Provides a relative path to your source file(s).
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.
# 用於定位NDK中的庫
# 需要傳入兩個參數(path變量、ndk庫名稱)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
# 指定CMake查詢庫的名稱,即在ndk 開發包中查詢liblog.so函數庫,將其路徑賦值給log-lib
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變量對應liblog.so函數庫
${log-lib} )
NOTE:
- 使用 add_library() 向您的 CMake 構建腳本添加源文件或庫時,Android Studio 還會在您同步項目後在 Project 視圖下顯示關聯的標頭文件。不過,爲了確保 CMake 可以在編譯時定位您的標頭文件,您需要將 include_directories() 命令添加到 CMake 構建腳本中並指定標頭的路徑:
# Specifies a path to native header files.
include_directories(src/main/cpp/include/)
- 將 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/ )
- 要將預構建庫關聯到您自己的原生庫,請將其添加到 CMake 構建腳本的 target_link_libraries() 命令中:
target_link_libraries( native-lib imported-lib app-glue ${log-lib} )
構建應用時,Gradle 會自動將導入的庫打包到 APK 中。您可以使用 APK 分析器驗證 Gradle 將哪些庫打包到您的 APK 中。如需瞭解有關 CMake 命令的詳細信息,請參閱 CMake 文檔。
坑
新建Demo時候遇到的坑
-
CMakeList.txt
add_libary() 腳本,報錯,如果寫了新的Cpp/C文件,rebuld不會提示並生成,直接報錯在該節點首行,
應先在對應目錄新建Cpp/C文件,再引入
-
注意修改 target_link_libraries 下的 target Libary