智能体模式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Studio 中处于智能体模式的 Gemini 旨在处理复杂的多阶段开发任务,这些任务不是通过与 Gemini 聊天就能完成的。您可以描述一个高级目标,智能体将创建并执行一个方案,调用必要的工具,跨多个文件进行更改,并以迭代方式修复 bug。借助此代理辅助工作流程,您可以应对复杂的挑战,从而加快开发流程。
开始使用
如需在 Android Studio 中开始使用代理模式,请按以下步骤操作:
- 点击边栏中的 Gemini 图标
。
根据需要登录并完成初始配置。
- 选择代理标签页。
- 描述您希望代理执行的任务。
当代理按照步骤完成任务时,您可以选择查看并批准任何更改。
可选:如需自动批准更改,请依次选择代理选项
> 自动批准更改。
用例
以下是一些代理可以为您提供帮助的用例示例:
修复 build 错误。当您使用“修复项目中的 build 错误”之类的提示要求智能体修复 build 错误时,它会应用建议的修复方案,构建项目以验证解决方案,并进行迭代,直到问题得到解决。
代理模式可以自行添加或更新界面元素。例如,让代理执行“在用户偏好设置中将深色模式设为默认模式”任务,代理会搜索相关文件并建议更改,以完成任务。您可以立即在正在运行的设备工具窗口中预览界面更新。
在为应用制作原型和测试应用时,模拟数据非常有用。您可以让代理来完成这项任务,而无需手动创建虚假数据。在此示例中,我们要求代理“向虚假数据再添加两个会话”。它会找到相关文件,并向 mockSessions
列表添加另外两个事件。
您可以尝试的其他提示:
- “为 <module> 中的 <class> 编写单元测试。”
- “为当前打开的文件生成文档。”
- “将应用标题从 <当前名称> 更改为 <新名称>。”
- “解决 null 指针异常。”
- “重构我的代码,将 <可组合项名称> 可组合项移至新文件。
确保所有导入都已更新。”
- “向应用的主屏幕添加一个名为‘关注’的新按钮,点击该按钮可前往主题列表。”
- “在 <可组合项名称> 可组合项中,减少 <修饰符名称> 修饰符的边衬区。”
- “创建一个用于分享到社交媒体的分享按钮。”
代理的工作方式
在代理模式下,系统会将您的提示连同可用的工具列表一起发送给 Gemini API。您可以将工具视为技能:它们包括搜索文件、读取文件、搜索文件中的文本、使用您配置的任何 MCP 服务器等功能。
当您为代理提供任务时,它会制定计划并确定所需的工具。在代理使用这些工具之前,您可能需要授予相应权限。您授予权限后,代理会使用该工具执行必要的操作,并将结果发送回 Gemini API。Gemini 会处理操作的结果,并生成其他回答。这种操作和评估的循环会一直持续,直到任务完成。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-05。
[null,null,["最后更新时间 (UTC):2025-08-05。"],[],[],null,["# Agent Mode\n\nGemini in Android Studio's Agent Mode is designed to handle complex, multi-stage\ndevelopment tasks that go beyond what you can experience by just chatting with\nGemini. You can describe a high-level goal, and the agent will create and\nexecute a plan, invoking the necessary [tools](#how-it-works), making changes\nacross multiple files, and iteratively fixing bugs. This agent-assisted workflow\nempowers you to tackle intricate challenges, accelerating your development\nprocess.\n\nGet started\n-----------\n\nTo get started in Agent Mode in Android Studio, follow these steps:\n\n1. Click **Gemini** in the side bar. Sign in and onboard if you need to.\n2. Select the **Agent** tab.\n3. Describe the task you want the agent to perform.\n4. As the agent goes through the steps to accomplish the task, you'll have the\n option to review and approve any changes.\n\n5. Optional: To automatically approve changes, select **Agent options\n \\\u003e Auto-approve changes**.\n\nUse cases\n---------\n\nHere are some examples of use cases that the agent can help you with:\n\n- Fix build errors. When you ask the agent to fix a build error using a prompt\n like \"Fix build errors in my project,\" it will apply a recommended fix, build\n the project to verify the solution, and iterate until the issue is resolved.\n\n- Agent Mode can add or update UI elements all on its own. For example, ask the\n agent to \"Make dark mode the default in user preferences\" and it'll search for\n the relevant files and suggest changes to accomplish the task. You can\n immediately preview the UI updates in the **Running devices** tool window.\n\n- It's helpful to have mock data when prototyping and testing your app. Instead\n of manually creating fake data yourself, you can give that task to the agent.\n Here we've asked the agent to \"Add two more sessions to the fake data.\" It\n finds the relevant file and adds two more events to the `mockSessions` list.\n\nSome other prompts to try:\n\n- \"Write unit tests for \\\u003cclass\\\u003e in \\\u003cmodule\\\u003e.\"\n- \"Generate documentation for the currently open file.\"\n- \"Change the app title from \\\u003ccurrent name\\\u003e to \\\u003cnew name\\\u003e.\"\n- \"Resolve the null pointer exception.\"\n- \"Refactor my code by moving the \\\u003ccomposable name\\\u003e composable to a new file. Ensure all imports are updated.\"\n- \"Add a new button to the app's home screen called 'Follow' that takes you to a list of topics.\"\n- \"In the \\\u003ccomposable name\\\u003e composable, reduce the padding of the \\\u003cmodifier name\\\u003e modifier.\"\n- \"Create a share button to share to social media.\"\n\nHow the agent works\n-------------------\n\nIn Agent Mode, your prompt is sent to the Gemini API with a list of *tools* that\nare available. You can think of tools as skills: they include the ability to\nsearch for files, read files, search the text within files, use any\n[MCP servers](/studio/gemini/add-mcp-server) you have configured, and more.\n\nWhen you give the agent a task, it comes up with a plan and figures out which\ntools are needed. Some of these tools might need you to grant permission before\nthe agent can use them. Once you grant permission, the agent uses the tool to\nperform the necessary action and sends the result back to the Gemini API. Gemini\nprocesses the result of the action and generates another response. This cycle of\naction and evaluation continues until the task is complete."]]