AppFunctions 總覽

AppFunctions 是 Android 平台 API,並隨附 Jetpack 程式庫,可簡化 Android MCP 整合。這項功能可讓應用程式像裝置端 MCP 伺服器一樣運作,提供可做為工具的功能,供主動功能以及代理和助理 (例如 Google Gemini) 使用。截至 2026 年 5 月,AppFunctions 與 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 結構定義檔案,列出應用程式中所有宣告的 AppFunction。Android 作業系統會使用這個檔案,為可用的 AppFunction 建立索引。
  • 中繼資料擷取:代理程式可透過查詢擷取 AppFunction 中繼資料。除了函式專屬的 KDoc 外,開發人員還可在應用程式中繼資料中定義應用程式層級的操作模式和限制,引導代理在多項工具間自動調度資源。
  • AppFunction 選取和執行:代理會根據使用者提示,選取並執行適當的 AppFunction,並提供適當的參數。
從應用程式曝光到代理執行,AppFunctions 的一般流程。
圖 1:AppFunctions 的一般公開流程,以及後續由代理程式執行的流程。

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 based on [createTaskParams].
     *
     * @param createTaskParams The parameter to describe how to create the task.
     */
    @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("Title or content should be non-null")
        }

        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 parameter to create the 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 指令,用於本機評估和裝置端偵錯。
  • 如要進行指令列測試和驗證,請使用 adb shell cmd app_function ... 等 ADB 指令,直接以輕量方式測試函式註冊、檢查中繼資料說明,以及在裝置端執行函式。

常見問題 (FAQ)

以下章節提供 AppFunctions 的常見問題與解答。

我是應用程式開發人員,現在可以導入 AppFunctions 嗎?

可以,您可以按照前幾節所述的指引,在應用程式中實作及測試 AppFunctions。

AppFunctions 和 MCP 有何不同?

兩者都能讓 AI 代理自動調度管理工具,但在架構、延遲時間和開發人員所需投入的心力方面,有顯著差異。AppFunctions 是 Android 專屬的內建 OS 層級掛鉤,會在本機執行。相較之下,標準 MCP 伺服器是與平台無關的解決方案,仰賴雲端執行和網路往返。

簡而言之,使用 AppFunctions 開發時,您可以直接在裝置端使用現有的應用程式狀態,不必在 Android 應用程式外維護服務。

我已在應用程式中實作 AppFunctions,為什麼系統代理程式無法存取?

AppFunctions 是實驗功能。為了在實驗階段仔細評估整體體驗的品質,只有少數應用程式和系統代理程式可以存取整個管道。

如何為應用程式做好準備,以便在 AppFunctions 正式發布時使用?

請考量要讓代理自動化功能存取應用程式的哪些功能。您可以在應用程式中實作 AppFunctions。如要這麼做,請按照本頁前幾節的步驟操作,並使用應用程式的套件名稱呼叫 adb shell cmd app_function list-app-functions --package,確認這些函式已在裝置上註冊。

可以搶先體驗端對端代理式開發人員服務嗎?

我們正在進行搶先體驗計畫 (EAP),邀請特定應用程式參與測試,瞭解在 Android 上將 AppFunctions 推出至正式版所需的端對端開發人員體驗。您可以透過這份搶先體驗計畫註冊表單,登記整合 AppFunctions 的意願。登記意願並不代表您會自動取得完整整合功能的存取權。如果您的應用程式獲選加入 EAP,我們會傳送電子郵件通知您,或在 AppFunctions 公開發布時通知您。

如何提供 AppFunctions 相關意見回饋?

如要提供 API 相關意見,請提交問題,並填寫搶先體驗計畫表單,表達參與意願。