当前位置: 首页 > news >正文

Android 框架层AIDL 添加接口

文章目录

      • AIDL的原理
      • 构建AIDL的流程
      • 往冻结的AIDL中加接口

AIDL的原理

可以利用ALDL定义客户端与服务均认可的编程接口,以便二者使用进程间通信 (IPC) 进行相互通信。在 Android 中,一个进程通常无法访问另一个进程的内存。因此,为进行通信,进程需将其对象分解成可供操作系统理解的原语,并将其编组为可供您操作的对象。编写执行该编组操作的代码较为繁琐,因此 Android 会使用 AIDL 为您处理此问题。

AIDL 可以理解成是一个范式, 通过这个范式编写接口文件, 然后利用Android的AIDL工具 会生成继承binder所需要能力的头文件。

构建AIDL的流程

以automotive的audiocontrol模块为例

  1. 编写AIDL接口文件,编写Android.bp, 通过AIDL 生成头文件
    其aidl的文件位于下面的目录
hardware/interfaces/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/

编译会在下面的目录生成实现binder通信的接口文件。
接口文件有java cpp ndk三种类型。 使得能够被不同的客户端和服务端的代码引用到。

out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-V2-cpp-source/gen/include/android/hardware/automotive/audiocontrol$ ls
AudioFocusChange.h    BnFocusListener.h     BpDuckingInfo.h    IAudioControl.h
BnAudioControl.h      BnMutingInfo.h        BpFocusListener.h  IFocusListener.h
BnAudioFocusChange.h  BpAudioControl.h      BpMutingInfo.h     MutingInfo.h
BnDuckingInfo.h       BpAudioFocusChange.h  DuckingInfo.h
  1. 实现service,实现service对应的bin,以及rc,注册服务到servicemananger
    AudioContro 实现的demo bin位于hardware/interfaces/automotive/audiocontrol/aidl/default
    目录下,编译会生成
    android.hardware.automotive.audiocontrol-service.example这样的bin 这个bin在 audiocontrol-default.rc 中启动。
    当然服务端的是 就是把audiocontrol的服务注册到servicemanger中。
    std::shared_ptr<AudioControl> audioControl = ::ndk::SharedRefBase::make<AudioControl>();const std::string instance = std::string() + AudioControl::descriptor + "/default";binder_status_t status =AServiceManager_addService(audioControl->asBinder().get(), instance.c_str());CHECK_EQ(status, STATUS_OK);

服务的名字在audiocontrol-default.xml中定义为
android.hardware.automotive.audiocontrol.IAudioControl/default

  1. 实现client,获取service,调用service相关的接口。

clinet 端 主要是通过名字从serviceManager 中获取到audioControl的服务。然后通过服务调用其接口。
如下通过getService 获取服务,然后通过名字获取IAudioControl对象。然后就可以调用其函数了

    private static final String AUDIO_CONTROL_SERVICE ="android.hardware.automotive.audiocontrol.IAudioControl/default";private IBinder mBinder;private IAudioControl mAudioControl;private boolean mListenerRegistered = false;private AudioControlDeathRecipient mDeathRecipient;static @Nullable IBinder getService() {return Binder.allowBlocking(ServiceManager.waitForDeclaredService(AUDIO_CONTROL_SERVICE));}AudioControlWrapperAidl(IBinder binder) {mBinder = Objects.requireNonNull(binder);mAudioControl = IAudioControl.Stub.asInterface(binder);}IBinder binder = AudioControlWrapperAidl.getService();
if (binder != null) {return new AudioControlWrapperAidl(binder);
}@Overridepublic void onAudioFocusChange(@AttributeUsage int usage, int zoneId, int focusChange) {if (Slogf.isLoggable(TAG, Log.DEBUG)) {Slogf.d(TAG, "onAudioFocusChange: usage " + usageToString(usage)+ ", zoneId " + zoneId + ", focusChange " + focusChange);}try {String usageName = usageToXsdString(usage);mAudioControl.onAudioFocusChange(usageName, zoneId, focusChange);} catch (RemoteException e) {throw new IllegalStateException("Failed to query IAudioControl#onAudioFocusChange", e);}}

往冻结的AIDL中加接口

按照Android规则来说 发布之后的AIDL接口是不能修改的。 有相应的Freeze AIDL APIs处理。 从提交记录看 freeze 的操作是加hash值。

diff --git a/automotive/audiocontrol/aidl/Android.bp b/automotive/audiocontrol/aidl/Android.bp
index 7a947d3ab..4acfd82d6 100644
--- a/automotive/audiocontrol/aidl/Android.bp
+++ b/automotive/audiocontrol/aidl/Android.bp
@@ -19,4 +19,5 @@ aidl_interface {
sdk_version: "module_current",
},
},
+ versions: ["1"],
}
diff --git a/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash b/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash
new file mode 100644
index 000000000..c4bb36b47
--- /dev/null
+++ b/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash
@@ -0,0 +1 @@
+ba2a7caca61683385b3b100e4faab1b4139fc547

看提交记录加接口的地方:

  1. /aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
  2. /aidl/default/AudioControl.h
  3. /aidl/default/AudioControl.cpp
diff --git a/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl b/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
index 4b03af11a..3a0224557 100644
--- a/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidl
+++ b/automotive/audiocontrol/aidl/android/hardware/automotive/audiocontrol/IAudioControl.aidldiff --git a/automotive/audiocontrol/aidl/default/AudioControl.cpp b/automotive/audiocontrol/aidl/default/AudioControl.cpp
index 748947cb2..b076d0128 100644
--- a/automotive/audiocontrol/aidl/default/AudioControl.cpp
+++ b/automotive/audiocontrol/aidl/default/AudioControl.cppdiff --git a/automotive/audiocontrol/aidl/default/AudioControl.h b/automotive/audiocontrol/aidl/default/AudioControl.h
index cf5694762..ab0b1b305 100644
--- a/automotive/audiocontrol/aidl/default/AudioControl.h
+++ b/automotive/audiocontrol/aidl/default/AudioControl.h
  • 出现AIDL修改报错
Above AIDL file(s) has changed and this is NEVER allowed on a release platform
(i.e., PLATFORM_VERSION_CODENAME is REL). If a device is shipped with this
change by ignoring this message, it has a high risk of breaking later when a
module using the interface is updated, e.g., Maineline modules.
11:47:01 ninja failed with: exit status 1

报错的原因解释:
stability :此接口的稳定性承诺的可选标志。目前仅支持"vintf" 。如果未设置,则对应于在此编译上下文中具有稳定性的接口(因此此处加载的接口只能与一起编译的东西一起使用,例如在 system.img 上)。如果将其设置为"vintf" ,则这对应于稳定性承诺:接口必须在使用期间保持稳定。

解决: 所有的AIDL有关地方的接口都要增加。

  • 出现hash校验错误:
hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocont FAILED: out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp if [ $(cd 'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1' && { find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1) = $(read -r <'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash' hash extra; printf %s $hash) ]; then touch out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp; else cat 'system/tools/aidl/build/message_check_integrity.txt' && exit 1; fi ############################################################################### # ERROR: Modification detected of stable AIDL API file # ############################################################################### Above AIDL file(s) has changed, resulting in a different hash. Hash values may be checked at runtime to verify interface stability. If a device is shipped with this change by ignoring this message, it has a high risk of breaking later when a module using the interface is updated, e.g., Mainline modules. 16:41:52 ninja failed with: exit status 1

错误原因:

构建过程未能验证 AIDL 文件的哈希值,表明发生了修改。
哈希检查脚本:bash

if [ $(cd 'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1' && { find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1) = $(read -r <'hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1/.hash' hash extra; printf %s $hash) ]; then touch out/soong/.intermediates/hardware/interfaces/automotive/audiocontrol/aidl/android.hardware.automotive.audiocontrol-api/checkhash_1.timestamp; else cat 'system/tools/aidl/build/message_check_integrity.txt' && exit 1; fi

这是检查 AIDL 文件哈希值是否与预期哈希值匹配的脚本。如果哈希值匹配,构建过程将继续进行;否则,将引发错误。错误消息:makefile
ERROR: Modification detected of stable AIDL API file

  • 修改hash值。
    根据报错的提交脚本。使用下面的脚本在对应的目录下生成hash 值, 将这个hash值替换到.hash文件即可
    目录hardware/interfaces/automotive/audiocontrol/aidl/aidl_api/android.hardware.automotive.audiocontrol/1
{ find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version; } | sha1sum | cut -d " " -f 1
  • 出现这个错误

Android will be dropped but finished with status UNKNOWN_TRANSACTION
需要push 所有system/lib底下有关的audiocontrol的so、

使用版本化接口接口方法在运行时,当尝试在旧服务器上调用新方法时,新客户端会收到错误或异常,具体取决于后端。
cpp后端获取::android::UNKNOWN_TRANSACTION 。
ndk后端获取STATUS_UNKNOWN_TRANSACTION 。
java后端获取android.os.RemoteException并显示一条消息,说明 API 未实现。

总结: 在冻结的AIDL接口上面加新的接口 需要做的步骤。 但是强烈不建议这么做,可以自己单独实现一个AIDL接口、AIDL的服务、以及上层的实现

  1. 修改AIDL文件 添加接口
  2. 计算Hash,修改hash 值
  3. 编译push 生成的so
  4. 在应用上层获取服务就可以调用到新的接口了。

相关文章:

Android 框架层AIDL 添加接口

文章目录 AIDL的原理构建AIDL的流程往冻结的AIDL中加接口 AIDL的原理 可以利用ALDL定义客户端与服务均认可的编程接口&#xff0c;以便二者使用进程间通信 (IPC) 进行相互通信。在 Android 中&#xff0c;一个进程通常无法访问另一个进程的内存。因此&#xff0c;为进行通信&a…...

ubuntu命令行下中文乱码怎么解决

大家好,今天来介绍ubuntu命令行中文乱码怎么解决(ubuntu中文文件名乱码)的问题,以下是渲大师小编对此问题的归纳和整理,感兴趣的来一起看看吧! ubuntu命令行下中文乱码怎么解决 我也呀见过这个问题 一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字谈码符编码为zh_CN.UT…...

沈阳陪诊系统|陪诊软件开发功能

陪诊小程序的出现它可以帮助患者或家属解决就医过程中的各种问题。根据数据显示&#xff0c;2021年中国陪诊市场规模约为36.7亿元&#xff0c;预计到2025年将达到100亿元。同时&#xff0c;在医疗行业数字化转型的大背景下&#xff0c;陪诊微信小程序作为一种创新的医疗服务模式…...

Element-Plus 表格 el-table 如何支持分页多选

&#x1f680; 作者主页&#xff1a; 有来技术 &#x1f525; 开源项目&#xff1a; youlai-mall &#x1f343; vue3-element-admin &#x1f343; youlai-boot &#x1f33a; 仓库主页&#xff1a; Gitee &#x1f4ab; Github &#x1f4ab; GitCode &#x1f496; 欢迎点赞…...

网站被流量攻击了,该怎么处理

几乎每个网站都面临被攻击或者入侵的风险&#xff0c;无论是简单的博客论坛、投资平台、小型的独立电商网站还是动态电子商务平台都有被攻击的情况出现,只是或大或小,或多或少罢了 为什么网站会被攻击&#xff1f;黑客如何来入侵这些网站&#xff1f;如何才能有效保护我的网站不…...

配置特定 IP 地址走指定网关

公司有两个日常上网用的路由器&#xff0c;分别接不同的两条网线&#xff0c;虽然都是电信的&#xff0c;但是一条偶尔会抽风&#xff0c;我的 VPS 会连不上&#xff0c;也就是挂在上面的 SS 无法使用。恰好这根线是公司接台式机的&#xff0c;也就是说平时上班偶尔会无法科学上…...

Linux的基本指令(四)

目录 前言 时间相关的指令 date指令 时间戳 日志 时间戳转化为具体的时间 cal指令 find指令&#xff08;十分重要&#xff09; grep指令&#xff08;行文本过滤工具&#xff09; 学前补充 什么是打包和压缩&#xff1f; 为什么要打包和压缩&#xff1f; 怎么打包和…...

vue+elementui如何实现在表格中点击按钮预览图片?

效果图如上&#xff1a; 使用el-image-viewer 重点 &#xff1a; 引入 import ElImageViewer from "element-ui/packages/image/src/image-viewer"; <template><div class"preview-table"><el-table border :data"tableData" …...

LLaMA 2:开源的预训练和微调语言模型推理引擎 | 开源日报 No.86

facebookresearch/llama Stars: 36.0k License: NOASSERTION LLaMA 2 是一个开源项目&#xff0c;用于加载 LLaMA 模型并进行推理。 该项目的主要功能是提供预训练和微调后的 LLaMA 语言模型的权重和起始代码。这些模型参数范围从 7B 到 70B 不等。 以下是该项目的关键特性…...

01-AI大模型智能客服 V0.1「上」

你好&#xff0c;我是悦创。 首发&#xff1a;https://mp.weixin.qq.com/s/6MTkpWZCEbFWOcUn0Vexvw V0.1 版本我将分为上中下三篇进行书写和发布&#xff0c;欢迎分享和我微信进讨论群&#xff1a;Jiabcdefh。 计划&#xff1a; 会迭代好几个版本&#xff0c;看阅读量和点赞…...

【23真题】罕见211!数一配英二!

今天分享的是23年合肥工业大学833的信号与系统数字信号处理试题及解析。合工大833考数一英二&#xff0c;这样的搭配还是很少见的。 本套试卷难度分析&#xff1a;22年合肥工业大学833考研真题&#xff0c;我也发布过&#xff0c;若有需要&#xff0c;戳这里自取!平均分为80和…...

Linux 项目自动化构建工具:make/makefile

什么是 make make 是一个命令&#xff0c;他会在源文件的当前目录下寻找 makefile 或者 Makefile 文件执行这个文件中的代码。 makefile 文件的编写 我们先来见见猪跑&#xff0c;看看 make 怎么用的&#xff1a; 下面是 makefile 文件的内容&#xff1a; 这是 test.c 中的…...

android trace文件的抓取与查看方法

本地手机抓取trace 解压android trace文件的抓取与查看方法 找到config.pbtx文件&#xff0c;连接手机push进去 # push config.pbtx &#xff0c;/data/local/tmp/为自定义push到的目录 adb push config.pbtx /data/local/tmp/ adb shell # 抓取trace&#xff0c; /data/loc…...

ffmpeg开发 环境配置

ffmpeg开发简图 1 下载ffmpeg开发包 https://ffmpeg.org/download.html 包含三个版本&#xff1a;Static、Shared以及Dev Static --- 包含3个应用程序&#xff1a;ffmpeg.exe , ffplay.exe , ffprobe.exe&#xff0c;体积都很大&#xff0c;相关的DLL已经被编译到exe里面去…...

C++STL——string类详解及其模拟实现

CSTL——string类 1. STL简介 STL全称standard template libaray&#xff0c;译为标准模板库 需要注意&#xff0c;STL不是C的标准库&#xff0c;而是C标准库的重要组成部分STL是一个包含众多数据结构和算法的软件框架 下面展示STL的六大组件&#xff1a; 本章&#xff0c;我…...

使用Three.js创建导航立方体

什么是导航立方体? 导航立方体是一个交互式的3D控件,它允许用户通过点击和拖动立方体的各个面来改变3D视图的方向。这是一种非常直观的方式,让用户能够轻松地在3D空间中导航。 创建导航立方体 下面是一个基本的步骤,说明如何使用Three.js创建一个导航立方体: // 创建场景…...

C++初识类和对象

前言 上一期我们介绍了一些C入门的基础知识&#xff0c;本期我们来介绍面向对象。初步认识一下面向对象和面向过程、类、以及封装&#xff01; 本期内容介绍 面向过程和面向对象 类的引入 类的定义 类的访问限定符和封装 类的作用域 类的实例化 类对象模型 this指针 一、面向…...

MYSQL where 子句

文章目录 前言MySQL where 子句语法 从命令提示符中读取数据使用PHP脚本读取数据后言 前言 hello world欢迎来到前端的新世界 &#x1f61c;当前文章系列专栏&#xff1a;Mysql &#x1f431;‍&#x1f453;博主在前端领域还有很多知识和技术需要掌握&#xff0c;正在不断努力…...

系列十六、Spring IOC容器的扩展点

一、概述 Spring IOC容器的扩展点是指在IOC加载的过程中&#xff0c;如何对即将要创建的bean进行扩展。 二、扩展点 2.1、BeanDefinitionRegistryPostProcessor 2.1.1、概述 BeanDefinitionRegistryPostProcessor是bean定义的后置处理器&#xff0c;在BeanDefinition加载后&a…...

eclipse项目移到idea上部署运行

1.配置web模块 另外&#xff0c;模块这里&#xff0c;也要加上Spring 2.配置Artifact &#xff08;用于tomcat&#xff09; 就是从上面配置的web模块&#xff0c;产生的工件 3.添加lib 一般是在web-inf/lib &#xff0c; 遇到的坑&#xff1a; jdk版本问题&#xff0c;这里…...

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周&#xff0c;有很多同学在写期末Java web作业时&#xff0c;运行tomcat出现乱码问题&#xff0c;经过多次解决与研究&#xff0c;我做了如下整理&#xff1a; 原因&#xff1a; IDEA本身编码与tomcat的编码与Windows编码不同导致&#xff0c;Windows 系统控制台…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

mac 安装homebrew (nvm 及git)

mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用&#xff1a; 方法一&#xff1a;使用 Homebrew 安装 Git&#xff08;推荐&#xff09; 步骤如下&#xff1a;打开终端&#xff08;Terminal.app&#xff09; 1.安装 Homebrew…...

适应性Java用于现代 API:REST、GraphQL 和事件驱动

在快速发展的软件开发领域&#xff0c;REST、GraphQL 和事件驱动架构等新的 API 标准对于构建可扩展、高效的系统至关重要。Java 在现代 API 方面以其在企业应用中的稳定性而闻名&#xff0c;不断适应这些现代范式的需求。随着不断发展的生态系统&#xff0c;Java 在现代 API 方…...

用鸿蒙HarmonyOS5实现中国象棋小游戏的过程

下面是一个基于鸿蒙OS (HarmonyOS) 的中国象棋小游戏的实现代码。这个实现使用Java语言和鸿蒙的Ability框架。 1. 项目结构 /src/main/java/com/example/chinesechess/├── MainAbilitySlice.java // 主界面逻辑├── ChessView.java // 游戏视图和逻辑├──…...

小木的算法日记-多叉树的递归/层序遍历

&#x1f332; 从二叉树到森林&#xff1a;一文彻底搞懂多叉树遍历的艺术 &#x1f680; 引言 你好&#xff0c;未来的算法大神&#xff01; 在数据结构的世界里&#xff0c;“树”无疑是最核心、最迷人的概念之一。我们中的大多数人都是从 二叉树 开始入门的&#xff0c;它…...

【记录坑点问题】IDEA运行:maven-resources-production:XX: OOM: Java heap space

问题&#xff1a;IDEA出现maven-resources-production:operation-service: java.lang.OutOfMemoryError: Java heap space 解决方案&#xff1a;将编译的堆内存增加一点 位置&#xff1a;设置setting-》构建菜单build-》编译器Complier...