Android13 新增 Stable AIDL接口

发布于:2025-06-14 ⋅ 阅读:(15) ⋅ 点赞:(0)

需求描述:

我需要在Netd的AIDL中添加新的接口,用来调用iptable命令:
void setupLocalIptables();

背景:

Android 10 添加了对稳定的 Android 接口定义语言 (AIDL) 的支持,这是一种跟踪由 AIDL 接口提供的应用编程接口 (API)/应用二进制接口 (ABI) 的新方法。稳定的 AIDL 与 AIDL 的主要区别如下:

  • 在构建系统中使用 aidl_interfaces 定义接口。
  • 接口只能包含结构化数据。对于代表所需类型的 Parcelable,系统会根据其 AIDL 定义自动创建,并自动对其进行编组和解组。
  • 可以将接口声明为“稳定”接口(向后兼容)。声明之后,会在 AIDL 接口旁的一个文件中对这些接口的 API 进行跟踪和版本编号。

问题:

1.直接编译 ==> 提示 AIDL API 被改变了


在INetd.aidl中添加setupLocalIptable()接口后,使用如下指令编译模块

mmm /frameworks/libs/net/common/netd

此时会出现报错提示检测到API改变
在这里插入图片描述
这个错误需要 – 执行 m -update-api,因此使用如下指令更新模块:

make netd_aidl_interface-update-api
2.m -update-api后编译也是有编译错误

在这里插入图片描述
这个错误是因为在AIDL interface 定义的中间添加了新API,打乱了原API顺序。

3.更新接口到AIDL中的步骤
  1. 在原有的AIDL中的最后添加新的API(在最后添加新API,不要加到中间,打乱原API顺序)
  2. 执行 m -update-api ==> 会更新到current目录下的文件
  3. 编译生成对应version的modules
  4. 执行 m -freeze-api固定版本,自动更新到Andrioid.bp中的versions_with_info,以及生成新版本对应的aidl_api<name>\x目录及内容

实现

接下来将实现完整的添加新API及并调用新API的流程。
在这里插入图片描述
在AIDL最后添加新API,并使用如下指令编译模块:

mmm /frameworks/libs/net/common/netd

报错提示说需要更新API:

API dump for the current version of AIDL interface setHardwareAddress does not exist.
Run m netd_aidl_interface-update-api or add unstable: true to the build rule for 
the interface if it does not need to be versioned

按照提示使用如下指令更新API:

make netd_aidl_interface-update-api

执行此命令更新当前API后会更新如下目录下的AIDL文件
xxx\frameworks\libs\net\common\netd\aidl_api\netd_aidl_interface\current

因为是在原来的AIDL文件内新增的接口, 所以需要使用如下指令重新生成一个version的API:

make netd_aidl_interface-freeze-api

此命令会生成新的版本号的API。之前最大是10,更新以后多了11
xxx\frameworks\libs\net\common\netd\aidl_api\netd_aidl_interface\11
在这里插入图片描述
然后再重新编译模块:

mmm /frameworks/libs/net/common/netd

在如下路径会生成该版本的aidl对应的java文件和.h文件。 xxx\out\soong.intermediates\frameworks\libs\net\common\netd\netd_aidl_interface-V11-java-source\gen\android\net xxx\out\soong.intermediates\frameworks\libs\net\common\netd\netd_aidl_interface-V11-cpp-source\gen\include\android\net
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述可以打开.h文件找到新定义的方法
在这里插入图片描述
经过如上操作就可以实现该方法了
在这里插入图片描述
service层就实现好了,编译模块然后上层就可以调用了。
编译模块的时候,可能会出现如下报错:

FAILED: out/soong/apex/depsinfo/new-allowed-deps.txt.check if grep -v '^#' packages/modules
/common/build/allowed_deps.txt | diff -B - out/soong/apex/depsinfo/new-allowed-deps.txt; 
then touch out/soong/apex/depsinfo/new-allowed-deps.txt.check; 
else echo -e "\n******************************"; 
echo "ERROR: go/apex-allowed-deps-error"; 
echo "******************************"; 
echo "Detected changes to allowed dependencies in updatable modules."; 
echo "To fix and update packages/modules/common/build/allowed_deps.txt, please run:"; 
echo -e "$ (croot && packages/modules/common/build/update-apex-allowed-deps.sh)\n"; 
echo "When submitting the generated CL, you must include the following information"; 
echo "in the commit message if you are adding a new dependency:"; 
echo "Apex-Size-Increase:"; 
echo "Previous-Platform-Support:"; 
echo "Aosp-First:"; 
echo "Test-Info:"; 
echo "You do not need OWNERS approval to submit the change, but mainline-modularization@"; 
echo "will periodically review additions and may require changes."; 
echo -e "******************************\n"; exit 1; fi; 
738a739,740 > netd_aidl_interface-V11-java(minSdkVersion:29) > 
netd_aidl_interface-V11-ndk(minSdkVersion:29) 
************************** ERROR: go/apex-allowed-deps-error **************************
Detected changes to allowed dependencies in updatable modules. 
To fix and update packages/modules/common/build/allowed_deps.txt, 
please run: $ (croot && packages/modules/common/build/update-apex-allowed-deps.sh) 
When submitting the generated CL, you must include the following information 
in the commit message if you are adding a new dependency: 
Apex-Size-Increase: Previous-Platform-Support: Aosp-First: Test-Info: 
You do not need OWNERS approval to submit the change, 
but mainline-modularization@ will periodically review additions and may require changes. 
******************************

可以看到提示,需要在系统添加如下声明:

netd_aidl_interface-V11-java(minSdkVersion:29)
netd_aidl_interface-V11-ndk(minSdkVersion:29)

如上声明可以使用脚本自动生成,运行如下python脚本就可以了:

croot && packages/modules/common/build/update-apex-allowed-deps.sh

如上就可以编译模块成功了,编译成功以后,就可以通过INetd 调用该函数了。
在这里插入图片描述
在调试过程中,可能会遇到的如下报错:
· Backward incompatible change detected on AIDL API
在这里插入图片描述
该问题的原因是新增的方法加在了中间,导致原始的API顺序产生了变化。这时需要把新增的接口,加在最后面,然后使用make netd_aidl_interface-update-api 更新current
· ERROR: Modification detected of stable AIDL API file
在这里插入图片描述
该问题产生的原因是因为手动修改xxx\frameworks\libs\net\common\netd\aidl_api\netd_aidl_interface\10 目录下的AIDL文件,导致hash值更改了,后面通过make netd_aidl_interface-freeze-api 生成新的API,后续更改应用的版本就可以了。
· java层通过INetd 调用不到新增的方法
在这里插入图片描述
该问题的原因为:frameworks\libs\net\common\netd\Android.bp 使用的AIDL版本为10,而新增的AIDL接口是在11版本里面
在这里插入图片描述
所以需要将这里的10修改成11。
在这里插入图片描述
如上即为新增AIDL接口遇到的全部流程。