使用 Home API 知識庫和 Gemini 開發 Android Home API 應用程式

1. 事前準備

建構項目

在本程式碼研究室中,您將使用 Home API 知識庫和 Android Studio 中的 Gemini,擴充現有的 Android Home API 範例應用程式,自動建立 UI 及整合 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. 事前準備和設定

請先確認已完成下列事項,再繼續操作:

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 Context 設定,顯示已選取 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. 建立新的「Debug」分頁

使用 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 生成程式碼,新增「偵錯」分頁

現在,底部會顯示新的「Debug」分頁:

範例應用程式底部導覽列,顯示新的「Debug」分頁

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. 實作「Dump Automation」功能

提示 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. 實作「摘要住家」按鈕,列出目前的房間和裝置

提示 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.

偵錯頁面上會顯示新的「Summarize Home」按鈕:

偵錯頁面顯示新的「Summarize Home」按鈕

點選後,系統會顯示房間和裝置的最新摘要:

「Summarize Home」對話方塊,列出目前的房間和裝置

9. 查看變更

Gemini 還能協助您查看變更 (或上次 Git 提交內容)、找出潛在問題並加以修正。

提示 Gemini:

Review the uncommitted changes and find potential issues.

Android Studio 內建 Gemini 正在審查未提交的程式碼變更

10. 恭喜!

恭喜!您已成功使用 Home API 知識庫和 Gemini 擴充 Android Home API 範例應用程式,自動建立 UI 並整合 API!您已瞭解如何運用 AI 輔助開發技術擴充範例應用程式,接下來可以嘗試以下做法:

  • 調整 AGENTS.md:嘗試在 AGENTS.md 檔案中加入特定程式設計標準或品牌宣傳指南,看看 Gemini 的程式碼建議會如何變化。
  • 建立自訂自動化動作:使用 Gemini 和知識庫編寫指令碼,觸發「晚安」日常安排,例如鎖上所有門和調暗燈光。
  • 閱讀官方 Home API 知識庫指南,並觀看運用 Gemini 和知識庫加速開發