版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ddxxii/article/details/84310569
一. Android Studio配置
(一) 组件下载
- 要使用和调试,先下载NDK组件:
- NDK包:这套工具集允许您为 Android 使用 C 和 C++ 代码,并提供众多平台库,让您可以管理原生 Activity 和访问物理设备组件,例如传感器和触摸输入。
- cmake:一款外部构建工具,可与 Gradle 搭配使用来构建原生库。如果您只计划使用 ndk-build,则不需要此组件。
- LLDB:一种调试程序,Android Studio 使用它来调试原生代码。
勾选如上图所示三项即可。这个不用经常更新,特别是NDK,一般有新版本出来了会提示你,但是不用随意更新,因为有可能更新到比较新的版本,你之前的版本编译就不行了哦,改起来还挺麻烦的,嘿嘿,特别是针对含有第三方库编译的情况。
(二) 配置
Android新版本推荐使用CMake,这里讲的都是CMake,使用的脚本为CMakeLists.txt
-
如果是新建项目,则勾选C++支持即可完成创建
-
如果是老项目:
- 在main目录下新建cpp的原生代码目录,然后再创建一个native-lib.cpp的c++文件
- 在module下新建文件:CMakeLists.txt
- 链接到gradle中,目的通过gradle来识别编译脚本,在运行的时候以依赖的方式来运行CMake
- gradle配置
android {
defaultConfig {
externalNativeBuild {
cmake {
//这里是打包x86的so,可以改为armabi-v7a等
arguments "-DANDROID_ABI=x86"
cppFlags ""
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
- CMakeLists.txt基本内容如下:
- add_library:配置我们库的名称、类型、和编译文件
- find_library:找寻ndk中的提供的原生库
- target_link_libraries:链接指定的库到我们库中
# 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.
#指定需传入三个参数(准备打包生成so函数库名称,库类型,依赖源文件相对路径即代码路径)
add_library( # Sets the name of the library.
my_native_lib
# SHARED 动态链接库 STATIC 静态链接库
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.
# 用于定位NDK中的库,通过find来找到,并取个path变量
# 需要传入两个参数(path变量、ndk库名称)
find_library( # Sets the name of the path variable.
#变量名取为log-lib
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
#找ndk中的log库,即找ndk中的包为liblog.so
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.
#我们的库,与前面的name保持一致
my_native_lib
# Links the target library to the log library
# included in the NDK.
#要链接进来的库,如上面指定的path
${log-lib} )
- 最后的项目结构如下:
到此,基础配置完成。下一篇开始学习下使用