使用 AIDL 编写 Android 服务时如何为客户分发自定义 类

How to distribute custom classes for clients when writing Android service with AIDL

我正在编写 Android 服务应用程序。客户端绑定到服务,然后调用远程函数(使用 AIDL 生成的接口)并取回结果。我知道我可以使用任意 class 对象(继承自 Parcelable 并实现 Creator 接口)作为参数或 return 值。客户端和服务必须使用相同的 "interface arguments" classes。问题是如何向客户端发布和分发.aidl 文件以及这样的"shared" class 集?此 classes 必须是 public API 的一部分。我看到的唯一方法是制作一个 "dependency" 库,其中包含客户端和服务器使用的所有 classes,并让客户在他们的项目中使用它。这是一种常见的方法吗?

The only way I see is to make a "dependency" library with all classes used by client and server, and let clients to use it in their projects. Is this a common approach?

没错。只需将所有 类 和您的 AIDL 文件打包到该库中,即可将其提供给您的客户。客户唯一需要另外做的就是将您的 AIDL 文件的副本放入他们的项目中,这样就可以构建服务代理实现。

Google 为他们的应用内结算库执行此操作。 Here is a description 他们推荐的使用方式。