详解AndroidStudio3.0开发调试安卓NDK的C++代码本⽂介绍了AndroidStudio3.0开发调试安卓NDK的C++代码,分享给⼤家,具有如下:
⼀、新建项⽬
新建项⽬,没有发现Include C++ Support 选项。因为印象中是有过该选项的,了半天没到。
后来⽆意间拖了下窗⼝⼤⼩,原来是被隐藏了,真特么坑。
新建⼀个测试项⽬,勾选Include C++ Support 选项,看看⼯程上有哪些不同。
1、gradle
⾸先看gradle⽂件,android节点下添加:
externalNativeBuild {
cmake {
path ""
}
}
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(
xposed
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
3、
在app⽬录下多了⼀个⽂件。
# For more information about using CMake with Android Studio, read the
# documentation: d.android/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.
然后是指定⽂件的路径,这⾥可以复制新建项⽬的⽂件到现有项⽬的app⽬录下,把它放到和proguard-rules.pro相同的⽂件夹下即可。然后把这个⽂件的全路径输⼊进去,点OK。
这个时候会发现gradle⽂件⾃动添加了:
externalNativeBuild {
cmake {
path ""
}
}
但是并未指定C++的版本,可以参考新建项⽬的内容⼿动添加:
externalNativeBuild {
cmake {
cppFlags "-std=c++14"
}
}
3、整理C++源码的⽂件组织形式
新建⼀个cpp⽬录:src\main\cpp,与src\main\java同级,把C++源码⽂件移动⾄此⽬录下,并有序组织好。
4、修改
由于是复制的demo⼯程的⽂件,⽐较简单,不能够满⾜现有⼯程,需要修改⼀下。这⾥说⼀下常⽤的⼏个功能:
- 设置其他后缀⽂件(例如.S汇编⽂件)为可编译源⽂件:
复制代码代码如下:
set_property(SOURCE src/main/cpp/art/art_quick_dexposed_invoke_handler.S PROPERTY LANGUA
GE 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++代码均可以调试了。
三、总结
能⽀持对C++代码的动态调试,⽆疑是⾮常强⼤的功能,关键现在AndroidStudio对C++代码在编辑器也⽀持的很好,所以总体是建议迁移过来的。
不⾜就是编译速度太慢了,VisualStudio编译下来秒间就能完成了,AndroidStudio下要⼗⼏秒甚⾄更长。在调试的时候启动LLDB也很慢,有时⼀直卡在Starting LLDB server 建议VS和本⽅法结合使⽤,需要调试的时候就⽤AndroidStudio调试,如果仅仅是编译C++代码则可以使⽤VS,VS的⽅法参见:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。