前言
本文主要介绍如何在Android Studio上进行Native开发和调试,通过本文希望读者可以了解到以下几个方面:
- 搭建Native相关环境,主要包括NDK、CMake、LLDB
- 创建一个新的Native项目
- CMake命令和脚本编写
环境说明
本文基于MAC系统和Android Studio 3.5,不同系统版本和AS版本可能不一样,具体参考官方文档。
搭建Native相关环境
编译和调试native代码,需要
-
NDK(Android 原生开发套件):这套工具使您能在 Android 应用中使用 C 和 C++ 代码,链接
- CMake:一款外部编译工具,可与 Gradle 搭配使用来编译原生库。如果您只计划使用 ndk-build,则不需要此组件。
- LLDB:Android Studio 用于调试原生代码的调试程序。
安装路径如下:
Android Studio -> Preferences -> Appearance & Behavior -> System Settings -> Android SDK -> SDK Tools
选择 LLDB、CMake、NDK,点击Apply,然后在弹出的对话框点击OK,AS会默认安装NDK的最新版本。安装成功后我们就可以尝试创建Native项目,请看下一步。
创建Native项目
选择File -> New Project -> Native C++,然后Next,填写项目名称和包名后Next,选择C++标准,没有特别需求可以直接默认,点finish,官方文档
至此Native项目创建完成,在android视图下可以看到在app目录下有一个与java目录平级的cpp目录,在该目录下可以找到项目中的所有原生源代码文件、头文件、CMake 或 ndk-build 的编译脚本,以及项目中的预编译库。
对于新项目,Android Studio 会默认创建一个示例 C++ 源代码文件 native-lib.cpp,并将其置于应用模块的 src/main/cpp/ 目录中。此示例代码提供了一个简单的 C++ 函数 stringFromJNI(),它可以返回字符串“Hello from C++”。
我们先来编译一下项目,点击Build -> Make Project,完成后在app/build/outputs/apks目录下可以看到生成的apk,通过apk分析窗口查看如下
可以看到生成了原生库libnative-lib.so,这个是怎么生成的呢?具体跟cpp目录下的编译脚本CMakeList.txt有关,请看下一节。
CMake命令和脚本编写
默认生成的CMake脚本内容如下,这个脚本主要是用来指示CMake编译native源代码(native-lib.cpp)并创建原生库(libnative-lib.so)
# 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).
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})
上面用到了4个命令:
- cmake_minimum_required:指定最低要求版本
- add_library:添加要编译的native源代码,
- native-lib:第一个参数指定了编译生成的库名称,
- SHARED:第二个参数指定了为STATIC或是SHARED,
- native-lib.cpp:第三个参数提供了native源代码文件的相对路径
- find_library:让CMake找到指定的 NDK 库(log)并将其路径存储为一个变量,在编译脚本的其他部分可以使用该变量
- target_link_libraries:关联要使用的NDK库(log),让我们的原生库能够调用NDK库( log) 库中的函数
除了上面这几个命令,还有其他的一些命令可能用到,具体参考 CMake脚本