背景:
第一版本的aidl没有指定版本,只有一个aidl_api下面current文件夹即默认就是版本v1,那么如果后面有相关的接口修改呢?
那么应该怎么办,怎么操作?
修改aidl
在修改接口前记得把老版本的aidl进行冻结一下
冻结命令:
m android.hardware.mytest-freeze-api
执行后相关的aidl_api目录多个一个版本目录
可以看到这里的版本多了版本1,而且还带有hash号文件
接下来既可以修改aidl文件了
package android.hardware.mytest;
import android.hardware.mytest.MyTestObj;
@VintfStability
interface IMyTest {
MyTestObj test();
void addTest(int a,int b); //增加了一个接口方法
}
注意这里修改aidl接口也不是随意修改的哈,官方有相关要求:
In addition, you need to manage ToT version's API definition. Whenever an API is updated, run foo-update-api to update aidl_api/name/current which contains ToT version's API definition.
To maintain the stability of an interface, owners can add new:
Methods to the end of an interface (or methods with explicitly defined new serials)
Elements to the end of a parcelable (requires a default to be added for each element)
Constant values
In Android 11, enumerators
In Android 12, fields to the end of a union
No other actions are permitted, and no one else can modify an interface (otherwise they risk collision with changes that an owner makes).
大概就是修改时候一定要考虑老版本的兼容问题,一般不删除原来接口和修改
然后执行命令:
m android.hardware.mytest-update-api
可以看到out临时文件目录多了V2相关的库
test@test:~/nx563j_xiaomi/out/soong/.intermediates/hardware/interfaces/mytest/aidl$ ll
total 40
drwxrwxr-x 10 test test 4096 Dec 13 20:57 ./
drwxrwxr-x 3 test test 4096 Dec 13 20:57 ../
drwxrwxr-x 4 test test 4096 Dec 13 20:57 android.hardware.mytest-V1-ndk/
drwxrwxr-x 3 test test 4096 Dec 13 20:57 android.hardware.mytest-V1-ndk-source/
drwxrwxr-x 4 test test 4096 Dec 13 20:57 android.hardware.mytest-V2-ndk/
drwxrwxr-x 3 test test 4096 Dec 13 20:57 android.hardware.mytest-V2-ndk-source/
修改实现代码的和android.bp
MyTestImpl.h增加相关方法引入
class MyTestImpl : public BnMyTest {
public:
::aidl::android::hardware::mytest::MyTestObj mObj = {"hello",1};
::ndk::ScopedAStatus test(::aidl::android::hardware::mytest::MyTestObj* _aidl_return) override;
::ndk::ScopedAStatus addTest(int32_t in_a, int32_t in_b) override;
};
MyTestImpl.cpp增加相关方法实现
namespace aidl::android::hardware::mytest {
::ndk::ScopedAStatus MyTestImpl::test(::aidl::android::hardware::mytest::MyTestObj* _aidl_return) {
*_aidl_return = mObj;
ALOGE(" MyTestImpl::test ok");
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus MyTestImpl::addTest(int32_t in_a, int32_t in_b) {
ALOGE(" MyTestImpl::test ok a %d b %d",in_a,in_b);
return ::ndk::ScopedAStatus::ok();
}
} // namespace aidl::android::hardware::mytest
Android.bp需要把原来V1版本的库变成V2
本文章对应视频手把手教你学framework:
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
私聊作者+v(androidframework007)