Android JNI系列详解之CMake编译工具的使用

一、CMake工具的介绍

如图所示,CMake工具的主要作用是,将C/C++编写的native源文件编译打包生成库文件(包含动态库或者静态库文件),集成到Android中使用。

二、CMake编译工具的使用

 使用主要是配置两个文件:CMakeList.txt和build.gradle

1、CMakeList.txt介绍

文件路径如下:

 文件默认的内容如下:(没加#号的都是配置)

cmake_minimum_required(VERSION 3.22.1)# Declares and names the project.project("testnative")# 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.testnative# 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.testnative# Links the target library to the log library# included in the NDK.${log-lib})

1.cmake_minimum_required(VERSION 3.22.1)

表示项目支持的最低cmake工具版本是3.22.1

2.

add_library( # Sets the name of the library.testnative# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).native-lib.cpp)

表示的是添加库,定义生成的库的名称、类型、指定的源码

testnative就是指定库的名称;

SHARED就是指定生成库的类型:动态|静态;

native-lib.cpp就是要编译的源码的相对CMakeList.txt的路径,相对路径;

3.

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)

表示引入库,这里是指定了ndk里面的log库,并将库的路径赋值给了log-lib变量,相当于java中定义了一个变量log-lib,值是log。

4.

target_link_libraries( # Specifies the target library.testnative# Links the target library to the log library# included in the NDK.${log-lib})

就是链接库。将特定库链接到目标库,这是是将上面定义的log-lib链接到我们的testnative,这里的${log-lib}就是将log-lib的值取出来,链接到我们的testnative,这样我们就可以使用log库了。

2、module下的build.gradle配置了CMake的介绍

文件路径如下:

 打开文件看下如下配置:

 CMake在android{}闭包中做了如下配置:

externalNativeBuild {cmake {path file('src/main/cpp/CMakeLists.txt')version '3.22.1'}}

这里这个配置,配置了CMakeList.txt文件的路径(路径是相对于项目的)和编译使用的cmake的版本。

另外我们还可以在defaultConfig闭包下配置其他信息(C/C++标准库的支持版本),如下:

 上面这个配置可以不写,使用默认的就行。以下是一个配置样例:

这里配置的C++标准库是动态类型(静态库就不需要指定),这样编译后会在build中生成一个标准库文件。

加之前的是:

加之后的是:

 这里顺便介绍一下C++标准库是什么:

 三、Android中如何生成的so库

 以上代码就能将so库加载到程序中,然后就可以调用相关的native的api,从而就基于JNI来调用so库里面的功能。

编写相关的代码:

在Java端,我们编写一个native的方法:

 对应到C/C++中的代码就是:

 Java端通过调用stringFromJNI()就能调用到C/C++端的这段代码,这就是JNI的使用。

效果:Android界面显示了C++函数中的字符串。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/90115.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nodepad++ 插件的安装

nodepad 插件的安装 一、插件安装二、安装插件:Json Viewer nodepad 有 插件管理功能,其中有格式化json以及可以将json作为树查看的插件: Json Viewer 一、插件安装 1、首先下载最新的notepad 64位【https://notepad-plus.en.softonic.com…

echart 图表添加数据分析功能,(右上控制选择)

echart 图表添加数据分析功能,可区域选择数据,右上按钮,控制echart行为 chart.on(globalcursortaken, onGlobalcursortaken); //绑定事件chart.off("brushSelected");//解绑事件处理函数chart.on(brushSelected, renderBrushed);getBarDev2(eIndex, eTimeArr, eSerie…

最新ChatGPT程序源码+AI系统+详细图文部署教程/支持GPT4.0/支持Midjourney绘画/Prompt知识库

一、AI系统 如何搭建部署人工智能源码、AI创作系统、ChatGPT系统呢?小编这里写一个详细图文教程吧!SparkAi使用Nestjs和Vue3框架技术,持续集成AI能力到AIGC系统! 1.1 程序核心功能 程序已支持ChatGPT3.5/GPT-4提问、AI绘画、Mi…

流媒体弱网优化之路(BBR应用)——GCC与BBR的算法思想分析

流媒体弱网优化之路(WebRTC)——GCC与BBR的算法思想分析 —— 我正在的github给大家开发一个用于做实验的项目 —— github.com/qw225967/Bifrost目标:可以让大家熟悉各类Qos能力、带宽估计能力,提供每个环节关键参数调节接口并实现一个json全配置&…

Blender 3D建模要点

3d模型可以为场景的仿真模拟带来真实感,它还有助于更轻松地识别场景中的所有内容。 例如,如果场景中的所有对象都是简单的形状,如立方体和圆形,则很难在仿真中区分对象。 1,碰撞形状与视觉形状 像立方体和球体这样的简单形状,通常被称为“基本体”,通常用作碰撞块。 与…

912.排序数组

目录 一、题目 二、代码 一、题目 912. 排序数组 - 力扣&#xff08;LeetCode&#xff09; 二、代码 class Solution { public:void _MergeSort(vector<int>&data,vector<int>&tmp,int begin,int end){if(begin>end)return;//结束条件int mid (beg…

windows安装Scala

Windows安装Scala 下载地址&#xff1a;https://downloads.lightbend.com/scala/2.11.11/scala-2.11.11.zip 解压完成之后 配置环境变量

接口幂等性设计的最佳实现

一、什么是幂等 二、为什么需要幂等 三、接口超时了&#xff0c;到底如何处理&#xff1f; 四、如何设计幂等 全局的唯一性ID 幂等设计的基本流程 五、实现幂等的8种方案 selectinsert主键/唯一索引冲突 直接insert 主键/唯一索引冲突 状态机幂等 抽取防重表 token令牌 悲观锁…

【数据结构】二叉数的存储与基本操作的实现

文章目录 &#x1f340;二叉树的存储&#x1f333;二叉树的基本操作&#x1f431;‍&#x1f464;二叉树的创建&#x1f431;‍&#x1f453;二叉树的遍历&#x1f3a1;前中后序遍历&#x1f4cc;前序遍历&#x1f4cc;中序遍历&#x1f4cc;后续遍历 &#x1f6eb;层序遍历&am…

《异常检测——从经典算法到深度学习》22 Kontrast: 通过自监督对比学习识别软件变更中的错误

《异常检测——从经典算法到深度学习》 0 概论1 基于隔离森林的异常检测算法 2 基于LOF的异常检测算法3 基于One-Class SVM的异常检测算法4 基于高斯概率密度异常检测算法5 Opprentice——异常检测经典算法最终篇6 基于重构概率的 VAE 异常检测7 基于条件VAE异常检测8 Donut: …

自平衡性:保持数据结构稳定的关键

自平衡性是一种重要的数据结构属性&#xff0c;它确保在执行插入、删除等操作后&#xff0c;数据结构能够自动进行调整&#xff0c;以保持整体的平衡状态。平衡的数据结构可以提供更快的操作性能&#xff0c;避免极端情况下的低效操作&#xff0c;同时保持树或其他结构的整体稳…

iptables教程

iptables netfilter/iptables&#xff08;简称iptables&#xff09;是与2.4.x和2.6.x系列版本Linux内核集成的IP信息包过滤系统。 Iptables Tutorial 1、表和链 1.1、表 iptables会根据不同的数据包处理功能使用不同的规则表。它包括如下五个表&#xff1a;filter、nat和m…