一工具、demo源码
1.aidl 工具在sdk中路径:~/Android/Sdk/build-tools/33.0.0-rc2/2.IIpcServiceListener.aidl文件
// IIpcServiceListener.aidl
package com.android.demo.aidl;
// Declare any non-default types here with import statements
interface IIpcServiceListener {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
// void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
// double aDouble, String aString);
String processServiceRequest(String serviceRequest);
}
3.IIpcService.aidl文件
// IIpcService.aidl
package com.android.demo.aidl;
import android.os.ParcelFileDescriptor;
import com.android.demo.aidl.IIpcServiceListener;
// Declare any non-default types here with import statements
interface IIpcService {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
// void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
// double aDouble, String aString);
void register2Server(String packageName,IIpcServiceListener ipcServiceListener);
void unregister2Server(String packageName);
String processClientRequest(String packageName,String clientRequest,inout ParcelFileDescriptor pfd);
oneway void processClientRequestOneway(String packageName,String clientRequest);
}
4.将创建的连个aidl文件按照包命名放到如下目录中
lilei@lilei-HP-Z4-G4-Workstation:~/code/aidl$ tree
.
├── com
│ └── android
│ └── demo
│ └── aidl
│ ├── IIpcService.aidl
│ └── IIpcServiceListener.aidl
二、生成java aidl接口文件
命令说明:
1.--lang= 指定生成接口类型。如果不指定,默认生成Java文件
2.-o 指定输出目录
3.-I DIR, --include=DIR import搜索路径,指定依赖的aidl文件所在目录
执行命令1
~/Android/Sdk/build-tools/33.0.0-rc2$ ./aidl -o /home/lilei/code/aidl/out/ --lang=java /home/lilei/code/aidl/com/android/demo/aidl/IIpcServiceListener.aidl
查看生成的java文件1
lilei@lilei-HP-Z4-G4-Workstation:~/Android/Sdk/build-tools/33.0.0-rc2$ ls /home/lilei/code/aidl/out/com/android/demo/aidl/IIpcServiceListener.java
/home/lilei/code/aidl/out/com/android/demo/aidl/IIpcServiceListener.java
执行命令2 ,这里需要-I 是因为需要导入其他关联的aidl
~/Android/Sdk/build-tools/33.0.0-rc2$ ./aidl -I /home/lilei/code/aidl/ -o /home/lilei/code/aidl/out/ --lang=java /home/lilei/code/aidl/com/android/demo/aidl/IIpcService.aidl
查看生成的java文件2
lilei@lilei-HP-Z4-G4-Workstation:~/Android/Sdk/build-tools/33.0.0-rc2$ ls /home/lilei/code/aidl/out/com/android/demo/aidl/IIpcService.java
/home/lilei/code/aidl/out/com/android/demo/aidl/IIpcService.java
二、生成c++ aidl接口文件
-o和-h参数分别指定.cpp文件和.h文件输出的路径。可以指定不同的目录。这里 --lang=cpp 。PS:如果设置参数ndk,生成的文件可以不依赖Android源码
C++接口文件比较多,一个aidl文件生成4个文件。第一步编译IMyServiceCallback.aidl后,生成文件包括:
IIpcServiceListener.h、BpIpcServiceListener.h、BnIpcServiceListener.h、 IIpcServiceListener.cpp
执行命令1
~/Android/Sdk/build-tools/33.0.0-rc2$ ./aidl -h /home/lilei/code/aidl/out/cpp_android/ -o /home/lilei/code/aidl/out/cpp_android/ --lang=cpp /home/lilei/code/aidl/com/android/demo/aidl/IIpcServiceListener.aidl
查看生成的头文件和cpp文件 1
lilei@lilei-HP-Z4-G4-Workstation:~/code/aidl/out/cpp_android$ tree
.
└── com
└── android
└── demo
└── aidl
├── BnIpcServiceListener.h
├── BpIpcServiceListener.h
├── IIpcServiceListener.cpp
└── IIpcServiceListener.h
执行命令2
~/Android/Sdk/build-tools/33.0.0-rc2$ ./aidl -I /home/lilei/code/aidl/ -h /home/lilei/code/aidl/out/cpp_android/ -o /home/lilei/code/aidl/out/cpp_android/ --lang=cpp /home/lilei/code/aidl/com/android/demo/aidl/IIpcService.aidl
查看生成的头文件和cpp文件 2
lilei@lilei-HP-Z4-G4-Workstation:~/code/aidl/out/cpp_android$ tree
.
└── com
└── android
└── demo
└── aidl
├── BnIpcService.h
├── BpIpcService.h
├── IIpcService.cpp
├── IIpcService.h
完整生成的JAVA和cpp文件见 附件 :https://download.csdn.net/download/kk3087961/88813343