在将 Firebase 添加到您的 Android 项目时,您需要将 Gradle 插件 (com.google.gms.google-services) 和 配置文件添加到您的项目。该插件和配置文件专门用于 Firebase 服务和 Firebase SDK for Android。
当您在 Firebase 项目中注册 Android 应用时,Firebase 会提供一个 配置文件,供您添加到应用的代码库中。此配置文件包含 Firebase SDK 与 Firebase API 通信以及识别要与之交互的资源所需的值(称为 Firebase 选项)。在构建时,Google 服务 Gradle 插件 () 会将此文件转换为 Android 字符串资源;在运行时,Firebase SDK 会读取这些资源,以初始化您的应用并将其连接到您的 Firebase 项目及其预配的资源(例如数据库和存储桶)。其中一些值是必需的(API 密钥、项目 ID 和 Firebase 应用 ID),而其他值则是可选的,具体取决于您使用的 Firebase 服务和功能。
如果您使用 Firebase 控制台、Management REST API 或 Firebase CLI 将 Firebase 添加到 Android 项目,则必须手动将 Gradle 插件和 JSON 配置文件添加到 Android 项目。不过,如果您使用 Firebase Assistant for Android Studio 或 Firebase 智能体 skill,则系统会在设置过程中自动为您完成这些任务。
尽管名称如此,但 Google 服务 Gradle 插件是 Firebase 专用的,与 Google Play services SDK 不同。
如果您在设置插件或 JSON 配置文件时遇到 build 或配置问题,请参阅 Android 问题排查和常见问题解答页面。
添加 Gradle 插件
Google 服务 Gradle 插件 () 有两个主要功能:
处理
配置文件:将 JSON 文件中的 Firebase 配置值转换为 Android 字符串资源,Firebase SDK 会在运行时读取这些资源,以将您的应用连接到 Firebase 项目和资源。google-services.json 检查依赖项版本兼容性:验证应用 build 中解析的 Firebase 和 Google Play services 库版本是否满足每个库的版本要求,并在检测到不兼容的版本时抛出 build 错误。
将 Google 服务 Gradle 插件添加到 Android 项目的方法如下:
在您的根级(项目级)Gradle 文件(
<project>/build.gradle.kts或<project>/build.gradle)中,将 Google 服务 Gradle 插件添加为依赖项:Kotlin
plugins { id("com.android.application") version "7.3.0" apply false // ... // Add the dependency for the Google services Gradle plugin id("com.google.gms.google-services") version "4.6.0" apply false }
Groovy
plugins { id 'com.android.application' version '7.3.0' apply false // ... // Add the dependency for the Google services Gradle plugin id 'com.google.gms.google-services' version '4.6.0' apply false }
在您的模块(应用级)Gradle 文件(通常是
<project>/<app-module>/build.gradle.kts或<project>/<app-module>/build.gradle)中,采用 Google 服务 Gradle 插件:Kotlin
plugins { id("com.android.application") // Add the Google services Gradle plugin id("com.google.gms.google-services") // ... }
Groovy
plugins { id 'com.android.application' // Add the Google services Gradle plugin id 'com.google.gms.google-services' // ... }
自定义插件
您可以在模块(应用级)Gradle 文件中添加一个可选的 googleServices 代码块,并使用以下一个或两个可选值,来自定义 Google 服务 Gradle 插件的行为:
disableVersionCheck:- 停用检查 Firebase 和 Google Play services 库依赖项是否具有兼容版本的功能。
- 有效值为
true或false;默认值为false。
missingGoogleServicesStrategy:配置在缺少
配置文件时(例如,如果特定 build 变体不使用 Firebase)build 的响应方式。google-services.json 有效值为
MissingGoogleServicesStrategy.IGNORE、MissingGoogleServicesStrategy.WARN或MissingGoogleServicesStrategy.ERROR;默认值为MissingGoogleServicesStrategy.ERROR。
以下是配置 googleServices 块的示例:
Kotlin
import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy
googleServices {
disableVersionCheck = false // Set to true only if you need to bypass version checks
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.WARN // Log a warning instead of failing the build
}
Groovy
import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy
googleServices {
disableVersionCheck = false // Set to true only if you need to bypass version checks
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.WARN // Log a warning instead of failing the build
}
添加 JSON 配置文件
配置文件通常放置在 Android 项目的模块(应用级)根目录(通常为 app/)中。该插件还支持将特定于变体的 JSON 文件放置在 app/src/ 下的 build 类型、产品变种或组合变体目录中。以下所有目录结构均有效:
app/
google-services.json // Module root (default fallback)
src/debug/google-services.json // Build type (for example, debug or release)
src/free/google-services.json // Product flavor (for example, free or paid)
src/free/release/google-services.json // Product flavor and build type
src/paidDebug/google-services.json // Combined variant directory
...
JSON 配置文件的处理方式
配置文件具有以下基本结构:
{
"project_info": {...},
"client": [...]
}
project_info 对象包含有关 Firebase 项目的一般信息,而 client 数组中的每个条目都包含在 Firebase 项目中注册的 Firebase Android 应用的配置信息。
在为 Android 应用处理 JSON 文件时,该插件会检查 client 数组中的每个条目,并使用其 client_info/android_client_info/package_name 值与当前 build 变体的 Android 软件包名称(在模块级 Gradle 文件中配置为 applicationId)相匹配的 client 对象。如果 client 中没有与 applicationId 匹配的条目,插件会在构建期间抛出 No matching client found for package name 异常。
生成的 XML 资源文件
处理 配置文件会生成一个 XML 资源文件 (values.xml),其中包含 Firebase SDK 在运行时读取的 Android 字符串资源,用于在应用中初始化 Firebase。
此文件位于:
app/build/generated/res/process{build_variant}GoogleServices/values/values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Required Firebase options (always present) -->
<string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>
<string name="project_id" translatable="false">mydemoapp</string>
<string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<!-- Always present in Firebase Android configurations -->
<string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
<string name="google_storage_bucket" translatable="false">mydemoapp.firebasestorage.app</string>
<!-- Added when the corresponding Firebase services are configured -->
<string name="firebase_database_url" translatable="false">https://mydemoapp-default-rtdb.firebaseio.com</string>
<string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
<!-- Legacy resource emitted for backwards compatibility -->
<string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
</resources>
XML 资源文件中的值
values.xml 中的每个值都提供一个 Firebase 选项,该选项源自 配置文件,位于下面列出的 JSON 路径中(其中 YOUR_CLIENT 是指 client 数组中与应用的 applicationId 匹配的条目)。
不同的 Firebase 服务需要不同的选项,但所有 Firebase 服务(包括 Firebase 安装)都至少需要三个选项:google_app_id(Firebase 应用 ID)、project_id(Firebase 项目 ID)和 google_api_key(Firebase API 密钥)。其他值是可选的,具体取决于您使用的 Firebase 服务和功能。
如果您的 Android 项目具有自定义 build 设置,导致您无法使用 Gradle 插件,则可以使用这些值在应用的 res/values/ 目录中安全地手动定义这些字符串资源。
google_app_id- Firebase Android 应用 ID:这是 Firebase Android 应用的唯一标识符,不是 Android 软件包名称或 Android 应用 ID
- 始终存在;所有 Firebase 服务都需要
YOUR_CLIENT/client_info/mobilesdk_app_idproject_id- Firebase 项目 ID
- 始终存在;所有 Firebase 服务都需要
project_info/project_idgoogle_api_key- Firebase Android 应用的 Firebase API 密钥
- 始终存在;所有 Firebase 服务都需要
YOUR_CLIENT/api_key/current_keygcm_defaultSenderId- Firebase Cloud Messaging 的默认发送者 ID(保留旧版
gcm_前缀) - 始终存在
project_info/project_number- Firebase Cloud Messaging 的默认发送者 ID(保留旧版
google_storage_bucket- Firebase 项目的默认 Cloud Storage for Firebase 存储桶
- 始终存在(请注意,您必须在 Firebase 项目中预配此存储桶,应用才能使用此值)
project_info/storage_bucketfirebase_database_url- Firebase 项目的默认 Firebase Realtime Database 网址
- 在 Firebase 项目中预配第一个 Firebase Realtime Database 实例时添加
project_info/firebase_urldefault_web_client_id- OAuth 2.0 Web 客户端 ID,由 Firebase Authentication(例如 Google 登录)使用
- 在 Firebase 项目中配置 OAuth 2.0 Web 客户端时添加
YOUR_CLIENT/oauth_client/client_id (client_type == 3)