使用 Home API 知识库和 Gemini 开发 Android Home API 应用

1. 准备工作

构建内容

在此 Codelab 中,您将使用 Home API 知识库和 Android Studio 中的 Gemini 扩展现有的 Android Home API 示例应用,以自动创建界面和集成 API。您将添加一个调试界面,让用户能够转储日志并总结其住宅情况,以便进行调试和开发。

强强联合:Home API 知识库 + Android Studio 中的 Gemini

此工作流依赖于两个关键组件:

  • Home API 知识库:一个精选文档文件 (google-home-api-knowledge-base.txt),为 Gemini 提供与 Google Home 设备交互所需的特定技术限制、模型和方法。
  • 请参阅:https://developers.home.google.com/apis/android/knowledge-base
  • Android Studio 中的 Gemini:直接内置于 IDE 中的 AI 赋能的协作工具。它能感知您的代码、资源和终端的上下文。

学习内容

  • 如何将 Home API 知识库作为上下文提供给 Android Studio 中的 Gemini。
  • 如何使用 Android Studio 中的 Gemini 进行氛围编程,为现有 Home API 示例应用添加新功能。

2. 前提条件和设置

在继续操作之前,请确保您已完成以下任务:

  • 构建并运行 Home API Android 示例应用:按照 https://developers.home.google.com/apis/android/sample-app/build#build_the_app 中的说明,构建并运行我们的 Codelab 所基于的示例应用。
  • 环境设置
  • 下载 Android Studio:从开发者官方网站安装最新的稳定版本(Koala 或更高版本)。
  • 启用 Gemini:打开 Gemini 工具窗口(通常位于右侧条带上)。使用您的 Google 账号登录并接受相关条款,即可激活 AI 助理。如需查看相关说明,请参阅 https://developer.android.com/studio/gemini/get-started。
  • 知识库文件:确保您已将 google-home-api-knowledge-base.txt 文件保存到项目目录中。
  • 注意:如果您从官方 GitHub 示例应用克隆了示例应用,则此文件已包含在 tools/ 文件夹中。

3. 创建 AGENTS.md 文件

无需在每次对话中重复指令,而是为 Gemini 创建一个持久的“大脑”。

在项目根目录中,创建一个名为 AGENTS.md 的新文件,并粘贴以下内容:

**Role:** You are an expert Android Home API Developer.

**Context:** This project uses the Google Home APIs.

**Source of Truth:** Always reference tools/google-home-api-knowledge-base.txt for Home API patterns, data models (Structures, Automations), and debugging methods.

**Guidelines:** Prioritize Material 3 Compose components and ensure all code is compatible with the latest Home API SDK.

在“上下文”中,选中 AGENTS.md Files 旁边的复选框。

Android Studio Gemini 上下文设置,其中 AGENTS.md 文件处于选中状态

验证设置是否成功。向 Gemini 发出以下提示:

Can you see the Agents.md file? Do you know what file you need to check first before developing with Google Home API?

您应该会看到类似于以下屏幕截图的结果,其中提到 Gemini 正在读取 AGENTS.md,并将使用知识库文件!

Android Studio 中的 Gemini 确认已读取 AGENTS.md 和知识库

4. 创建新的“调试”标签页

使用 Gemini + 知识库修改 HomeAppView.kt 并添加新的导航目的地。

在开始之前,您当前的 Home API 示例应用应如下所示:

Home API Android 示例应用,显示“设备”和“自动化操作”标签页

提示 Gemini 使用滚轮图标来表示新的调试标签页。

Can you help to add a wheel icon as the third tab in the HomeAppView .

以下屏幕截图显示了在 Android Studio 中与 Gemini 进行提示和回答对话的可能场景:

Android Studio 中的 Gemini 正在生成代码以添加“调试”标签页

现在,我们可以在底部看到一个新的“调试”标签页:

示例应用底部导航栏,其中显示了新的“调试”标签页

5. 创建 2 个新按钮,分别用于“转储结构”和“转储自动化”

向 Gemini 发出提示:

Create two selectable buttons on the Debug page called "Dump Structure" and "Dump Automations".

现在,“调试”页面上新增了 2 个按钮:

显示“转储结构”和“转储自动化操作”按钮的调试页面

6. 实现“转储结构”功能

向 Gemini 发出提示:

Implement the first button "Dump Structure" to call dumpStructure(structure: Structure) in Debugger.kt with current selected structure.

点击“Dump Structure”按钮后,结构日志会转储到 Logcat:

Android Studio Logcat 显示转储的结构日志

7. 实现“转储自动化”功能

向 Gemini 发出提示:

Now Implement the "Dump Automation" functionality to call dumpAutomationsInStructure(structure: Structure) in Debugger.kt with current selected structure.

当您点击“Dump Automation”按钮时,自动化日志会转储到 Logcat:

Android Studio Logcat 显示转储的自动化日志

8. 实现“Summarize Home”(总结住宅信息)按钮,用于列出当前的房间和设备

向 Gemini 发出提示:

In Debug tab, add a button "Summarize Home" to find the current room and devices with Home API and show them on a dialog.

“调试”页面上会显示一个新的“总结住宅”按钮:

“调试”页面,其中显示了新的“总结首页”按钮

点击该图标后,系统会显示会议室和设备的当前摘要:

总结列出当前房间和设备的 Home 对话框

9. 查看更改

Gemini 还可以帮助您查看更改(或上次 git 提交)、找出潜在问题并进行修复。

向 Gemini 发出提示:

Review the uncommitted changes and find potential issues.

Android Studio 中的 Gemini 正在审核未提交的代码更改

10. 恭喜!

恭喜!您已成功使用 Home API 知识库和 Gemini 自动创建界面并集成 API,从而扩展了 Android Home API 示例应用!现在,您已了解如何使用 AI 驱动的开发方式扩展示例应用,接下来可以尝试以下操作:

  • 优化 AGENTS.md:尝试在 AGENTS.md 文件中添加特定的编码标准或品牌规范,看看这会如何改变 Gemini 的代码建议。
  • 构建自定义自动化操作:使用 Gemini 和知识库编写一个脚本,用于触发“晚安”日常安排,例如锁上所有门和调暗灯光。
  • 阅读官方 Home API 知识库指南并观看利用 Gemini 和知识库加速开发