AppFunctions 是 Android 平台 API,並隨附 Jetpack 程式庫,可簡化 Android MCP 整合。這項功能可讓應用程式像裝置端 MCP 伺服器一樣運作,提供可做為工具的功能,供主動功能以及代理和助理 (例如 Google Gemini) 使用。如要使用 Gemini 測試及發布 AppFunctions,請申請加入連結應用程式平台開發人員預覽版。加入後,請按照「建立及管理連結的應用程式」中的步驟操作。
定義這些 AppFunction 後,應用程式就能向 Android 作業系統內建的註冊資料庫提供服務、資料和動作,讓使用者透過代理程式和系統層級互動完成工作。
AppFunctions 是 Model Context Protocol (MCP) 中工具的行動裝置版本。MCP 傳統上會標準化代理連線至伺服器端工具的方式,而 AppFunctions 則為 Android 應用程式提供相同的機制。這樣一來,您就能將應用程式的功能公開為可協調的「工具」,讓授權應用程式 (呼叫端) 探索及執行,以滿足使用者意圖。呼叫端必須具備 EXECUTE_APP_FUNCTIONS 權限,才能探索及執行 AppFunction,包括代理、應用程式和 Gemini 等 AI 助理。
AppFunctions 適用於搭載 Android 16 以上版本的裝置。
應用實例
AppFunctions 提供強大的機制,可自動執行工作並簡化使用者互動。開放應用程式功能後,使用者就能透過自然語言達成複雜目標,通常不必再逐步手動操作 UI。
以下情境說明如何使用 AppFunctions,在各種應用程式類別中推動體驗:
工作管理與效率提升
- 使用者要求:「提醒我今天下午 5 點在公司領取包裹」。
- AppFunction 動作:呼叫端會找出相關的工作管理應用程式,並叫用函式來建立工作,然後根據使用者的提示,自動填入標題、時間和位置欄位。
/** * Create a new task or reminder with a title, due time, and location. * * @param title The descriptive title of the task (e.g., "Pick up my package"). * @param dueDateTime The specific date and time when the task should be completed. * @param location The physical location associated with the task (e.g., "Work"). * @return The created Task */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun createTask( title: String, dueDateTime: LocalDateTime? = null, location: String? = null, ): Task = TODO()
媒體娛樂
- 使用者要求:「建立新的播放清單,收錄今年最熱門的爵士樂專輯」。
- AppFunction 動作:呼叫端在音樂應用程式中執行播放清單建立函式,並傳遞「2026 年熱門爵士專輯」等情境做為查詢,立即生成播放清單。
/** * Create a new music playlist based on a natural language query. * * @param query The description used to generate the playlist (e.g., "top jazz albums from 2026"). * @return The final created playlist based on songs. */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun createPlaylistFromQuery( query: String, ): Playlist = TODO()
跨應用程式工作流程
- 使用者要求:「找出 Lisa 電子郵件中的麵食食譜,並將食材加入我的購物清單」。
- AppFunction 動作:這項要求會使用多個應用程式的函式。首先,呼叫端會使用電子郵件應用程式的搜尋功能擷取內容。接著,模型會擷取相關食材,並叫用購物清單應用程式的函式,填入使用者的清單。
/** * Search for emails matching a query or sender name to retrieve content like recipes. * * @param query The search term or contact name (e.g., "Lisa noodle recipe"). * @return A list of matching email summaries containing the requested information. */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun searchEmails( query: String, ): List<EmailSummary> = TODO() /** * Add a list of items or ingredients to the user's active shopping list. * * @param items The names of the ingredients or products to add to the list. * @return The final shopping list with new items added */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun addItemsToShoppingList( items: List<String>, ): ShoppingList = TODO()
日曆和排程
- 使用者要求:「在日曆中新增下週一下午 6 點的媽媽生日派對」。
- AppFunction 動作:經核准的代理應用程式會叫用日曆應用程式的「建立活動」函式,剖析「下週一」和「下午 6 點」等相關情境,建立項目,使用者不必手動開啟日曆。
/** * Schedule a new event on the user's primary calendar. * * @param title The name of the calendar event (e.g., "Mom's birthday party"). * @param startDateTime The specific date and time the event is scheduled to begin. * @return The created Event object. */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun createCalendarEvent( title: String, startDateTime: LocalDateTime, ): Event = TODO()
AppFunctions 的運作方式
下圖說明應用程式將 AppFunctions 分享給代理,並隨後執行的典型流程。處理使用者要求時,代理可能會同時考量伺服器端遠端 MCP 工具和本機 AppFunction。使用本機 AppFunction 的詳細流程如下:
- AppFunction 宣告:Android 應用程式建構時,會使用 AppFunction 提供功能,例如「建立記事」或「傳送訊息」。
- 結構定義生成:AppFunctions Jetpack 程式庫會生成 XML 結構定義檔案,列出應用程式中所有宣告的 AppFunctions。Android 作業系統會使用這個檔案為可用的 AppFunctions 建立索引。
- 中繼資料擷取:代理程式可透過查詢擷取 AppFunction 中繼資料。除了函式專屬的 KDoc,開發人員還可在應用程式中繼資料中,定義應用程式層級的操作模式和限制,引導代理在多項工具間自動調度資源。
- AppFunction 選取和執行:代理程式會根據使用者提示,選取並執行適當的 AppFunction,並提供適當的參數。
AppFunctions Jetpack 程式庫可簡化應用程式功能的公開程序。您可以使用註解處理器,為要提供給代理的函式加上註解。呼叫端接著可以使用 AppFunctionManager 探索及叫用這些已建立索引的函式。
叫用函式前,呼叫端應嘗試擷取 AppFunctionManager 的例項,確認裝置支援 AppFunctions 功能。支援後,呼叫端可以使用 isAppFunctionEnabled(packageName,functionId) 驗證目標應用程式是否啟用特定函式。如要查詢其他套件中的函式狀態,則需要 android.permission.EXECUTE_APP_FUNCTIONS 權限。
應用程式不需要驗證是否支援 AppFunction 功能,Jetpack 程式庫會自動處理。舉例來說,AppFunctionManager 可以驗證是否支援該功能。
以下是記事應用程式的 AppFunctions 範例,具備建立、編輯和列出記事的功能:
@RequiresApi(36) @AndroidEntryPoint @AppFunctionServiceEntryPoint( serviceName = "TaskAppFunctionService", appFunctionXmlFileName = "task_app_function_service", ) abstract class BaseTaskAppFunctionService : AppFunctionService() { @Inject internal lateinit var taskRepository: TaskRepository /** * Creates a task with a title, content, or both. * * @param createTaskParams The title and content of the task. At least one * of them is required. * @return The created task, including its ID. */ @AppFunctionDeclaration(isDescribedByKDoc = true) suspend fun createTask( createTaskParams: CreateTaskParams, ): Task = withContext(Dispatchers.IO) { // Developers can use predefined exceptions to let the agent know // why it failed. if (createTaskParams.title == null && createTaskParams.content == null) { throw AppFunctionInvalidArgumentException("A task needs a title, content, or both.") } val id = taskRepository.createTask( createTaskParams.title, createTaskParams.content ) return@withContext taskRepository .getTask(id) ?.toTask() ?: throw AppFunctionElementNotFoundException("Task not found for ID = $id") } // Maps internal TaskEntity private fun TaskEntity.toTask() = Task(id = id, title = title, content = description) }
/** The title and content of a new task. */ @AppFunctionSerializable(isDescribedByKDoc = true) data class CreateTaskParams( /** The title of the task. */ val title: String?, /** The content of the task. */ val content: String?, ) /** The user-created task. */ @AppFunctionSerializable(isDescribedByKDoc = true) data class Task( /** The ID of the task. */ val id: String, /** The title of the task. */ val title: String, /** The content of the task. */ val content: String, )
範例、技能和測試工具
我們提供下列資源,協助您提升 AppFunctions 技能:
- 探索 AppFunctions 範例,驗證並瞭解裝置上的運作方式。
- 使用 AppFunctions 代理技能,加快四步驟生命週期的開發速度:
- 探索:分析程式碼集,找出並推薦適合 AI 協作的高價值功能。
- 實作與設定:生成 Kotlin 實作,並設定系統中繼資料和建構依附元件。
- KDoc Refinement:針對 AI 代理和 Android MCP,最佳化函式和屬性說明文件。
- 測試與偵錯:提供 ADB 指令,用於本機評估和裝置端偵錯。
- 測試函式的行為 (包括省略的預設值、主執行緒呼叫、
AppFunctionException錯誤和模型生成的輸入內容),以及代理選取和呼叫函式的方式。如要進一步瞭解單元、ADB 和代理程式測試,請參閱「驗證 AppFunction 整合」。 - 如要測試 Gemini 如何使用 AppFunctions,請按照「建立及管理連結的應用程式」中的步驟操作。
常見問題 (FAQ)
以下章節將回答 AppFunctions 的常見問題。
我是應用程式開發人員,現在可以導入 AppFunctions 嗎?
可以,只要按照前幾節所述的指引操作,即可在應用程式中實作及測試 AppFunctions。
AppFunctions 和 MCP 有何不同?
兩者都能讓 AI 代理自動調度管理工具,但在架構、延遲時間和開發人員所需投入的心力方面,有顯著差異。AppFunctions 是 Android 專屬的內建 OS 層級掛鉤,會在本機執行。相較之下,標準 MCP 伺服器是與平台無關的解決方案,仰賴雲端執行和網路來回。
簡而言之,使用 AppFunctions 開發時,您可以直接在裝置端使用現有的應用程式狀態,不必在 Android 應用程式外維護服務。
我已在應用程式中實作 AppFunctions,為什麼 Gemini 或其他 AI 代理無法存取?
- 如要在 Android 裝置上搭配 Gemini 使用 AppFunctions:請透過連結的應用程式平台開發人員預覽版,在 Google Connected Console 註冊並提交應用程式。在連結應用程式發布前,只有連結應用程式測試人員名單中的測試人員可以使用,且只能在您提交的應用程式套件版本上使用。如需設定步驟,請參閱 Google Connected Console 指南中的「連結的應用程式」和「Android MCP」部分。
- 從自己的代理呼叫 AppFunctions:應用程式可以呼叫在自身套件中宣告的 AppFunctions。如要探索及呼叫其他應用程式的 AppFunction,必須具備
EXECUTE_APP_FUNCTIONS特權權限,Android 只會將這項權限授予授權的系統助理。
如何為應用程式做好準備,以便在 AppFunctions 正式發布時使用?
請思考要讓代理自動化功能存取應用程式的哪些功能。您可以在應用程式中實作 AppFunctions。如要這麼做,請按照本頁前幾節的步驟操作,並使用應用程式的套件名稱呼叫 adb shell cmd app_function list-app-functions --package,確認這些函式已在裝置上註冊。
如何使用 Gemini 測試及發布 AppFunctions?
加入連結的應用程式平台開發人員預覽版。如要申請存取權,請申請預先發布版。加入後,請在 Google Connected Console 建立 Android MCP 連結應用程式,將應用程式套件上傳至測試群組,並將相同的測試人員加入這兩項。詳情請參閱「建立及管理連結的應用程式」。
如何提供 AppFunctions 相關意見回饋?
您可以提交問題,提供 API 相關意見。