源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解AndroidStudio3.0开发调试安卓NDK的C++代码

  • 时间:2020-12-30 08:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解AndroidStudio3.0开发调试安卓NDK的C++代码
[b]一、新建项目[/b] 新建项目,没有发现Include C++ Support 选项。因为印象中是有过该选项的,找了半天没找到。 [img]http://files.jb51.net/file_images/article/201712/2017120611101418.png[/img] 后来无意间拖了下窗口大小,原来是被隐藏了,真特么坑。 [img]http://files.jb51.net/file_images/article/201712/2017120611101419.png[/img] 新建一个测试项目,勾选Include C++ Support 选项,看看工程上有哪些不同。 [b]1、gradle[/b] 首先看gradle文件,android节点下添加:
externalNativeBuild {
 cmake {
 path "CMakeLists.txt"
 }
}
defaultConfig节点下添加:
externalNativeBuild {
 cmake {
 cppFlags "-std=c++14"
 }
}
[b]2、CPP[/b] 与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());
}
[b]3、CMakeLists.txt[/b] 在app目录下多了一个CMakeLists.txt文件。
# For more information about using CMake with Android Studio, read the
# documentation: [url=https://d.android.com/studio/projects/add-native-code.html]https://d.android.com/studio/projects/add-native-code.html
[/url]# 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,会自动断下,变量数值均能在调试状态下看到。 [img]http://files.jb51.net/file_images/article/201712/2017120611101420.png[/img] 试用了下AndroidStudio对NDK的调试支持的还不错,于是打算把过去的项目也支持起来,方法请看下节。 [b]二、已有项目[/b] [b]1、安装C++调试器LLDB[/b] 由于之前一直没有使用过AndroidStudio调试过native的代码,网上了解到AndroidStudio调试NDK是需要一个LLDB的插件,默认是没有的,所以先手动安装一下。 这里有个另类的方法:“Edit Configurations”打开程序配置,在debugger里选择Native(默认为auto),然后运行App,因为工程之前一直是只有Java代码的,所以这里选择了Native,AndroidStudio会提示并没有安装C++的调试器,根据提示安装即可。 [img]http://files.jb51.net/file_images/article/201712/2017120611101421.png[/img] 可以看出,安装的调试器是LLDB。 [img]http://files.jb51.net/file_images/article/201712/2017120611101422.png[/img] [b]2、Link C++ Project with Gradle[/b] 在老项目里面添加NDK的支持,可以右键项目选择菜单:Link C++ Project with Gradle [img]http://files.jb51.net/file_images/article/201712/2017120611101423.png[/img] 编译方式有两种:CMake和ndk-build,其中ndk-build是传统方式,AndroidStudio默认推荐CMake方式,也许这是以后的主流方式,所以我们选择默认的CMake. [img]http://files.jb51.net/file_images/article/201712/2017120611101424.png[/img] 然后是指定CMakeLists.txt文件的路径,这里可以复制新建项目的CMakeLists.txt文件到现有项目的app目录下,把它放到和proguard-rules.pro相同的文件夹下即可。然后把这个CMakeLists.txt文件的全路径输入进去,点OK。 这个时候会发现gradle文件自动添加了:
externalNativeBuild {
 cmake {
 path "CMakeLists.txt"
 }
}
但是并未指定C++的版本,可以参考新建项目的内容手动添加:
externalNativeBuild {
 cmake {
 cppFlags "-std=c++14"
 }
}
[b]3、整理C++源码的文件组织形式[/b] 新建一个cpp目录:src\main\cpp,与src\main\java同级,把C++源码文件移动至此目录下,并有序组织好。 [b]4、修改CMakeLists.txt[/b] 由于是复制的demo工程的CMakeLists.txt文件,比较简单,不能够满足现有工程,需要修改一下。这里说一下常用的几个功能: - 设置其他后缀文件(例如.S汇编文件)为可编译源文件:
[url=http://www.1sucai.cn/article/129889.htm]使用VisualStudio高效开发调试AndroidNDK[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部