AppFunctions 概览

AppFunctions 是一种 Android 平台 API,附带 Jetpack 库,可简化 Android MCP 集成。它使您的应用能够像设备上的 MCP 服务器一样运行,提供充当工具的功能,供主动功能以及智能体和助理(例如 Google Gemini)使用。 截至 2026 年 5 月,AppFunctions 与 Gemini 的集成处于可信测试员非公开预览阶段。您可以立即开始准备应用,以便使用 AppFunctions 和开发工具。

通过定义这些 AppFunctions,您可以让应用向 Android OS 内置的注册表提供服务、数据和操作,从而让用户能够通过智能体和系统级互动完成任务。

AppFunctions 相当于 Model Context Protocol (MCP) 中的移动工具。MCP 传统上用于标准化智能体连接到服务器端工具的方式,而 AppFunctions 为 Android 应用提供了相同的机制。这样,您就可以将应用的功能公开为可编排的“工具”,授权的应用(调用方)可以发现和执行这些工具,以满足用户意图。调用方必须具有 EXECUTE_APP_FUNCTIONS 权限才能 发现和执行 AppFunctions,并且可以包括智能体、应用和 AI 助理(例如 Gemini)。

AppFunctions 适用于搭载 Android 16 或更高版本的设备。

应用场景示例

AppFunctions 提供了一种强大的机制来自动执行任务并简化用户互动。通过开放应用的功能,您可以让用户使用自然语言完成复杂的目标,通常无需使用界面进行逐步手动导航。

以下场景说明了如何使用 AppFunctions 来提升各种应用类别的体验:

  • 任务管理和工作效率

    • 用户请求:“提醒我今天下午 5 点在工作地点取包裹”。
    • AppFunction 操作:调用方会识别相关的任务 管理应用并调用函数来创建任务,根据用户提示自动 填充标题、时间和地点字段。
    /**
    *   Create a new task or reminder with a title, due time, and location.
    *
    *   @param context The execution context provided by the system.
    *   @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
    */
    @AppFunction(isDescribedByKDoc = true)
    suspend fun createTask(
        context: AppFunctionContext,
        title: String,
        dueDateTime: LocalDateTime? = null,
        location: String? = null
    ) : Task
    
  • 媒体和娱乐

    • 用户请求:“创建一个包含 今年最热门爵士专辑的新播放列表”。
    • AppFunction 操作:调用方在音乐应用中执行播放列表创建函数 ,传递“2026 年最热门爵士专辑”等上下文作为 查询,以立即生成播放列表。
    /**
    *   Create a new music playlist based on a natural language query.
    *
    *   @param context The execution context provided by the system.
    *   @param query The description used to generate the playlist (e.g., "top jazz albums from 2026").
    *   @return The final created playlist based on songs.
    */
    @AppFunction(isDescribedByKDoc = true)
    suspend fun createPlaylistFromQuery(
        context: AppFunctionContext,
        query: String
    ): Playlist
    
  • 跨应用工作流

    • 用户请求:“从 Lisa 的电子邮件中找到面条食谱,并将 配料添加到我的购物清单中”。
    • AppFunction 操作: 此请求使用来自多个应用的函数。 首先,调用方使用电子邮件应用的搜索功能检索内容。然后,它会提取相关配料并调用购物清单应用的函数来填充用户的清单。
    /**
    *   Search for emails matching a query or sender name to retrieve content like recipes.
    *
    *   @param context The execution context provided by the system.
    *   @param query The search term or contact name (e.g., "Lisa noodle recipe").
    *   @return A list of matching email summaries containing the requested information.
    */
    @AppFunction(isDescribedByKDoc = true)
    suspend fun searchEmails(
        context: AppFunctionContext,
        query: String
    ): List<EmailSummary>
    
    /**
    *   Add a list of items or ingredients to the user's active shopping list.
    *
    *   @param context The execution context provided by the system.
    *   @param items The names of the ingredients or products to add to the list.
    *   @return The final shopping list with new items added
    */
    @AppFunction(isDescribedByKDoc = true)
    suspend fun addItemsToShoppingList(
        context: AppFunctionContext,
        items: List<String>
    ): ShoppingList
    
  • 日历和日程安排

    • 用户请求:“将妈妈的生日派对添加到我的日历中,时间为下周 一下午 6 点”。
    • AppFunction 操作:获批准的智能体应用会调用日历 应用的“创建活动”函数,解析“下周一”和“下午 6 点”等相关上下文,以创建条目,而无需用户 手动打开日历。
    /**
    *   Schedule a new event on the user's primary calendar.
    *
    *   @param context The execution context provided by the system.
    *   @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.
    */
    @AppFunction(isDescribedByKDoc = true)
    suspend fun createCalendarEvent(
        context: AppFunctionContext,
        title: String,
        startDateTime: LocalDateTime
    ): Event
    

AppFunctions 的运作方式

下图说明了应用如何将 AppFunctions 共享给代理,以及随后如何执行这些 AppFunctions 的典型流程。在处理用户请求时,智能体可能会同时考虑服务器端远程 MCP 工具和本地 AppFunctions。使用本地 AppFunctions 的详细流程如下:

  • AppFunction 声明:Android 应用构建为使用 AppFunctions 来 提供其功能,例如“创建笔记”或“发送消息”。
  • 架构生成:AppFunctions Jetpack 库会生成一个 XML 架构文件,其中列出了应用中所有已声明的 AppFunctions。Android OS 使用此文件为可用的 AppFunctions 编制索引。
  • 元数据检索:智能体可以通过 查询来检索 AppFunction 元数据。
  • AppFunction 选择和执行:根据用户提示,智能体 会选择并执行具有相应参数的相应 AppFunction 。
从应用公开到代理执行的典型 AppFunctions 流程。
图 1:智能体公开和 随后执行 AppFunctions 的典型流程。

AppFunctions Jetpack 库简化了公开应用功能的过程。 借助注解处理器,您可以为要向智能体提供的函数添加注解。然后,调用方可以使用 AppFunctionManager 发现和调用这些已编制索引的 函数。

在调用函数之前,调用方应尝试检索 AppFunctionManager 的实例,以验证设备是否支持 AppFunctions 功能。如果支持,调用方可以使用 isAppFunctionEnabled(packageName,functionId)验证目标应用中是否启用了特定 函数。查询其他软件包中 函数的状态需要 android.permission.EXECUTE_APP_FUNCTIONSpermission

您的应用无需验证是否支持 AppFunction 功能;此操作会在 Jetpack 库中自动处理。例如, AppFunctionManager 可以验证是否支持该功能。

以下是笔记应用 AppFunctions 的示例,该应用具有创建、修改和列出笔记的功能:

/**
 * A note app's [AppFunction]s.
 */
class NoteFunctions(
    private val noteRepository: NoteRepository
) {
    /**
     * 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 noteRepository.appNotes.ifEmpty { null }?.toList()
    }

    /**
     * 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 note's 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: Int,
        title: String?,
        content: String?,
    ): Note? {
        return noteRepository.updateNote(noteId, title, content)
    }
}

/**
 * A note.
 */
@AppFunctionSerializable(isDescribedByKDoc = true)
data class Note(
    /** The note's identifier */
    val id: Int,
    /** The note's title */
    val title: String,
    /** The note's content */
    val content: String
)

常见问题解答 (FAQ)

以下部分解答了有关 AppFunctions 的常见问题。

我是一名应用开发者。我今天可以实现 AppFunctions 吗?

可以,您可以按照前面部分中详述的指南在应用中实现和测试 AppFunctions。

AppFunctions 和 MCP 有什么区别?

两者都允许 AI 智能体编排工具,但在架构、延迟时间和所需的开发者工作量方面存在显著差异。AppFunctions 是 Android 独有的内置操作系统级钩子,可在本地执行。相比之下,标准 MCP 服务器是一种与平台无关的解决方案,依赖于云执行和网络往返。

简而言之,使用 AppFunctions 进行开发可让您直接在设备上使用现有的应用状态,而无需在 Android 应用之外维护服务。

我已在应用中实现了 AppFunctions。为什么我的系统智能体无法访问它们?

AppFunctions 是一项实验性功能。为了在实验阶段仔细评估整体体验的质量,只有有限数量的应用和系统智能体可以访问整个流水线。

如何为 AppFunctions 正式版做好应用准备?

考虑要向智能体自动化公开哪些应用功能。您可以在应用中实现 AppFunctions。为此,请按照此页面前面部分中的步骤操作,并通过调用 adb shell cmd app_function list-app-functions 验证它们是否已在设备上注册。

我可以抢先体验端到端智能体开发者体验吗?

我们正在开展抢先体验计划 (EAP),以便让选定的应用测试在 Android 上将 AppFunctions 发布到生产环境所需的端到端开发者体验。您可以通过此 EAP 注册表单登记对集成您的 AppFunctions 的意向。登记意向并不意味着您会自动获得对完整集成的访问权限。如果您的应用入选 EAP,或者 AppFunctions 公开发布后,我们会向您发送电子邮件。

如何提供有关 AppFunctions 的反馈?

您可以提交问题并填写抢先体验计划表单来提供有关 API 的反馈。