手动编译AIDL

通常项目中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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抓饼先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值