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. 在应用上层获取服务就可以调用到新的接口了。

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

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

相关文章

卷积神经网络(AlexNet)鸟类识别

文章目录 一、前言二、前期工作1. 设置GPU&#xff08;如果使用的是CPU可以忽略这步&#xff09;2. 导入数据3. 查看数据 二、数据预处理1. 加载数据2. 可视化数据3. 再次检查数据4. 配置数据集 三、AlexNet (8层&#xff09;介绍四、构建AlexNet (8层&#xff09;网络模型五、…

微信小程序image组件图片设置最大宽度 宽高自适应

问题描述&#xff1a;在使用微信小程序image组件的时候&#xff0c;在不确定图片宽高情况下 想给一个最大宽度让图片自适应&#xff0c;按比例&#xff0c;image的widthfiex和heightFiex并不能满足&#xff08;只指定最大宽/高并不会生效&#xff09; 问题解决&#xff1a;使用…

居家适老化设计第二十九条---卫生间之花洒

无电源 灯光显示 无障碍扶手型花洒 以上产品图片均来源于淘宝 侵权联系删除 居家适老化卫生间的花洒通常具有以下特点和功能&#xff1a;1. 高度可调节&#xff1a;适老化卫生间花洒可通过调节高度&#xff0c;满足不同身高的老年人使用需求&#xff0c;避免弯腰或过高伸展造…

【开源】基于Vue.js的固始鹅块销售系统

项目编号&#xff1a; S 060 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S060&#xff0c;文末获取源码。} 项目编号&#xff1a;S060&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 鹅块类型模块2.3 固…

qgis添加xyz栅格瓦片

方式1&#xff1a;手动一个个添加 左侧浏览器-XYZ Tiles-右键-新建连接 例如添加高德瓦片地址 https://wprd01.is.autonavi.com/appmaptile?langzh_cn&size1&style7&x{x}&y{y}&z{z} 双击即可呈现 收集到的一些图源&#xff0c;仅供参考&#xff0c;其中一…

【C++学习手札】模拟实现list

​ &#x1f3ac;慕斯主页&#xff1a;修仙—别有洞天 ♈️今日夜电波&#xff1a;リナリア—まるりとりゅうが 0:36━━━━━━️&#x1f49f;──────── 3:51 &#x1f504; ◀️ ⏸ ▶️…

聊聊httpclient的staleConnectionCheckEnabled

序 本文主要研究一下httpclient的staleConnectionCheckEnabled staleConnectionCheckEnabled org/apache/http/client/config/RequestConfig.java public class RequestConfig implements Cloneable {public static final RequestConfig DEFAULT new Builder().build();pr…

【ARM 嵌入式 编译 Makefile 系列 18 -- Makefile 中的 export 命令详细介绍】

文章目录 Makefile 中的 export 命令详细介绍Makefile 使用 export导出与未导出变量的区别示例&#xff1a;导出变量以供子 Makefile 使用 Makefile 中的 export 命令详细介绍 在 Makefile 中&#xff0c;export 命令用于将变量从 Makefile 导出到由 Makefile 启动的子进程的环…

qgis添加wms服务

例如添加geoserver的wms服务 左右浏览器-WMS/WMTS-右键-新建连接 URL添加geoserver的wms地址 http://{ip}:{port}/geoserver/{workspace}/wms 展开wms目录&#xff0c;双击相应图层即可打开

Spark---基于Yarn模式提交任务

Yarn模式两种提交任务方式 一、yarn-client提交任务方式 1、提交命令 ./spark-submit --master yarn --class org.apache.spark.examples.SparkPi ../examples/jars/spark-examples_2.11-2.3.1.jar 100 或者 ./spark-submit --master yarn–client --class org.apache.s…

三菱PLC应用[集锦]

三菱PLC应用[集锦] 如何判断用PNP还是NPN的个人工作心得 10&#xff5e;30VDC接近开关与PLC连接时&#xff0c;如何判断用PNP还是NPN的个人工作心得: 对于PLC的开关量输入回路。我个人感觉日本三菱的要好得多&#xff0c;甚至比西门子等赫赫大名的PLC都要实用和可靠&#xff01…

vulnhub4

靶机地址: https://download.vulnhub.com/admx/AdmX_new.7z 信息收集 fscan 扫一下 ┌──(kali㉿kali)-[~/Desktop/Tools/fscan] └─$ ./fscan_amd64 -h 192.168.120.138 ___ _ / _ \ ___ ___ _ __ __ _ ___| | __ / /_\/____/ __|/ …

LeetCode | 622. 设计循环队列

LeetCode | 622. 设计循环队列 OJ链接 思路&#xff1a; 我们这里有一个思路&#xff1a; 插入数据&#xff0c;bank往后走 删除数据&#xff0c;front往前走 再插入数据&#xff0c;就循环了 那上面这个方法可行吗&#xff1f; 怎么判断满&#xff0c;怎么判断空&#xff1…

模电知识点总结(二)二极管

系列文章目录 文章目录 系列文章目录二极管二极管电路分析方法理想模型恒压降模型折线模型小信号模型高频/开关 二极管应用整流限幅/钳位开关齐纳二极管变容二极管肖特基二极管光电器件光电二极管发光二极管激光二极管太阳能电池 二极管 硅二极管&#xff1a;死区电压&#xf…

今年注册电气工程师考试乱象及就业前景分析

1、注册电气工程师挂靠价格 # 2011年以前约为5万一年&#xff0c;2011年开始强制实施注册电气执业制度&#xff0c;证书挂靠价格开始了飞涨&#xff0c;2013年达到巅峰&#xff0c;供配电15万一年&#xff0c;发输变电20-25万一年&#xff0c;这哪里是证书&#xff0c;简直就是…

Docker kill 命令

docker kill&#xff1a;杀死一个或多个正在运行的容器。 语法&#xff1a; docker kill [OPTIONS] CONTAINER [CONTAINER...]OPTIONS说明&#xff1a; -s&#xff1a;向容器发送一个信号 描述&#xff1a; docker kill子命令会杀死一个或多个容器。容器内的主进程被发送S…

C语言数组的距离(ZZULIOJ1200:数组的距离)

题目描述 已知元素从小到大排列的两个数组x[]和y[]&#xff0c; 请写出一个程序算出两个数组彼此之间差的绝对值中最小的一个&#xff0c;这叫做数组的距离 。 输入&#xff1a;第一行为两个整数m, n(1≤m, n≤1000)&#xff0c;分别代表数组f[], g[]的长度。第二行有m个元素&a…

如何在Simulink中使用syms?换个思路解决报错:Function ‘syms‘ not supported for code generation.

问题描述 在Simulink中的User defined function使用syms函数&#xff0c;报错simulink无法使用外部函数。 具体来说&#xff1a; 我想在Predefined function定义如下符号函数作为输入信号&#xff0c;在后续模块传入函数参数赋值&#xff0c;以实现一次定义多次使用&#xf…

014:MyString

题目 描述 补足MyString类&#xff0c;使程序输出指定结果 #include <iostream> #include <string> #include <cstring> using namespace std; class MyString {char * p; public:MyString(const char * s) {if( s) {p new char[strlen(s) 1];strcpy(p,…

最小二乘线性回归

​ 线性回归&#xff08;linear regression&#xff09;&#xff1a;试图学得一个线性模型以尽可能准确地预测实际值的输出。 以一个例子来说明线性回归&#xff0c;假设银行贷款会根据 年龄 和 工资 来评估可放款的额度。即&#xff1a; ​ 数据&#xff1a;工资和年龄&…