前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android NDK cmake编译方式(六)

Android NDK cmake编译方式(六)

原创
作者头像
PengJie
发布2021-01-10 13:06:24
2.3K0
发布2021-01-10 13:06:24
举报
文章被收录于专栏:音视频修炼路音视频修炼路

介绍

Android Studio 2.2 及以后的版本默认使用CMake进行 NDK 编译,让开发者在开发NDK程序时可以进行联机调试,大大的提高了开发者开发NDK程序的效率,如果想详细了解在cmake下定位调试bug的可以看一下笔者以前发布过的文章

什么是CMake

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 如何使用 CMake

  1. Android studio 默认下载的cmake 为 cmake version 3.10.2

android studio 的版本是: Android Studio 4.2

2.还需要在 build.gradle中增加

代码语言:txt
复制
android{
            externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
         }
    }
 }

2.也可以知道c++编译版本,显示异常。

代码语言:txt
复制
android{
    externalNativeBuild {
          cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
            }
        }
 }

3.可以指定编译平台版本都会,在 build.gradle 中增加如下代码

代码语言:txt
复制
android {
    defaultConfig {
        ndk{
            abiFilters "arm64-v8a"
        }
    }
}

4.配置CMakeLists.txt

6.我们打开CMakeLists.txt文件看一下源码

代码语言: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} )
  • 其中,通过 cmake_minimum_required 方法指定 CMake 使用版本,通过 project 指定工程名。
  • set指令用于显式的定义变量。使用方式为set(var value force). 其中定义时必须填写的参数为:var 和 value。
  • add_library 指令 add_library():用于将一组源文件编译生成一个库文件,并保存为 libname.so (lib 前缀是生成文件时 CMake自动添加上去的)。可传入多个源文件,其语法为:add_library(libname SHARED | STATIC | MODULE source)。 其中生成的有三种库文件类型,不写的话,默认为 STATIC。下面我们简单的讲解一下生成的库文件类型: SHARED:表示生成的为动态库,可以在Java代码中使用System.loadLibaray(name)进行动态调用。 STATIC:表示生成的为静态库,集成到代码的时候,会在编译时调用。 MODULE:只有在dyld的系统有效,如果不支持dyld,则会被当作SHARED对待。
  • find_library 指令 这个指令是Android NDK开发提供的特有的Cmake指令,用于添加NDK API。语法为:find_library(<VAR> name1 path1 path2 ...)。例如上面的CMakeList.txt文件中,我们就添加了日志支持的API。
  • target_link_libraries 指令 target_link_libraries 指令用来为 target 添加需要链接的共享库,同样也可以用于为自己编写的共享库添加共享库链接。语法为:target_link_libraries(target library <debug | optimized> library2…)

CMake NDK 编译过程

当我们在Android Studio中build我们的NDK工程时,Aandroid Studio会通过上面的步骤为我们设置好交叉编译环境,然后再将CMakelists.txt文件传给 CMake, CMake解析里面的内容,并最终调用不同平台的工具,编译出我们需要的目标环境程序。

总结

通过前文的介绍,主要了解 Android Studio 在开发 NDK 程序时,是怎样使用 CMake Gradle plugin 配置交叉编译环境的,同时了解了 CMake 编译 NDK 程序的基本流程。希望对大家理解最新的 Andriod Studio 是如何使用 CMake进行交叉编译的有所帮助。

[Github Demo下载链接]

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 什么是CMake
  • Android Studio 如何使用 CMake
  • CMake NDK 编译过程
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档