亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解AndroidStudio3.0開發(fā)調(diào)試安卓NDK的C++代碼

 更新時(shí)間:2017年12月06日 11:21:13   作者:asmcvc  
這篇文章主要介紹了AndroidStudio3.0開發(fā)調(diào)試安卓NDK的C++代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一、新建項(xiàng)目

新建項(xiàng)目,沒有發(fā)現(xiàn)Include C++ Support 選項(xiàng)。因?yàn)橛∠笾惺怯羞^該選項(xiàng)的,找了半天沒找到。

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

新建一個(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文件,沒有.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目錄下多了一個(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的名稱(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ò),于是打算把過去的項(xiàng)目也支持起來,方法請(qǐng)看下節(jié)。

二、已有項(xiàng)目

1、安裝C++調(diào)試器LLDB

由于之前一直沒有使用過AndroidStudio調(diào)試過native的代碼,網(wǎng)上了解到AndroidStudio調(diào)試NDK是需要一個(gè)LLDB的插件,默認(rèn)是沒有的,所以先手動(dòng)安裝一下。

這里有個(gè)另類的方法:“Edit Configurations”打開程序配置,在debugger里選擇Native(默認(rèn)為auto),然后運(yùn)行App,因?yàn)楣こ讨耙恢笔侵挥蠮ava代碼的,所以這里選擇了Native,AndroidStudio會(huì)提示并沒有安裝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文件,比較簡單,不能夠滿足現(xiàn)有工程,需要修改一下。這里說一下常用的幾個(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庫文件(例如我需要使用三方的libsubstrate.so庫做測(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”打開程序配置,在debugger里選擇auto,因?yàn)橹靶薷臑榱薔ative。這樣,無論是Java代碼還是C++代碼均可以調(diào)試了。

三、總結(jié)

能支持對(duì)C++代碼的動(dòng)態(tài)調(diào)試,無疑是非常強(qiáng)大的功能,關(guān)鍵現(xiàn)在AndroidStudio對(duì)C++代碼在編輯器也支持的很好,所以總體是建議遷移過來的。

不足就是編譯速度太慢了,VisualStudio編譯下來秒間就能完成了,AndroidStudio下要十幾秒甚至更長。在調(diào)試的時(shí)候啟動(dòng)LLDB也很慢,有時(shí)一直卡在Starting LLDB server

建議VS和本方法結(jié)合使用,需要調(diào)試的時(shí)候就用AndroidStudio調(diào)試,如果僅僅是編譯C++代碼則可以使用VS,VS的方法參見:使用VisualStudio高效開發(fā)調(diào)試AndroidNDK

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論