1、創(chuàng)建新項(xiàng)目(Create New Project)
點(diǎn)擊File — New — New Project宰掉,把Include C++ Support前面的CheckBook勾上麻汰,然后依次進(jìn)行接下來(lái)的操作
2窟扑、配置C++支持功能(Customize C++ Support)
在Customize C++ Support界面默認(rèn)即可
- C++ Standard
指定編譯庫(kù)的環(huán)境预皇,其中Toolchain Default使用的是默認(rèn)的CMake環(huán)境撤蚊;C++ 11也就是C++環(huán)境奕短。- Exceptions Support
如果選中復(fù)選框宜肉,則表示當(dāng)前項(xiàng)目支持C++異常處理,如果支持翎碑,在項(xiàng)目Module級(jí)別的build.gradle文件中會(huì)增加一個(gè)標(biāo)識(shí) -fexceptions到cppFlags屬性中谬返,并且在so庫(kù)構(gòu)建時(shí),gradle會(huì)把該屬性值傳遞給CMake進(jìn)行構(gòu)建日杈。- Runtime Type Information Support
同理遣铝,選中復(fù)選框,項(xiàng)目支持RTTI莉擒,屬性cppFlags增加標(biāo)識(shí)-frtti
3酿炸、CMakeLists.txt構(gòu)建
CMakeLists.txt文件用于配置JNI項(xiàng)目屬性,主要用于聲明CMake使用版本涨冀、so庫(kù)名稱填硕、C/CPP文件路徑等信息,下面是該文件內(nèi)容:
# 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).
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.
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} )
cmake_minimum_required(VERSION 3.4.1)
CMake最小版本使用的是3.4.1鹿鳖。add_library()
配置so庫(kù)信息(為當(dāng)前腳本文件添加庫(kù))
- native-lib這個(gè)是聲明引用so庫(kù)的名稱扁眯,在項(xiàng)目中,如果需要使用這個(gè)so文件翅帜,引用的名稱就是這個(gè)姻檀。值得注意的是,實(shí)際上生成的so文件名稱是libnative-lib涝滴。當(dāng)Run項(xiàng)目或者build項(xiàng)目是绣版,在Module級(jí)別的build文件下的intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main
下會(huì)生成相應(yīng)的so庫(kù)文件。- SHARED這個(gè)參數(shù)表示共享so庫(kù)文件歼疮,也就是在Run項(xiàng)目或者build項(xiàng)目時(shí)會(huì)在目錄intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main
下生成so庫(kù)文杂抽。此外,so庫(kù)文件都會(huì)在打包到.apk里面腋妙,可以通過(guò)選擇菜單欄的Build->Analyze Apk...*查看apk中是否存在so庫(kù)文件,一般它會(huì)存放在lib目錄下讯榕。- src/main/cpp/native-lib.cpp 構(gòu)建so庫(kù)的源文件骤素。
- STATIC:靜態(tài)庫(kù)匙睹,是目標(biāo)文件的歸檔文件,在鏈接其它目標(biāo)的時(shí)候使用济竹。
- SHARED:動(dòng)態(tài)庫(kù)痕檬,會(huì)被動(dòng)態(tài)鏈接,在運(yùn)行時(shí)被加載送浊。
- MODULE:模塊庫(kù)梦谜,是不會(huì)被鏈接到其它目標(biāo)中的插件,但是可能會(huì)在運(yùn)行時(shí)使用dlopen-系列的函數(shù)動(dòng)態(tài)鏈接
include_directories
配置頭文件路徑目錄find_library()
這個(gè)方法與我們要?jiǎng)?chuàng)建的so庫(kù)無(wú)關(guān)而是使用NDK的Apis或者庫(kù)袭景,默認(rèn)情況下Android平臺(tái)集成了很多NDK庫(kù)文件唁桩,所以這些文件是沒(méi)有必要打包到apk里面去的。直接聲明想要使用的庫(kù)名稱即可(猜測(cè):貌似是在Sytem/libs目錄下)耸棒。在這里不需要指定庫(kù)的路徑荒澡,因?yàn)檫@個(gè)路徑已經(jīng)是CMake路徑搜索的一部分。如示例中使用的是log相關(guān)的so庫(kù)与殃。
- log-lib這個(gè)指定的是在NDK庫(kù)中每個(gè)類型的庫(kù)會(huì)存放一個(gè)特定的位置单山,而log庫(kù)存放在log-lib中
- log指定使用log庫(kù)
-
target_link_libraries()
如果你本地的庫(kù)(native-lib)想要調(diào)用log庫(kù)的方法,那么就需要配置這個(gè)屬性幅疼,意思是把NDK庫(kù)關(guān)聯(lián)到本地庫(kù)米奸。
- native-lib要被關(guān)聯(lián)的庫(kù)名稱
- ${log-lib}要關(guān)聯(lián)的庫(kù)名稱,要用大括號(hào)包裹爽篷,前面還要有$符號(hào)去引用悴晰。
實(shí)際上,我們可以自己創(chuàng)建CMakeLists.txt文件狼忱,而且路徑不受限制膨疏,只要在build.gradle中配置externalNativeBuild.cmake.path來(lái)指定該文件路徑即可
4、MainActivity調(diào)用和運(yùn)行結(jié)果
其它
- 我的CSDN博客地址:http://blog.csdn.net/s003603u
- 我的GitHub地址:https://github.com/soulrelay
- 我的簡(jiǎn)書(shū)地址:http://www.reibang.com/u/514ca03bbc17
- 我的掘金地址:https://juejin.im/user/56f3d9d1816dfa00522b8f20
- 我的個(gè)人站點(diǎn): http://sushuai.tech/