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 提供了一种强大的机制来自动执行任务并简化用户互动。通过开放应用的功能,您可以让用户使用自然语言完成复杂的目标,从而无需再通过界面进行逐步手动导航。
以下场景说明了如何使用 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 */ @AppFunction(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. */ @AppFunction(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. */ @AppFunction(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 */ @AppFunction(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. */ @AppFunction(isDescribedByKDoc = true) suspend fun createCalendarEvent( title: String, startDateTime: LocalDateTime, ): Event = TODO()
AppFunction 的运作方式
下图展示了应用如何将 AppFunction 共享给代理,以及随后如何执行这些 AppFunction 的典型流程。在处理用户请求时,代理可能会同时考虑服务器端远程 MCP 工具和本地 AppFunctions。使用本地 AppFunctions 的详细流程如下:
- 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_FUNCTIONSpermission。
您的应用无需验证是否支持 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. */ @AppFunction(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 shell cmd app_function ...等 ADB 命令,以直接、轻量的方式测试函数注册、检查元数据说明以及在设备上执行函数。 - 如需体验 Android MCP 的实际效果并测试端到端工作流,而无需任何提示,请在设备上安装并使用 AppFunctions 测试代理 Android 应用。
常见问题解答 (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 的反馈。