当前位置: 代码网 > it编程>编程语言>Java > aidl文件生成Java、C++[android]、C++[ndk]、Rust接口

aidl文件生成Java、C++[android]、C++[ndk]、Rust接口

2024年07月28日 Java 我要评论
aidl文件生成Java、C++[android]、C++[ndk]、Rust接口


前言

在 android 开发中,aidl 文件通常会被自动编译,生成对应语言的接口文件。对于应用层 java 开发者来说,使用 aidl 和 binder 封装的接口可以让他们更加专注于应用逻辑,而不需要过多关注 binder 的调用细节或 aidl 的编译过程。

然而,对于中下层开发者来说,包括 framework 和 native 层开发,可能需要编写 native service,并且有必要理解 binder 的底层机制。

关于编译 aidl 文件的过程,通常是由构建系统自动完成。在编译过程中,aidl 文件会被处理并生成相应的接口文件,以便后续开发使用。生成的接口文件内容会根据具体的需求和语言类型而有所不同,可以根据生成的接口文件来进行后续的开发工作。

准备文件
itestaudio.aidl

package henry.test.audio;

interface itestaudio{
    int requestaudiofocus(int streamtype, int durationhint);
    int abandonaudiofocus();
    void requestaudiofocusforcall(int streamtype, int durationhint);
    void abandonaudiofocusforcall();
    void setmode(int mode);
}

环境
源码环境:out/soong/host/linux-x86/bin/

aidl命令
aidl --lang={java|cpp|ndk} [options] input_aidl_file。

一、java

aidl -o ./java --lang=java henry/test/audio/itestaudio.aidl
tree ./java
./java
└── henry
    └── test
        └── audio
            └── itestaudio.java

3 directories, 1 file

二、c++[android]

cpp:生成的代码是为了在android源码中编译,代码中会调用android源码中的native接口。
例如,引用的头文件:
<binder/ibinder.h>,<binder/iinterface.h>,<binder/status.h>,<android-base/macros.h>

–lang=cpp,参数指定生成android源码下编译的c++接口文件。

-o和-h参数分别指定.cpp文件和.h文件输出的路径。可以指定不同的目录。
c++接口文件比较多,一个aidl文件生成4个文件。第一步编译itestaudio.aidl后,生成文件包括:
3个头文件:
一个interface头文件:itestaudio.h
一个bp头文件:bptestaudio.h
一个bn头文件:bntestaudio.h
一个源码文件:itestaudio.cpp

aidl -h ./cpp_android/ -o ./cpp_android --lang=cpp henry/test/audio/itestaudio.aidl
tree ./cpp_android/
./cpp_android/
└── henry
    └── test
        └── audio
            ├── bntestaudio.h
            ├── bptestaudio.h
            ├── itestaudio.cpp
            └── itestaudio.h

3 directories, 4 files

三、c++[ndk]

ndk:生成的代码是为了使用ndk独立编译,调用的是ndk的接口,
例如,引用的头文件:
<android/binder_interface_utils.h> <android/binder_ibinder.h>
–lang=ndk

aidl -h ./cpp_ndk -o ./cpp_ndk --lang=ndk henry/test/audio/itestaudio.aidl
tree ./cpp_ndk/
./cpp_ndk/
├── aidl
│   └── henry
│       └── test
│           └── audio
│               ├── bntestaudio.h
│               ├── bptestaudio.h
│               └── itestaudio.h
└── henry
    └── test
        └── audio
            └── itestaudio.cpp

7 directories, 4 files

四、rust接口

–lang=rust指定生成rust后端接口文件
较新android 版本才支持

aidl -o ./rust --lang=rust henry/test/audio/itestaudio.aidl
tree ./cpp_ndk/
./rust/
── henry
    └── test
        └── audio
            └── itestaudio.rs
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com