Android 應用程式可透過 AppFunctions 分享特定功能,供系統和各種 AI 代理程式和助理探索及叫用。定義這些函式後,應用程式就能向 Android OS 提供服務、資料和動作,讓使用者透過 AI 代理程式和系統層級的互動完成工作。
AppFunctions 相當於 Model Context Protocol (MCP) 中的工具,但適用於行動裝置。MCP 傳統上會標準化代理連線至伺服器端工具的方式,而 AppFunctions 則為 Android 應用程式提供相同機制。您可以將應用程式功能公開為可編排的「工具」,授權應用程式 (呼叫端) 即可探索及執行這些工具,滿足使用者意圖。呼叫者必須具備 EXECUTE_APP_FUNCTIONS 權限,才能探索及執行 AppFunctions,且可包含代理程式、應用程式和 Gemini 等 AI 助理。
AppFunctions 適用於搭載 Android 16 以上版本的裝置。
應用實例
AppFunctions 提供強大的機制,可自動執行工作並簡化使用者互動。公開應用程式的功能後,使用者就能以自然語言完成複雜目標,通常不需要透過 UI 手動逐步導覽。
下列情境說明如何使用 AppFunctions,在各種應用程式類別中提供體驗:
- 工作管理與生產力
- 使用者要求:「今天下午 5 點提醒我到公司取貨」。
- AppFunction 動作:呼叫端會找出相關工作管理應用程式,並呼叫函式來建立工作,根據使用者的提示自動填入標題、時間和地點欄位。
- 媒體與娛樂
- 使用者要求:「建立新的播放清單,收錄今年最熱門的爵士專輯」。
- AppFunction 動作:呼叫端會在音樂應用程式中執行播放清單建立函式,並傳遞「2026 年熱門爵士專輯」等內容做為查詢,立即生成並啟動內容。
- 跨應用程式工作流程
- 使用者要求:「Find the noodle recipe from Lisa's email and add the ingredients to my shopping list」(找出 Lisa 電子郵件中的麵條食譜,然後將食材加入我的購物清單)。
- AppFunction 動作:這項要求會使用多個應用程式的函式。 首先,來電者會使用電子郵件應用程式的搜尋功能擷取內容。接著,系統會擷取相關食材,並叫用購物清單應用程式的函式,在使用者清單中填入食材。
- 日曆和行事曆
- 使用者要求:「在日曆中新增下週一下午 6 點的媽媽生日派對」。
- AppFunction 動作:獲得核准的代理程式應用程式會叫用日曆應用程式的「建立活動」函式,剖析「下週一」和「下午 6 點」等相關情境,建立項目,使用者不必手動開啟日曆。
AppFunctions 的運作方式
AppFunctions 是 Android 16 平台功能和隨附的 Jetpack 程式庫,可讓應用程式公開特定功能,供呼叫端 (例如代理應用程式) 存取及在裝置上執行。
下圖說明應用程式如何將 AppFunction 分享給代理程式,以及後續的執行流程。服務專員處理使用者要求時,可能會同時考量伺服器端遠端 MCP 工具和本機 AppFunctions。使用本機 AppFunctions 的詳細流程如下:
- AppFunction 宣告:Android 應用程式會建構為公開其 AppFunction,例如「建立記事」或「傳送訊息」。
- 產生結構定義:AppFunctions Jetpack 程式庫會產生 XML 結構定義檔案,列出應用程式中所有已宣告的 AppFunctions。Android OS 會使用這個檔案為可用的 AppFunctions 建立索引。
- 擷取中繼資料:代理程式可以查詢 AppFunction 中繼資料,藉此擷取中繼資料。
- 選取及執行 AppFunction:代理程式會根據使用者提示,選取及執行適當的 AppFunction,並提供適當的參數。
AppFunctions Jetpack 程式庫可簡化公開應用程式功能的程序。開發人員可使用註解處理器,為要公開的函式加上註解。呼叫端隨後可以使用 AppFunctionManager 探索及叫用這些已編列索引的函式。
應用程式不需驗證是否支援 AppFunction 功能,Jetpack 程式庫會自動處理這項作業。舉例來說,AppFunctionManager 可以驗證功能是否受到支援。
以下是筆記應用程式的 AppFunctions 範例,可建立、編輯及列出記事。
class NoteFunctions(
private val noteRepository: NoteRepository
) {
/**
* A note.
*
* @param id The note's ID.
* @param title The note's title.
* @param content The note's content.
*/
@AppFunctionSerializable(isDescribedByKDoc = true)
data class Note(val id: Int, val title: String, val content: String)
/**
* Lists all available notes.
*
* @param appFunctionContext The context in which the AppFunction is executed.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun listNotes(appFunctionContext: AppFunctionContext): List<Note>? {
return if (noteRepository.appNotes.isEmpty()) null else viewModel.appNotes
}
/**
* Adds a new note to the app.
*
* @param appFunctionContext The context in which the AppFunction is executed.
* @param title The title of the note.
* @param content The note's content.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun createNote(
appFunctionContext: AppFunctionContext,
title: String,
content: String
): Note {
return noteRepository.createNote(title, content)
}
/**
* Edits a single note.
*
* @param appFunctionContext The context in which the AppFunction is executed.
* @param noteId The target note's ID.
* @param title The new title if it should be updated.
* @param content The new content if it should be updated.
*/
@AppFunction(isDescribedByKDoc = true)
suspend fun editNote(
appFunctionContext: AppFunctionContext,
noteId: String,
title: String?,
content: String,
): Note? {
return noteRepository.updateNote(noteId, title, content)
}
}