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

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

新建一個測試項目,勾選Include C++ Support 選項,看看工程上有哪些不同。
1、gradle
首先看gradle文件,android節點下添加:
externalNativeBuild { cmake { path "CMakeLists.txt" }}defaultConfig節點下添加:
externalNativeBuild { cmake { cppFlags "-std=c++14" }}2、CPP
與Java節點同級多了一個cpp的節點,對應目錄為src/main/cpp,與src/main/java同級,默認只有一個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),默認CPU為armeabi-v7a
默認的工程屬性不用配置,debugger默認為auto會自動適配,直接在cpp里下斷點,調試方式運行App,會自動斷下,變量數值均能在調試狀態下看到。

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

可以看出,安裝的調試器是LLDB。

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

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

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