使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分旨在让工程师在 5 分钟内大致了解跨资料调用如何与 SDK 搭配使用以及如何进行测试。请勿尝试
尚不能用作参考或指南,
只是简单介绍一下
自定义个人资料感知类和方法
通过关联的应用 SDK,您可以为自己的类和方法添加注解,
进行跨资料跟踪这将生成允许您执行
注释的方法。
例如,请考虑添加 SDK 注释的以下类:
public class CalendarDatabase {
@CrossProfile // SDK annotation
public void deleteEvent(Event event, Account account) {
// complex logic with database calls
}
}
这会生成一个带有 Profile 前缀的类,让您可以在
所选个人资料例如:java
profileCalendarDatabase.work().deleteEvent(event, account);
更复杂的示例
在更现实的情况下,您的类和方法会更复杂。例如:
您的现有 API 可以使用 ListenableFuture
返回值类型,您可能需要
来合并两个配置文件的结果。请参考下面的示例:
public class CalendarDatabase {
@CrossProfile // SDK annotation
public ListenableFuture<Collection<Event>> getEvents() {
// complex logic with database calls
}
}
// Merge results from both profiles into a set
profileCalendarDatabase.both()
.getEvents()
.transform((Map<Profile, Collection<Event>> events) -> {
return events.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}, directExecutor());
这些生成的类和方法会按预期运行,并具有完整的类型安全性和 IDE 代码补全功能。
SDK 必须支持带注解的 API 的每个返回值和参数类型,但除了 ListenableFuture
、Optional
和 proto 之外,它还完全支持列表、集合、数组、基元、任何 Parcelable 类型和任何可序列化类型的嵌套和泛型。你还可以将
支持 SDK 本身不支持的类型。极端情况下,它可以无缝支持 ListenableFuture<List<Map<CustomProto,
CustomParcelableType[]>>>
。
测试
该 SDK 旨在简化单元测试。对于生成的每个 Profile 类
您可以提供对应的 FakeProfile
类,
个人实例。例如:
// Create an instance of the SDK-generated fake connector class. This
// class lets you control the availability of the profiles, which
// profile you are now running on.
private final FakeCrossProfileConnector connector =
new FakeCrossProfileConnector();
// Create an instance of your real/fake/mock class for both profiles.
private final CalendarDatabase personalCalendarDatabase =
new FakeCalendarDatabase();
private final CalendarDatabase workCalendarDatabase =
new FakeCalendarDatabase();
// Create an instance of the SDK-generated fake profile-aware class.
private final FakeProfileCalendarDatabase profileCalendarDatabase =
FakeProfileCalendarDatabase.builder()
.personal(personalCalendarDatabase)
.work(workCalendarDatabase)
.connector(connector)
.build();
// Pass profileCalendarDatabase into your classes under test, or set
// Dagger up to inject the fake automatically.
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThis document provides a high-level introduction to cross-profile calls with the SDK for engineers, focusing on concepts rather than detailed implementation.\u003c/p\u003e\n"],["\u003cp\u003eThe SDK enables annotation of custom classes and methods for cross-profile functionality, allowing execution on any profile through generated classes.\u003c/p\u003e\n"],["\u003cp\u003eGenerated classes support a wide range of data types, including complex nesting and generics, and offer flexibility for adding custom type support.\u003c/p\u003e\n"],["\u003cp\u003eTesting is simplified with fake profile classes provided by the SDK, allowing control over profile availability and behavior during unit tests.\u003c/p\u003e\n"]]],[],null,["# Getting started\n\nThis section is designed to give engineers a 5-minute overview of how\ncross-profile calls work with the SDK and how they can be tested. Don't try to\nbuild anything yet - this isn't designed to be used as a reference or a guide,\nbut just as an introduction.\n\nCustom profile-aware classes and methods\n----------------------------------------\n\nThe connected apps SDK lets you to annotate your own classes and methods as\ncross-profile. This generates classes and methods that allow you to execute the\nannotated method on any profile.\n\nFor example, consider the following class, with SDK annotation added: \n\n public class CalendarDatabase {\n\n @CrossProfile // SDK annotation\n public void deleteEvent(Event event, Account account) {\n // complex logic with database calls\n }\n }\n\nThis generates a class prefixed with Profile, allowing you to call this API on\nthe profile of your choice. For example: `java\nprofileCalendarDatabase.work().deleteEvent(event, account);`\n\n### More complex examples\n\nMore realistically, your classes and methods will be more complex. For example,\nyour existing API could use `ListenableFuture` return types and you might need\nto combine results from both profiles. Consider this example: \n\n public class CalendarDatabase {\n\n @CrossProfile // SDK annotation\n public ListenableFuture\u003cCollection\u003cEvent\u003e\u003e getEvents() {\n // complex logic with database calls\n }\n }\n\n // Merge results from both profiles into a set\n profileCalendarDatabase.both()\n .getEvents()\n .transform((Map\u003cProfile, Collection\u003cEvent\u003e\u003e events) -\u003e {\n return events.values()\n .stream()\n .flatMap(Collection::stream)\n .collect(Collectors.toSet());\n }, directExecutor());\n\nThese generated classes and methods work as expected with full type safety and\nIDE code completion.\n\nEach return and parameter type of your annotated APIs must be supported by the\nSDK, but it fully supports nesting and generics of lists, sets, arrays,\nprimitives, any parcelable type, and any serializable type, in addition to\n`ListenableFuture`, `Optional`, and protos. It's also possible for you to add\nsupport for types not natively supported by the SDK. As an extreme example, it\nwould seamlessly support `ListenableFuture\u003cList\u003cMap\u003cCustomProto,\nCustomParcelableType[]\u003e\u003e\u003e`.\n\n### Testing\n\nThe SDK is designed to simplify unit testing. For each generated Profile class,\nthere is a corresponding `FakeProfile` class that you can provide work and\npersonal instances to. For example: \n\n // Create an instance of the SDK-generated fake connector class. This\n // class lets you control the availability of the profiles, which\n // profile you are now running on.\n private final FakeCrossProfileConnector connector =\n new FakeCrossProfileConnector();\n\n // Create an instance of your real/fake/mock class for both profiles.\n private final CalendarDatabase personalCalendarDatabase =\n new FakeCalendarDatabase();\n private final CalendarDatabase workCalendarDatabase =\n new FakeCalendarDatabase();\n\n // Create an instance of the SDK-generated fake profile-aware class.\n private final FakeProfileCalendarDatabase profileCalendarDatabase =\n FakeProfileCalendarDatabase.builder()\n .personal(personalCalendarDatabase)\n .work(workCalendarDatabase)\n .connector(connector)\n .build();\n\n // Pass profileCalendarDatabase into your classes under test, or set\n // Dagger up to inject the fake automatically."]]