如果您是 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:34.17.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:34.17.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 的强制执行。
在调试 build 中,将 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 控制台中,依次前往 安全性 > App Check > 应用 标签页。
找到您的应用,点击溢出菜单 (),然后选择 管理调试令牌。
按照屏幕上的说明注册调试令牌。
如需详细了解调试提供程序(包括如何获取新的调试令牌), 请参阅 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);
在 Firebase 文档中,您可以详细了解可用的 Gemini 模型。您还可以了解如何 配置模型参数。
生成文本
如需生成文本回答,请使用提示调用 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 文档。