1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Android NDK开发一 NDK环境搭建及cmake简介

Android NDK开发一 NDK环境搭建及cmake简介

时间:2018-12-20 22:23:17

相关推荐

Android NDK开发一 NDK环境搭建及cmake简介

1 前言

关于NDK的介绍可以查看官方的介绍:

/ndk/guides/index.html

一句话总结NDK:NDK(Native Development Kit) : 原生开发工具包,即帮助开发原生代码的一系列工具,包括但不限于编译工具、一些公共库、开发IDE等。

现在NDK的应用也越来越广泛,作为一个App开发者,对NDK也必须有一定的掌握,NDK在app主要有以下几方面的应用:

1 直播的推流

2 图片压缩 jpeg压缩

3 加密:例如AES加密等

4 算法类:例如图像处理的算法,视频编解码的算法ffmpeg等

5 其他的需要使用c/c++开发的场景:例如增量更新等

开发NDK时,我们会把c/cpp文件编译成so库,然后供上层的java调用,在早期的编译中一般使用ndk-build,在android studio2.2以后就可以使用cmake了。google现在也主推cmake,因此本文也介绍使用cmake来开发NDK。

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。只是 CMake 的组态档取名为 CMakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者可以用标准的方式建构他的软件,这种可以使用各平台的原生建构系统的能力是 CMake 和 SCons 等其他类似系统的区别之处。

NDK开发需要具备以下知识:

1 基本的c/c++知识:例如指针的用法,智能指针

2 JNI的基本知识:JNI的数据类型,参数类型,方法签名以及调用java对象等

3 熟悉至少一种编译工具ndk-build或者cmake的使用

2 NDK环境搭建

android 工程支持NDK开发的的搭建还是比较简单的,一般有以下几个方面

1 下载响应的NDK开发支持的组件 Android SDK 界面

下载Cmake,LLDB,NDK这三个组件或包

2 如果是新建工程的话,勾上Include C++ Support,这样能自动的使app module支持NDK开发

这样我们一路新建工程后,一般来说会有这些变化

工程目录变化如下,多了一个cpp目录,这里主要放的就是我们的c/c++文件

另外。app module的gradle变化如下:

可以看到,在gradle中多了

defaultConfig {......externalNativeBuild {cmake {cppFlags ""}}}......externalNativeBuild {cmake {path "CMakeLists.txt"}}

另外在app mudule 的gradle同级目录下还多了一个CMakeLists.txt文件,这个文件主要用来描述如何编译c/c++代码的

3 对于一个存在的APP工程,如果Android Studio 版本大于2.2,那么可以使用如下的方法支持NDK开发

local.properties配置好ndk的SDK路径:ndk.dir=D:\Android\sdk\ndk-bundle在module的gradle中增加如下描述即可

defaultConfig {......externalNativeBuild {cmake {cppFlags ""}}}......externalNativeBuild {cmake {path "CMakeLists.txt"}}

在module 层的目录下新建一个空的CMakeLists.txt

在src/main目录下新建cpp目录,并修改gradle中如下:

android {compileSdkVersion 25buildToolsVersion '25.0.3'defaultConfig {minSdkVersion 16targetSdkVersion 25versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags ""}}}externalNativeBuild {cmake {path "CMakeLists.txt"}}sourceSets {main {jni.srcDirs = ['src/main/cpp/']}}}

同步一下工程,如果有编译错误,解决编译错误

这样,一个以前不支持NDK开发的APP工程,现在也可以支持NDK开发了

3 cmake编译简介

1 新建的CMakeLists.txt简要介绍

CMakeLists.txt文件用于配置JNI项目属性,主要用于声明CMake使用版本、so库名称、C/CPP文件路径等信息,下面是该文件内容:

# Sets the minimum version of CMake required to build the native# library. You should either keep the default value or only pass a# value of 3.4.0 or lower.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 it 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).# Associated headers in the same location as their source# file are automatically included.src/main/cpp/native-lib.cpp )# Searches for a specified prebuilt library and stores the path as a# variable. Because system libraries are included 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 the# 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(VERSION 3.4.1)

CMake最小版本使用的是3.4.1。

add_library()

配置so库信息(为当前当前脚本文件添加库)

native-lib

这个是声明引用so库的名称,在项目中,如果需要使用这个so文件,引用的名称就是这个。值得注意的是,实际上生成的so文件名称是libnative-lib。当Run项目或者build项目是,在Module级别的build文件下的intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main下会生成相应的so库文件。

SHARED

这个参数表示共享so库文件,也就是在Run项目或者build项目时会在目录intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main下生成so库文。此外,so库文件都会在打包到.apk里面,可以通过选择菜单栏的Build->Analyze Apk…*查看apk中是否存在so库文件,一般它会存放在lib目录下。

src/main/cpp/native-lib.cpp

构建so库的源文件。

STATIC:静态库,是目标文件的归档文件,在链接其它目标的时候使用。

SHARED:动态库,会被动态链接,在运行时被加载。

MODULE:模块库,是不会被链接到其它目标中的插件,但是可能会在运行时使用dlopen-系列的函数动态链接。

更详细的解释请参考这篇文章:C++静态库与动态库

find_library()

这个方法与我们要创建的so库无关而是使用NDK的Apis或者库,默认情况下Android平台集成了很多NDK库文件,所以这些文件是没有必要打包到apk里面去的。直接声明想要使用的库名称即可(猜测:貌似是在Sytem/libs目录下)。在这里不需要指定库的路径,因为这个路径已经是CMake路径搜索的一部分。如示例中使用的是log相关的so库。

log-lib

这个指定的是在NDK库中每个类型的库会存放一个特定的位置,而log库存放在log-lib中

log

指定使用log库

target_link_libraries()

如果你本地的库(native-lib)想要调用log库的方法,那么就需要配置这个属性,意思是把NDK库关联到本地库。

native-lib

要被关联的库名称

${log-lib}

要关联的库名称,要用大括号包裹,前面还要有$符号去引用。

gradle脚本引用CMakeLists.txt文件

实际上,我们可以自己创建CMakeLists.txt文件,而且路径不受限制,只要在build.gradle中配置externalNativeBuild.cmake.path来指定该文件路径即可。

当Run或者Build项目时,想要执行CMakeLists.txt构建脚本,需要把脚本配置到模块级的build.gradle中。

android {externalNativeBuild {cmake {path "CMakeLists.txt"}}}

2 cmake相关资料收集

我们是使用cmake开发时,不可避免的会查询一些资料,以下资料非常有帮助

Android NDK-CMake文档/ndk/guides/cmake.html

CMake 的官方文档/documentation/

CMake中文简易手册/khan-lau/note/254724

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。