项目目录结构:
-- Setting
------- src
----------- main
--------------- java
--------------- aidl
-------------------- IBluetoothManager.aidl
-------------------- IBluetoothCallback.aidl
------- Android.bp
IBluetoothManager.aidl代码如下:
// IBluetooth1Manager.aidl package com.example.setting; // Declare any non-default types here with import statements import com.example.setting.IBluetoothCallback; interface IBluetoothManager { void connectUnPariDevice(String addr); void unPair(); void registerCallback(IBluetoothCallback callback); }
IBluetoothCallback.aidl代码如下:
// IBluetoothCallback.aidl package com.example.setting; // Declare any non-default types here with import statements interface IBluetoothCallback { void onConnectionStateChanged(int state); }
Android.bp代码如下:
android_app { name: "Setting", manifest: "src/main/AndroidManifest.xml", srcs: [ "src/main/java/**/*.java", "src/main/aidl/**/*.aidl", ], //省略无关代码... }
编译的时候出现如下错误:
ERROR: com.example.IBluetoothCallback: couldn't find import for class com.example.IBluetoothCallback
导致该错误的原因是没有指定系统应该在哪里查找 AIDL 文件
只需在Android.bp文件中添加如下属性就能解决这个错误:
aidl: {
local_include_dirs: ["src/main/aidl"],
},
修改后的Android.bp文件如下:
android_app {
name: "Setting",
manifest: "app/src/main/AndroidManifest.xml",
srcs: [
"app/src/main/java/**/*.java",
"app/src/main/aidl/com/ais/setting/IBluetoothCallback.aidl",
"app/src/main/aidl/com/ais/setting/IBluetooth1Manager.aidl",
],
aidl: {
local_include_dirs: ["app/src/main/aidl"],
},
//省略无关代码...
}