這篇文章將為大家詳細講解有關AndroidStudio3.0如何開發調試安卓NDK,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
一、新建項目
新建項目,沒有發現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 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目錄下多了一個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匯編文件)為可編譯源文件:
復制代碼 代碼如下:
set_property(SOURCE src/main/cpp/art/art_quick_dexposed_invoke_handler.S PROPERTY LANGUAGE C)
設置多個不定數量的源文件(也即使用*星號通配符的方式):
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++代碼均可以調試了。
關于“AndroidStudio3.0如何開發調試安卓NDK”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。