Android Studio 2.2 及以后的版本默认使用CMake进行 NDK 编译,让开发者在开发NDK程序时可以进行联机调试,大大的提高了开发者开发NDK程序的效率,如果想详细了解在cmake下定位调试bug的可以看一下笔者以前发布过的文章。
CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多。CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt文件转化为make所需要的makefile文件,最后用make命令编译源码生成可执行程序或共享库(so(shared object)).它的作用和qt的qmake是相似的。“CMake”这个名字是"cross platform make"的缩写。虽然名字中含有"make",但是CMake和Unix上常见的“make”系统是分开的,而且更为高级
android studio 的版本是: Android Studio 4.2
2.还需要在 build.gradle中增加
android{
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
2.也可以知道c++编译版本,显示异常。
android{
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
3.可以指定编译平台版本都会,在 build.gradle 中增加如下代码
android {
defaultConfig {
ndk{
abiFilters "arm64-v8a"
}
}
}
4.配置CMakeLists.txt
6.我们打开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.10.2)
# Declares and names the project.
project("myapplication")
# 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 #设置库的名称。即SO文件的名称,生产的so文件为“libnative-lib.so”,
# 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.
# 搜索指定的预构建库,并将该路径存储为一个变量。因为cbuild默认包含了搜索路径中的系统库,所以您只需要指定您想要添加的公共NDK库的名称。cbuild在完成构建之前验证这个库是否存在。
find_library( # Sets the name of the path variable.
log-lib # 设置path变量的名称。
# Specifies the name of the NDK library that
# you want CMake to locate.
# 指定NDK库的名称 你想让CMake来定位。
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} )
当我们在Android Studio中build我们的NDK工程时,Aandroid Studio会通过上面的步骤为我们设置好交叉编译环境,然后再将CMakelists.txt文件传给 CMake, CMake解析里面的内容,并最终调用不同平台的工具,编译出我们需要的目标环境程序。
通过前文的介绍,主要了解 Android Studio 在开发 NDK 程序时,是怎样使用 CMake Gradle plugin 配置交叉编译环境的,同时了解了 CMake 编译 NDK 程序的基本流程。希望对大家理解最新的 Andriod Studio 是如何使用 CMake进行交叉编译的有所帮助。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。