您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)AndroidStudio3.0如何開(kāi)發(fā)調(diào)試安卓NDK,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
一、新建項(xiàng)目
新建項(xiàng)目,沒(méi)有發(fā)現(xiàn)Include C++ Support 選項(xiàng)。因?yàn)橛∠笾惺怯羞^(guò)該選項(xiàng)的,找了半天沒(méi)找到。
后來(lái)無(wú)意間拖了下窗口大小,原來(lái)是被隱藏了,真特么坑。
新建一個(gè)測(cè)試項(xiàng)目,勾選Include C++ Support 選項(xiàng),看看工程上有哪些不同。
1、gradle
首先看gradle文件,android節(jié)點(diǎn)下添加:
externalNativeBuild { cmake { path "CMakeLists.txt" } }
defaultConfig節(jié)點(diǎn)下添加:
externalNativeBuild { cmake { cppFlags "-std=c++14" } }
2、CPP
與Java節(jié)點(diǎn)同級(jí)多了一個(gè)cpp的節(jié)點(diǎn),對(duì)應(yīng)目錄為src\main\cpp,與src\main\java同級(jí),默認(rèn)只有一個(gè)native-lib.cpp文件,沒(méi)有.mk文件及其他,比較簡(jiǎn)單:
#include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_bigsing_myapplication_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); }
3、CMakeLists.txt
在app目錄下多了一個(gè)CMakeLists.txt文件。
# 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} )
其中native-lib為最終生成的SO的名稱(chēng)(libnative-lib.so),默認(rèn)CPU為armeabi-v7a
默認(rèn)的工程屬性不用配置,debugger默認(rèn)為auto會(huì)自動(dòng)適配,直接在cpp里下斷點(diǎn),調(diào)試方式運(yùn)行App,會(huì)自動(dòng)斷下,變量數(shù)值均能在調(diào)試狀態(tài)下看到。
試用了下AndroidStudio對(duì)NDK的調(diào)試支持的還不錯(cuò),于是打算把過(guò)去的項(xiàng)目也支持起來(lái),方法請(qǐng)看下節(jié)。
二、已有項(xiàng)目
1、安裝C++調(diào)試器LLDB
由于之前一直沒(méi)有使用過(guò)AndroidStudio調(diào)試過(guò)native的代碼,網(wǎng)上了解到AndroidStudio調(diào)試NDK是需要一個(gè)LLDB的插件,默認(rèn)是沒(méi)有的,所以先手動(dòng)安裝一下。
這里有個(gè)另類(lèi)的方法:“Edit Configurations”打開(kāi)程序配置,在debugger里選擇Native(默認(rèn)為auto),然后運(yùn)行App,因?yàn)楣こ讨耙恢笔侵挥蠮ava代碼的,所以這里選擇了Native,AndroidStudio會(huì)提示并沒(méi)有安裝C++的調(diào)試器,根據(jù)提示安裝即可。
可以看出,安裝的調(diào)試器是LLDB。
2、Link C++ Project with Gradle
在老項(xiàng)目里面添加NDK的支持,可以右鍵項(xiàng)目選擇菜單:Link C++ Project with Gradle
編譯方式有兩種:CMake和ndk-build,其中ndk-build是傳統(tǒng)方式,AndroidStudio默認(rèn)推薦CMake方式,也許這是以后的主流方式,所以我們選擇默認(rèn)的CMake.
然后是指定CMakeLists.txt文件的路徑,這里可以復(fù)制新建項(xiàng)目的CMakeLists.txt文件到現(xiàn)有項(xiàng)目的app目錄下,把它放到和proguard-rules.pro相同的文件夾下即可。然后把這個(gè)CMakeLists.txt文件的全路徑輸入進(jìn)去,點(diǎn)OK。
這個(gè)時(shí)候會(huì)發(fā)現(xiàn)gradle文件自動(dòng)添加了:
externalNativeBuild { cmake { path "CMakeLists.txt" } }
但是并未指定C++的版本,可以參考新建項(xiàng)目的內(nèi)容手動(dòng)添加:
externalNativeBuild { cmake { cppFlags "-std=c++14" } }
3、整理C++源碼的文件組織形式
新建一個(gè)cpp目錄:src\main\cpp,與src\main\java同級(jí),把C++源碼文件移動(dòng)至此目錄下,并有序組織好。
4、修改CMakeLists.txt
由于是復(fù)制的demo工程的CMakeLists.txt文件,比較簡(jiǎn)單,不能夠滿(mǎn)足現(xiàn)有工程,需要修改一下。這里說(shuō)一下常用的幾個(gè)功能:
- 設(shè)置其他后綴文件(例如.S匯編文件)為可編譯源文件:
復(fù)制代碼 代碼如下:
set_property(SOURCE src/main/cpp/art/art_quick_dexposed_invoke_handler.S PROPERTY LANGUAGE C)
設(shè)置多個(gè)不定數(shù)量的源文件(也即使用*星號(hào)通配符的方式):
file(GLOB native_srcs "src/main/cpp/*.cpp" "src/main/cpp/dalvik/*.cpp" "src/main/cpp/art/*.cpp" "src/main/cpp/art/*.S") 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_srcs} )
鏈接三方SO庫(kù)文件(例如我需要使用三方的libsubstrate.so庫(kù)做測(cè)試):
file(GLOB libs src/main/cpp/3rd/libsubstrate.so src/main/cpp/3rd/libsubstrate-dvm.so) target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${libs} ${log-lib} )
5、恢復(fù)debugger為默認(rèn)的auto
“Edit Configurations”打開(kāi)程序配置,在debugger里選擇auto,因?yàn)橹靶薷臑榱薔ative。這樣,無(wú)論是Java代碼還是C++代碼均可以調(diào)試了。
關(guān)于“AndroidStudio3.0如何開(kāi)發(fā)調(diào)試安卓NDK”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。