0%

aidl工具分析(二)

前言

这是Android打包过程分析系列文章的第二篇

Demo

aidl工具负责把aidl语言翻译成java语言

1
2
3
4
5
6
7
// IBookManager.aidl
package com.nancyyihao.aidlserver;
// Declare any non-default types here with import statements
interface IBookManager {
List<String> getBookList();
void addBook(in String book);
}

执行如下命令

1
aidl com/nancyyihao/aidlserver/IBookManager.aidl

上面的IBookManager.aidl经过aidl工具转换之后就变成下面的样子了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: com/nancyyihao/aidlserver/IBookManager.aidl
*/
package com.nancyyihao.aidlserver;
// Declare any non-default types here with import statements
public interface IBookManager extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.nancyyihao.aidlserver.IBookManager
{
private static final java.lang.String DESCRIPTOR = "com.nancyyihao.aidlserver.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.nancyyihao.aidlserver.IBookManager interface,
* generating a proxy if needed.
*/
public static com.nancyyihao.aidlserver.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.nancyyihao.aidlserver.IBookManager))) {
return ((com.nancyyihao.aidlserver.IBookManager)iin);
}
return new com.nancyyihao.aidlserver.IBookManager.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
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getBookList:
{
data.enforceInterface(DESCRIPTOR);
java.util.List<java.lang.String> _result = this.getBookList();
reply.writeNoException();
reply.writeStringList(_result);
return true;
}
case TRANSACTION_addBook:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
this.addBook(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.nancyyihao.aidlserver.IBookManager
{
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 java.util.List<java.lang.String> getBookList() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<java.lang.String> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createStringArrayList();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void addBook(java.lang.String book) 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.writeString(book);
mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public java.util.List<java.lang.String> getBookList() throws android.os.RemoteException;
public void addBook(java.lang.String book) throws android.os.RemoteException;
}

注意:aidl文件等级已经要和aidl文件中定义的package保持一致,不然会报错

错误

图1

refusing to generate code from aidl file defining parcelable

参考