一、背景
编写binder接口工作量大,google已提供aidl工具进行自动生成
二、aidl使用说明
找到aidl.exe可执行文件,在Android studio 设置中Android SDK找到 Android SDK Location存放路径build-tools/版本号/文件夹下(linux系统上为aidl)
windows上演示示例
//IStudent.aidl
interface IStudent {
int getStudentId(String name);
}
工具生成后效果:
└─aidl
│ IStudent.aidl
│ IStudent.java
│
└─cpp
BnStudent.h
BpStudent.h
IStudent.cpp
IStudent.h
自动生成Java代码
aidl.exe --lang=java -IE:\project\demo\aidl E:\project\demo\aidl\IStudent.aidl -o E:\project\demo\aidl
#–lang={java|cpp|ndk} 指定生成接口类型。如果不指定,默认生成Java文件。
#-o 指定输出目录
#-I DIR, --include=DIR import搜索路径,指定依赖的aidl文件所在目录
//IStudent.java
/*
* This file is auto-generated. DO NOT MODIFY.
* Using: aidl.exe --lang=java -IE:\\project\\demo\\aidl E:\\project\\demo\\aidl\\IStudent.aidl -o E:\\project\\demo\\aidl
*/
public interface IStudent extends android.os.IInterface
{
/** Default implementation for IStudent. */
public static class Default implements IStudent
{
@Override public int getStudentId(java.lang.String name) throws android.os.RemoteException
{
return 0;
}
@Override
public android.os.IBinder asBinder() {
return null;
}
}
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements IStudent
{
/** Construct the stub at attach it to the interface. */
@SuppressWarnings("this-escape")
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an IStudent interface,
* generating a proxy if needed.
*/
public static IStudent asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof IStudent))) {
return ((IStudent)iin);
}
return new IStudent.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
java.lang.String descriptor = DESCRIPTOR;
if (code >= android.os.IBinder.FIRST_CALL_TRANSACTION && code <= android.os.IBinder.LAST_CALL_TRANSACTION) {
data.enforceInterface(descriptor);
}
if (code == INTERFACE_TRANSACTION) {
reply.writeString(descriptor);
return true;
}
switch (code)
{
case TRANSACTION_getStudentId:
{
java.lang.String _arg0;
_arg0 = data.readString();
int _result = this.getStudentId(_arg0);
reply.writeNoException();
reply.writeInt(_result);
break;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
return true;
}
private static class Proxy implements IStudent
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public int getStudentId(java.lang.String name) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(name);
boolean _status = mRemote.transact(Stub.TRANSACTION_getStudentId, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getStudentId = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
/** @hide */
public static final java.lang.String DESCRIPTOR = "IStudent";
public int getStudentId(java.lang.String name) throws android.os.RemoteException;
}
自动生成C++代码
aidl.exe --lang=cpp -IE:\project\demo\aidl E:\project\demo\aidl\IStudent.aidl -h E:\project\demo\aidl\cpp -o E:\project\demo\aidl\cpp
//BnStudent.h
/*
* This file is auto-generated. DO NOT MODIFY.
* Using: aidl.exe --lang=cpp -IE:\\project\\demo\\aidl E:\\project\\demo\\aidl\\IStudent.aidl -h E:\\project\\demo\\aidl\\cpp -o E:\\project\\demo\\aidl\\cpp
*/
#pragma once
#include <binder/IInterface.h>
#include <IStudent.h>
#include <BnStudent.h>
#include <binder/Delegate.h>
class BnStudent : public ::android::BnInterface<IStudent> {
public:
static constexpr uint32_t TRANSACTION_getStudentId = ::android::IBinder::FIRST_CALL_TRANSACTION + 0;
explicit BnStudent();
::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) override;
}; // class BnStudent
class IStudentDelegator : public BnStudent {
public:
explicit IStudentDelegator(const ::android::sp<IStudent> &impl) : _aidl_delegate(impl) {}
::android::sp<IStudent> getImpl() { return _aidl_delegate; }
::android::binder::Status getStudentId(const ::android::String16& name, int32_t* _aidl_return) override {
return _aidl_delegate->getStudentId(name, _aidl_return);
}
private:
::android::sp<IStudent> _aidl_delegate;
}; // class IStudentDelegator
//BpStudent.h
/*
* This file is auto-generated. DO NOT MODIFY.
* Using: aidl.exe --lang=cpp -IE:\\project\\demo\\aidl E:\\project\\demo\\aidl\\IStudent.aidl -h E:\\project\\demo\\aidl\\cpp -o E:\\project\\demo\\aidl\\cpp
*/
#pragma once
#include <binder/IBinder.h>
#include <binder/IInterface.h>
#include <utils/Errors.h>
#include <IStudent.h>
class BpStudent : public ::android::BpInterface<IStudent> {
public:
explicit BpStudent(const ::android::sp<::android::IBinder>& _aidl_impl);
virtual ~BpStudent() = default;
::android::binder::Status getStudentId(const ::android::String16& name, int32_t* _aidl_return) override;
}; // class BpStudent
//IStudent.cpp
/*
* This file is auto-generated. DO NOT MODIFY.
* Using: aidl.exe --lang=cpp -IE:\\project\\demo\\aidl E:\\project\\demo\\aidl\\IStudent.aidl -h E:\\project\\demo\\aidl\\cpp -o E:\\project\\demo\\aidl\\cpp
*/
#include <IStudent.h>
#include <BpStudent.h>
DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(Student, "IStudent")
#include <BpStudent.h>
#include <BnStudent.h>
#include <binder/Parcel.h>
BpStudent::BpStudent(const ::android::sp<::android::IBinder>& _aidl_impl)
: BpInterface<IStudent>(_aidl_impl){
}
::android::binder::Status BpStudent::getStudentId(const ::android::String16& name, int32_t* _aidl_return) {
::android::Parcel _aidl_data;
_aidl_data.markForBinder(remoteStrong());
::android::Parcel _aidl_reply;
::android::status_t _aidl_ret_status = ::android::OK;
::android::binder::Status _aidl_status;
_aidl_ret_status = _aidl_data.writeInterfaceToken(getInterfaceDescriptor());
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = _aidl_data.writeString16(name);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = remote()->transact(BnStudent::TRANSACTION_getStudentId, _aidl_data, &_aidl_reply, 0);
if (_aidl_ret_status == ::android::UNKNOWN_TRANSACTION && IStudent::getDefaultImpl()) [[unlikely]] {
return IStudent::getDefaultImpl()->getStudentId(name, _aidl_return);
}
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_ret_status = _aidl_status.readFromParcel(_aidl_reply);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
if (!_aidl_status.isOk()) {
return _aidl_status;
}
_aidl_ret_status = _aidl_reply.readInt32(_aidl_return);
if (((_aidl_ret_status) != (::android::OK))) {
goto _aidl_error;
}
_aidl_error:
_aidl_status.setFromStatusT(_aidl_ret_status);
return _aidl_status;
}
#include <BnStudent.h>
#include <binder/Parcel.h>
#include <binder/Stability.h>
BnStudent::BnStudent()
{
::android::internal::Stability::markCompilationUnit(this);
}
::android::status_t BnStudent::onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags) {
::android::status_t _aidl_ret_status = ::android::OK;
switch (_aidl_code) {
case BnStudent::TRANSACTION_getStudentId:
{
::android::String16 in_name;
int32_t _aidl_return;
if (!(_aidl_data.checkInterface(this))) {
_aidl_ret_status = ::android::BAD_TYPE;
break;
}
_aidl_ret_status = _aidl_data.readString16(&in_name);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
::android::binder::Status _aidl_status(getStudentId(in_name, &_aidl_return));
_aidl_ret_status = _aidl_status.writeToParcel(_aidl_reply);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
if (!_aidl_status.isOk()) {
break;
}
_aidl_ret_status = _aidl_reply->writeInt32(_aidl_return);
if (((_aidl_ret_status) != (::android::OK))) {
break;
}
}
break;
default:
{
_aidl_ret_status = ::android::BBinder::onTransact(_aidl_code, _aidl_data, _aidl_reply, _aidl_flags);
}
break;
}
if (_aidl_ret_status == ::android::UNEXPECTED_NULL) {
_aidl_ret_status = ::android::binder::Status::fromExceptionCode(::android::binder::Status::EX_NULL_POINTER).writeOverParcel(_aidl_reply);
}
return _aidl_ret_status;
}