Android-HAL (四) AIDL

新增aidl接口

vendor/godv/hardware/interface/mytest/aidl/android/hardware/mytest/IMyTest.aidl

复制代码
package android.hardware.mytest;
import android.hardware.mytest.MyTestObj;
@VintfStability
interface IMyTest {
    MyTestObj test();
}

vendor/godv/hardware/interface/mytest/aidl/android/hardware/mytest/MyTest.aidl

复制代码
package android.hardware.mytest;
@VintfStability
parcelable MyTestObj {
    String key;
    int value;
}

vendor/godv/hardware/interface/mytest/aidl/Android.bp

backend 后端包含4种,c++ | java | ndk | rust 本文使用ndk 和 java,cpp置为false

复制代码
aidl_interface {
    name: "android.hardware.mytest",
    vendor_available: true,
    srcs: ["android/hardware/mytest/*.aidl"],
    stability: "vintf",
    backend: {
        cpp: {
            enabled: false,
        },
        java: {
            sdk_version: "module_current",
        },
        rust: {
            enabled: false,
        },
    },
}

mmm vendor/godv/hardware/interface/mytest/

报错

API dump for the current version of AIDL interface android.hardware.mytest does not exist.

Run the command "m android.hardware.mytest-update-api" or add "unstable: true" to the build rule for the interface if it does not need to be versioned

m android.hardware.mytest-update-api


编译中间文件

godv@godv-OptiPlex-7070:~/ass/godv/code/lineage/out/soong/.intermediates/vendor/godv/hardware/interface/mytest/aidl$ ls -la

total 36

drwxrwxr-x 9 godv godv 4096 12月 20 20:51 .

drwxrwxr-x 3 godv godv 4096 12月 20 20:34 ..

drwxrwxr-x 3 godv godv 4096 12月 20 20:50 android.hardware.mytest_interface

drwxrwxr-x 3 godv godv 4096 12月 20 20:51 android.hardware.mytest-V1-java

drwxrwxr-x 3 godv godv 4096 12月 20 20:34 android.hardware.mytest-V1-java-source

drwxrwxr-x 14 godv godv 4096 12月 20 20:51 android.hardware.mytest-V1-ndk

drwxrwxr-x 3 godv godv 4096 12月 20 20:34 android.hardware.mytest-V1-ndk-source

drwxrwxr-x 18 godv godv 4096 12月 20 20:51 android.hardware.mytest-V1-rust

drwxrwxr-x 3 godv godv 4096 12月 20 20:51 android.hardware.mytest-V1-rust-source

android.hardware.mytest-V1-ndk 代表aidl相关的库

android.hardware.mytest-V1-ndk-source 代表aidl相关的源文件 bn bp



新增服务

vendor/godv/hardware/interface/mytest/aidl/default/Android.bp

复制代码
cc_binary {
    name: "android.hardware.mytest-service",
    init_rc: ["android.hardware.mytest.rc"],
    vintf_fragments: ["mytest-default.xml"],
    vendor: true,
    srcs: [
        "MyTestImpl.cpp",
        "main.cpp",
    ],
    relative_install_path: "hw",
    shared_libs: [
        "libbase",
        "libbinder_ndk",
        "liblog",
        "libutils",
        "libcutils",
        "android.hardware.mytest-V1-ndk",
    ],
}

vendor/godv/hardware/interface/mytest/aidl/default/android.hardware.mytest.rc

复制代码
service vendor.mytest-default /system/bin/hw/android.hardware.mytest-service
    class hal
    user root
    group root

vendor/godv/hardware/interface/mytest/aidl/default/main.cpp

cpp 复制代码
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include "MyTestImpl.h"

using aidl::android::hardware::mytest::MyTestImpl;
using aidl::android::hardware::mytest::IMyTest;

int main(int, char* argv[]) {
    android::base::InitLogging(argv, android::base::KernelLogger);
    ABinderProcess_setThreadPoolMaxThreadCount(0);
    std::shared_ptr<IMyTest> service = ndk::SharedRefBase::make<MyTestImpl>();

    const std::string instance = std::string(IMyTest::descriptor) + "/default";
    auto status = AServiceManager_addService(service->asBinder().get(), instance.c_str());
    CHECK_EQ(status, STATUS_OK) << "Failed to add service " << instance << " " << status;
    LOG(INFO) << "IMyTest AIDL service running...";

    ABinderProcess_joinThreadPool();
    return EXIT_FAILURE;  // should not reach
}

vendor/godv/hardware/interface/mytest/aidl/default/mytest-default.xml

XML 复制代码
<manifest version="1.0" type="device">
    <hal format="aidl">
        <name>android.hardware.mytest</name>
        <version>1</version>
        <fqname>IMyTest/default</fqname>
    </hal>
</manifest>

vendor/godv/hardware/interface/mytest/aidl/default/MyTestImpl.cpp

cpp 复制代码
#define LOG_TAG "MyTestImpl"

#include "MyTestImpl.h"
#include <android-base/logging.h>

using ndk::ScopedAStatus;

namespace aidl {
namespace android {
namespace hardware {
namespace mytest {

ScopedAStatus MyTestImpl::test(MyTestObj* _aidl_return) {
    *_aidl_return = mTestObj;
    LOG(INFO) << "Computing shared secret";
    return ScopedAStatus::ok();
}

}  // namespace MyTestImpl
}  // namespace hardware
}  // namespace android
}  // namespace aidl

vendor/godv/hardware/interface/mytest/aidl/default/MyTestImpl.h

cpp 复制代码
#pragma once

#include "aidl/android/hardware/mytest/BnMyTest.h"

namespace aidl {
namespace android {
namespace hardware {
namespace mytest {
class MyTestImpl : public BnMyTest {
    MyTestObj mTestObj = {"test", 66};
    ::ndk::ScopedAStatus test(::aidl::android::hardware::mytest::MyTestObj* _aidl_return) override;
};

}  // namespace mytest
}  // namespace hardware
}  // namespace android
}  // namespace aidl
相关推荐
用户69371750013843 小时前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
用户69371750013843 小时前
Room 3.0:这次不是升级,是重来
android·前端·google
alexhilton6 小时前
Compose中的ContentScale:终极可视化指南
android·kotlin·android jetpack
Digitally8 小时前
2026 年 8 款安卓数据擦除软件和应用对比
android
杨忆8 小时前
android 11以上 截图工具类
android
粤M温同学9 小时前
Android Studio 中安装 CodeBuddy AI助手
android·ide·android studio
阿拉斯攀登9 小时前
【RK3576 安卓 JNI/NDK 系列 08】RK3576 实战(二):JNI 调用 I2C 驱动读取传感器数据
android·安卓ndk入门·jni方法签名·java调用c++·rk3576底层开发·rk3576 i2c开发
赶路人儿11 小时前
常见的mcp配置
android·adb
符哥200811 小时前
充电桩 WiFi 局域网配网(Android/Kotlin)流程、指令及实例说明文档
android·开发语言·kotlin
没有了遇见12 小时前
Android 项目架构之<用户信息模块>
android