如果您是第一次使用 Gemini API,建議 Android 開發人員採用 Gemini Developer API 做為 API 供應商。不過,如果您有特定的資料位置需求,或已採用 Google Cloud 生態系統,則可搭配使用 Agent Platform Gemini API (舊稱 Vertex AI) 和 Firebase AI Logic,存取 Gemini Pro 和 Flash 模型。
開始使用
在應用程式中直接與 Agent Platform Gemini API 互動前,可以先在 Agent Studio 測試提示。
設定 Firebase 專案,並將應用程式連結至 Firebase
準備好從應用程式呼叫 API 後,請按照 Firebase AI Logic 入門指南「步驟 1」的操作說明設定 Firebase,並啟用必要的 API 和服務。
新增 Gradle 依附元件
將下列 Gradle 依附元件新增至應用程式模組:
Kotlin
dependencies {
// ... other androidx dependencies
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
// Add the dependencies for the Firebase AI Logic and App Check libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
implementation("com.google.firebase:firebase-appcheck-debug")
}
Java
dependencies {
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
// Add the dependencies for the Firebase AI Logic and App Check libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
implementation("com.google.firebase:firebase-appcheck-debug")
// Required for one-shot operations (to use `ListenableFuture` from Guava Android)
implementation("com.google.guava:guava:31.0.1-android")
// Required for streaming operations (to use `Publisher` from Reactive Streams)
implementation("org.reactivestreams:reactive-streams:1.0.4")
}
為本機開發環境設定 App Check 偵錯服務
2026 年 7 月初起,在 Firebase 控制台設定 AI Logic 時,系統會自動強制執行 Firebase App Check,保護 Gemini API。在本機開發環境中,您需要設定 App Check 偵錯服務,略過認證程序,同時維持 App Check 的強制執行。
在偵錯版本中,將 App Check 設為使用偵錯服務工廠:
Kotlin
Firebase.initialize(context = this) Firebase.appCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance(), )Java
FirebaseApp.initializeApp(/*context=*/ this); FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance(); firebaseAppCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance());取得偵錯權杖:
在模擬器或測試裝置上執行應用程式。
在記錄中尋找 App Check 偵錯權杖。例如:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
在 App Check 註冊偵錯權杖:
在 Firebase 控制台中,依序前往「Security」(安全性) >「App Check」 >「Apps」(應用程式) 分頁標籤。
找到應用程式,按一下溢位選單 (),然後選取「Manage debug tokens」。
按照畫面上的指示註冊偵錯權杖。
如要進一步瞭解偵錯服務 (包括如何取得新的偵錯權杖),請參閱 App Check 官方說明文件。
初始化生成模型
首先,請例項化 GenerativeModel 並指定模型名稱:
Kotlin
val model = Firebase.ai(backend = GenerativeBackend.agentPlatform()) .generativeModel("gemini-3.5-flash")
Java
GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.agentPlatform()) .generativeModel("gemini-3.5-flash"); GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);
如要進一步瞭解可用的 Gemini 模型,請參閱 Firebase 說明文件。您也可以參閱這篇文章,瞭解如何設定模型參數。
生成文字
如要生成文字回覆,請使用提示詞呼叫 generateContent()。
Kotlin
suspend fun generateText(model: GenerativeModel) { // Note: generateContent() is a suspend function, which integrates well // with existing Kotlin code. val response = model.generateContent("Write a story about a magic backpack.") // ... }
Java
Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); // ... } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor);
與 Gemini Developer API 類似,您也可以在文字提示詞中傳遞圖片、音訊、影片和檔案。詳情請參閱「透過應用程式與 Gemini Developer API 互動」。
如要進一步瞭解 Firebase AI Logic SDK,請參閱 Firebase 說明文件。