通常项目中aidl文件都是Android Studio自动处理的,如果了解具体的处理过程,有助于了解原理,对定位和解决开发中遇到的问题非常有帮助。其实过程很简单,看一遍都明白。当然,这里是最简单的一个例子,后面会深入展开。
编写一个aidl文件
// ISimple.aidl
package com.abc.pkg;
interface ISimple {
void fun (int arg);
}
说明:
1)文件名(ISimple.aidl)要和接口名(ISimple)保持一致。
2)文件必须保存在和报名(com.abc.pkg)对应的目录下,按照上述例子,文件应该保存在:base_dir/com/abc/pkg/ISimple.aidl
用aidl工具编译
1)需要使用sdk中包含的aidl工具,安装路径:Android/Sdk/build-tools/33.0.1/aidl。
需要把这个文件所在目录添加到path环境变量,或者通过绝对路径进行引用。
2)在base_dir中执行如下命令:
aidl com/abc/pkg/ISimple.aidl
或者
cd com/abc/pkg/
aidl ISimple.aidl
执行结果,会在com/abc/pkg/目录下生成:ISimple.java文件。
此文件中包含Stub和Proxy的模板代码。
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.abc.pkg;
public interface ISimple extends android.os.IInterface
{
/** Default implementation for ISimple. */
public static class Default implements com.abc.pkg.ISimple
{
@Override public void fun(int arg) throws android.os.RemoteException
{
}
@Override
public android.os.IBinder asBinder() {
return null;
}
}
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.abc.pkg.ISimple
{
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.abc.pkg.ISimple interface,
* generating a proxy if needed.
*/
public static com.abc.pkg.ISimple asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.abc.pkg.ISimple))) {
return ((com.abc.pkg.ISimple)iin);
}
return new com.abc.pkg.ISimple.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);
}
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
}
switch (code)
{
case TRANSACTION_fun:
{
int _arg0;
_arg0 = data.readInt();
this.fun(_arg0);
reply.writeNoException();
break;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
return true;
}
private static class Proxy implements com.abc.pkg.ISimple
{
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 void fun(int arg) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(arg);
boolean _status = mRemote.transact(Stub.TRANSACTION_fun, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_fun = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public static final java.lang.String DESCRIPTOR = "com.abc.pkg.ISimple";
public void fun(int arg) throws android.os.RemoteException;
}