一、新建項目
新建項目,沒有發(fā)現(xiàn)Include C++ Support 選項。因為印象中是有過該選項的,找了半天沒找到。

后來無意間拖了下窗口大小,原來是被隱藏了,真特么坑。

新建一個測試項目,勾選Include C++ Support 選項,看看工程上有哪些不同。
1、gradle
首先看gradle文件,android節(jié)點下添加:
externalNativeBuild { cmake { path "CMakeLists.txt" }}defaultConfig節(jié)點下添加:
externalNativeBuild { cmake { cppFlags "-std=c++14" }}2、CPP
與Java節(jié)點同級多了一個cpp的節(jié)點,對應(yīng)目錄為src/main/cpp,與src/main/java同級,默認(rèn)只有一個native-lib.cpp文件,沒有.mk文件及其他,比較簡單:
#include <jni.h>#include <string>extern "C"JNIEXPORT jstringJNICALLJava_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目錄下多了一個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的名稱(libnative-lib.so),默認(rèn)CPU為armeabi-v7a
默認(rèn)的工程屬性不用配置,debugger默認(rèn)為auto會自動適配,直接在cpp里下斷點,調(diào)試方式運行App,會自動斷下,變量數(shù)值均能在調(diào)試狀態(tài)下看到。

試用了下AndroidStudio對NDK的調(diào)試支持的還不錯,于是打算把過去的項目也支持起來,方法請看下節(jié)。
二、已有項目
1、安裝C++調(diào)試器LLDB
由于之前一直沒有使用過AndroidStudio調(diào)試過native的代碼,網(wǎng)上了解到AndroidStudio調(diào)試NDK是需要一個LLDB的插件,默認(rèn)是沒有的,所以先手動安裝一下。
這里有個另類的方法:“Edit Configurations”打開程序配置,在debugger里選擇Native(默認(rèn)為auto),然后運行App,因為工程之前一直是只有Java代碼的,所以這里選擇了Native,AndroidStudio會提示并沒有安裝C++的調(diào)試器,根據(jù)提示安裝即可。

可以看出,安裝的調(diào)試器是LLDB。

2、Link C++ Project with Gradle
在老項目里面添加NDK的支持,可以右鍵項目選擇菜單:Link C++ Project with Gradle

編譯方式有兩種:CMake和ndk-build,其中ndk-build是傳統(tǒng)方式,AndroidStudio默認(rèn)推薦CMake方式,也許這是以后的主流方式,所以我們選擇默認(rèn)的CMake.

然后是指定CMakeLists.txt文件的路徑,這里可以復(fù)制新建項目的CMakeLists.txt文件到現(xiàn)有項目的app目錄下,把它放到和proguard-rules.pro相同的文件夾下即可。然后把這個CMakeLists.txt文件的全路徑輸入進(jìn)去,點OK。
這個時候會發(fā)現(xiàn)gradle文件自動添加了:
externalNativeBuild { cmake { path "CMakeLists.txt" }}但是并未指定C++的版本,可以參考新建項目的內(nèi)容手動添加:
externalNativeBuild { cmake { cppFlags "-std=c++14" }}3、整理C++源碼的文件組織形式
新建一個cpp目錄:src/main/cpp,與src/main/java同級,把C++源碼文件移動至此目錄下,并有序組織好。
4、修改CMakeLists.txt
由于是復(fù)制的demo工程的CMakeLists.txt文件,比較簡單,不能夠滿足現(xiàn)有工程,需要修改一下。這里說一下常用的幾個功能:
- 設(shè)置其他后綴文件(例如.S匯編文件)為可編譯源文件:
設(shè)置多個不定數(shù)量的源文件(也即使用*星號通配符的方式):
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庫文件(例如我需要使用三方的libsubstrate.so庫做測試):
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”打開程序配置,在debugger里選擇auto,因為之前修改為了Native。這樣,無論是Java代碼還是C++代碼均可以調(diào)試了。
三、總結(jié)
能支持對C++代碼的動態(tài)調(diào)試,無疑是非常強大的功能,關(guān)鍵現(xiàn)在AndroidStudio對C++代碼在編輯器也支持的很好,所以總體是建議遷移過來的。
不足就是編譯速度太慢了,VisualStudio編譯下來秒間就能完成了,AndroidStudio下要十幾秒甚至更長。在調(diào)試的時候啟動LLDB也很慢,有時一直卡在Starting LLDB server
建議VS和本方法結(jié)合使用,需要調(diào)試的時候就用AndroidStudio調(diào)試,如果僅僅是編譯C++代碼則可以使用VS,VS的方法參見:使用VisualStudio高效開發(fā)調(diào)試AndroidNDK
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選