Android JNI开发系列(二)HelloWorld
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
入門HelloWorld
新建項目
-
Configure your new project部分選中 Include C++ Support 復(fù)選框
-
Next
-
正常填寫所有其他字段并完成向?qū)Ы酉聛韼讉€部分
-
在向?qū)У腃ustomize C++ Support 部分,您可以使用謝列選項自定義項目:
-
C++ Standard : 使用下拉列表選擇使用的C++標(biāo)準(zhǔn)。選擇Toolchain Defalut 會使用默認(rèn)的CMake設(shè)置。
-
Exception Support : 如果您希望啟用對C++異常處理的支持,請選中該復(fù)選框,Android Studio 會將 -fexception標(biāo)志添加到模塊級別build.gradle 文件的cppFlag中,Gradle會將其傳遞到CMake.
-
Runtime Type Information Support: 如果您希望支持RTTI,請選中該復(fù)選框,如果啟用Android Studio會將**-frtti標(biāo)志添加到模塊級別build.gradle** 文件的cppFlag中,Gradle會將其傳遞到CMake.
-
-
finish
項目結(jié)構(gòu)
-
cpp 組
您可以找到屬于項目的所有原生源文件、標(biāo)頭和預(yù)構(gòu)建庫。對于新項目,Android Studio 會創(chuàng)建一個示例 C++ 源文件 native-lib.cpp,并將其置于應(yīng)用模塊的 src/main/cpp/ 目錄中。本示例代碼提供了一個簡單的 C++ 函數(shù) stringFromJNI(),此函數(shù)可以返回字符串“Hello from C++”。要了解如何向項目添加其他源文件,請參閱介紹如何創(chuàng)建新的原生源文件的部分。
-
External Build File :
您可以找到 CMake 或 ndk-build 的構(gòu)建腳本。與 build.gradle 文件指示 Gradle 如何構(gòu)建應(yīng)用一樣,CMake 和 ndk-build 需要一個構(gòu)建腳本來了解如何構(gòu)建您的原生庫。對于新項目,Android Studio 會創(chuàng)建一個 CMake 構(gòu)建腳本 CMakeLists.txt,并將其置于模塊的根目錄中。要詳細(xì)了解此構(gòu)建腳本的內(nèi)容,請參閱介紹如何創(chuàng)建 Cmake 構(gòu)建腳本的部分。
構(gòu)建過程
Gradle 調(diào)用您的外部構(gòu)建腳本 CMakeLists.txt。 CMake 按照構(gòu)建腳本中的命令將 C++ 源文件例如 native-lib.cpp 編譯到共享的對象庫中,并命名為 libnative-lib.so,Gradle 隨后會將其打包到APK 中。
創(chuàng)建 CMake 構(gòu)建腳本
如果您的原生源文件還沒有 CMake 構(gòu)建腳本,則您需要自行創(chuàng)建一個并包含適當(dāng)?shù)?CMake 命令。CMake 構(gòu)建腳本是一個純文本文件,您必須將其命名為 CMakeLists.txt。本部分介紹了您應(yīng)包含到構(gòu)建腳本中的一些基本命令,用于在創(chuàng)建原生庫時指示 CMake 應(yīng)使用哪些源文件。
要創(chuàng)建一個可以用作 CMake 構(gòu)建腳本的純文本文件,請按以下步驟操作
- 從 IDE 的左側(cè)打開 Project 窗格并從下拉菜單中選擇 Project 視圖。
- 右鍵點擊 您的模塊 的根目錄并選擇 New > File。
- 輸入“CMakeLists.txt”作為文件名并點擊 OK。
要指示 CMake 從原生源代碼創(chuàng)建一個原生庫,請將 cmake_minimum_required() 和 add_library() 命令添加到您的構(gòu)建腳本中:
# 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構(gòu)建腳本版本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. # 指定需傳入三個參數(shù)(函數(shù)庫名稱,庫類型,依賴源文件相對路徑)add_library( # Sets the name of the library.native-lib# SHARED 動態(tài)鏈接庫 STATIC 靜態(tài)鏈接庫 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中的庫 # 需要傳入兩個參數(shù)(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 開發(fā)包中查詢liblog.so函數(shù)庫,將其路徑賦值給log-liblog )# 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.# 指定關(guān)聯(lián)到原生庫的庫target_link_libraries( # Specifies the target library.# 指定目標(biāo)庫,與善變的函數(shù)名保持一致native-lib# Links the target library to the log library# included in the NDK.# 鏈接的庫,根據(jù)log-lib變量對應(yīng)liblog.so函數(shù)庫${log-lib} )NOTE:
- 使用 add_library() 向您的 CMake 構(gòu)建腳本添加源文件或庫時,Android Studio 還會在您同步項目后在 Project 視圖下顯示關(guān)聯(lián)的標(biāo)頭文件。不過,為了確保 CMake 可以在編譯時定位您的標(biāo)頭文件,您需要將 include_directories() 命令添加到 CMake 構(gòu)建腳本中并指定標(biāo)頭的路徑:
- 將 find_library() 命令添加到您的 CMake 構(gòu)建腳本中以定位 NDK 庫,并將其路徑存儲為一個變量。您可以使用此變量在構(gòu)建腳本的其他部分引用 NDK 庫。以下示例可以定位 Android 特定的日志支持庫并將其路徑存儲在 log-lib 中:
- 為了確保您的原生庫可以在 log 庫中調(diào)用函數(shù),您需要使用 CMake 構(gòu)建腳本中的 target_link_libraries() 命令關(guān)聯(lián)庫:
- NDK 還以源代碼的形式包含一些庫,您在構(gòu)建和關(guān)聯(lián)到您的原生庫時需要使用這些代碼。您可以使用 CMake 構(gòu)建腳本中的 add_library() 命令,將源代碼編譯到原生庫中。要提供本地 NDK 庫的路徑,您可以使用 ANDROID_NDK 路徑變量,Android Studio 會自動為您定義此變量。
以下命令可以指示 CMake 構(gòu)建 android_native_app_glue.c,后者會將 NativeActivity 生命周期事件和觸摸輸入置于靜態(tài)庫中并將靜態(tài)庫關(guān)聯(lián)到 native-lib:
add_library( app-glueSTATIC${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} )- 添加預(yù)構(gòu)建庫與為 CMake 指定要構(gòu)建的另一個原生庫類似。不過,由于庫已經(jīng)預(yù)先構(gòu)建,您需要使用 IMPORTED 標(biāo)志告知 CMake 您只希望將庫導(dǎo)入到項目中:
然后,您需要使用 set_target_properties() 命令指定庫的路徑,如下所示。
某些庫為特定的 CPU 架構(gòu)(或應(yīng)用二進(jìn)制接口 (ABI))提供了單獨的軟件包,并將其組織到單獨的目錄中。此方法既有助于庫充分利用特定的 CPU 架構(gòu),又能讓您僅使用所需的庫版本。要向 CMake 構(gòu)建腳本中添加庫的多個 ABI 版本,而不必為庫的每個版本編寫多個命令,您可以使用 ANDROID_ABI 路徑變量。此變量使用 NDK 支持的一組默認(rèn) ABI,或者您手動配置 Gradle 而讓其使用的一組經(jīng)過篩選的 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 可以在編譯時定位您的標(biāo)頭文件,您需要使用 include_directories() 命令,并包含標(biāo)頭文件的路徑:
- 要將預(yù)構(gòu)建庫關(guān)聯(lián)到您自己的原生庫,請將其添加到 CMake 構(gòu)建腳本的 target_link_libraries() 命令中:
構(gòu)建應(yīng)用時,Gradle 會自動將導(dǎo)入的庫打包到 APK 中。您可以使用 APK 分析器驗證 Gradle 將哪些庫打包到您的 APK 中。如需了解有關(guān) CMake 命令的詳細(xì)信息,請參閱 CMake 文檔。
坑
新建Demo時候遇到的坑
-
CMakeList.txt
add_libary() 腳本,報錯,如果寫了新的Cpp/C文件,rebuld不會提示并生成,直接報錯在該節(jié)點首行,
應(yīng)先在對應(yīng)目錄新建Cpp/C文件,再引入
-
注意修改 target_link_libraries 下的 target Libary
轉(zhuǎn)載于:https://my.oschina.net/caipeng/blog/2243624
總結(jié)
以上是生活随笔為你收集整理的Android JNI开发系列(二)HelloWorld的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql galera 下载_Mysq
- 下一篇: linux 内核编程之proc虚拟文件系