開始使用
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本章節旨在提供工程師 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 程式碼完成功能。
已加註 API 的每個傳回和參數類型都必須由
但完整支援清單、組合、陣列、
原始、任何可包裝類型,以及任何可序列化的型別
ListenableFuture
、Optional
和 proto。您也可以將
支援 SDK 未原生支援的類型。這個極端例子
會順暢支援 ListenableFuture<List<Map<CustomProto,
CustomParcelableType[]>>>
。
測試
SDK 旨在簡化單元測試,針對每個產生的設定檔類別
有一個對應的 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.
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間: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."]]