AppFunctions 概览

AppFunctions 是一个 Android 平台 API,并配有相应的 Jetpack 库,旨在简化 Android MCP 集成。它能赋予您的应用强大的能力,使其像设备端 MCP 服务器一样运作,并提供可作为工具使用的各种函数,供各类主动功能以及智能体和助理(例如 Google Gemini)调用。截至 2026 年 5 月,AppFunctions 与 Gemini 的集成正面向可信测试员开放私测预览。您现在可以开始准备应用,以便使用 AppFunctions 和开发者工具。

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

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

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

应用场景示例

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

以下场景说明了如何使用 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 工具和本地 AppFunctions。使用本地 AppFunctions 的详细流程如下:

  • AppFunction 声明:Android 应用在构建时会使用 AppFunction 来提供其功能,例如“创建记事”或“发送消息”。
  • 架构生成:AppFunctions Jetpack 库会生成一个 XML 架构文件,其中列出了应用中所有已声明的 AppFunctions。Android OS 会使用此文件为可用的 AppFunctions 编制索引。
  • 元数据检索:代理可以通过查询来检索 AppFunction 元数据。除了特定于函数的 KDoc 之外,开发者还可以在应用元数据中定义应用级操作模式和限制,以指导代理跨多个工具进行编排。
  • AppFunction 选择和执行:根据用户提示,智能体选择并执行具有适当参数的适当 AppFunction。
从应用公开到代理执行的典型 AppFunctions 流程。
图 1: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 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 实现,并配置系统元数据和 build 依赖项。
    • KDoc 优化:针对 AI 智能体和 Android MCP 优化函数和属性文档。
    • 测试和调试:提供 ADB 命令,以便在设备端进行本地评估和调试。
  • 对于命令行测试和验证,请使用 ADB 命令(例如 adb shell cmd app_function ...)作为直接、轻量级的方法来测试函数注册、检查元数据说明以及在设备上执行函数。

常见问题解答 (FAQ)

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

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

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

AppFunctions 与 MCP 有何区别?

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

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

我已在应用中实现 AppFunctions。为什么我的系统代理无法访问它们?

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

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

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

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

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

如何提供有关 AppFunctions 的反馈?

您可以通过提交问题和在抢先体验计划表单中登记意向,就该 API 提供反馈。