这就是我想要达到的目标
int main(int argc, char *argv[])
)。我使用 aidl 二进制文件从 IMyService.aidl 生成一些 .cpp 和 .h,但是当尝试使用我的 C++ 程序编译它们时,这些生成的 C++ 文件引用了一些其他头文件,例如“
我做了一些研究并找到了这篇文章,但读完后我仍然找不到答案。
谢谢!
让我们看一下
logcat
系统服务,以了解如何从 C++ 独立程序绑定到 Java 服务。
使用 https://cs.android.com/android 搜索 AOSP 中的实际代码。
frameworks/base/core/java/android/os/logcat/ILogcatManagerService.aidl
interface ILogcatManagerService {
void startThread(in int uid, in int gid, in int pid, in int fd);
void finishThread(in int uid, in int gid, in int pid, in int fd);
}
frameworks/base/services/core/java/com/android/server/logcat/LogcatManagerService.java
import com.android.server.SystemService;
import android.os.logcat.ILogcatManagerService;
public final class LogcatManagerService extends SystemService {
// Service implementation
private final class BinderService extends ILogcatManagerService.Stub {
@Override
public void startThread(int uid, int gid, int pid, int fd) { ... }
@Override
public void finishThread(int uid, int gid, int pid, int fd) { ... }
}
public LogcatManagerService(Context context, ...) {
super(context);
mBinderService = new BinderService();
}
@Override
public void onStart() {
// Publish the binder service
publishBinderService("logcat", mBinderService);
}
}
system/logging/logd/LogReaderList.cpp
#include <android/os/logcat/ILogcatManagerService.h>
#include <binder/IServiceManager.h>
using android::defaultServiceManager;
using android::sp;
sp<ILogcatManagerService> logcat_service = android::interface_cast<ILogcatManagerService>(
defaultServiceManager()->waitForService(String16("logcat"))
);
service->startThread(...);
service->finishThread(...);
frameworks/base/core/java/Android.bp
aidl_library {
name: "ILogcatManagerService_aidl",
srcs: ["android/os/logcat/ILogcatManagerService.aidl"],
}
frameworks/base/services/Android.bp
// All system services are packages into the services.jar
java_library {
name: "services",
srcs: [":services-main-sources"],
}
filegroup {
name: "services-main-sources",
srcs: [
"java/**/*.java",
"java/**/package.html",
],
path: "java",
visibility: ["//visibility:private"],
}
system/logging/logd/Android.bp
cc_binary {
name: "logd",
// logd standalone program is executed at system startup via init.rc
init_rc: ["logd.rc"],
srcs: [
"main.cpp",
...
],
static_libs: [
"liblogd",
...
],
aidl: {
libs: [
"ILogcatManagerService_aidl",
]
}
}
cc_library_static {
name: "liblogd",
srcs: [
"LogReaderList.cpp",
...
],
aidl: {
libs: [
"ILogcatManagerService_aidl",
],
},
}
您可能还必须为您的服务定义 SELiux 策略。