Google 服務 Gradle 外掛程式和 JSON 設定檔

將 Firebase 新增至 Android 專案時,您需要將 google-services Gradle 外掛程式 (com.google.gms.google-services) 和 google-services.json 設定檔新增至專案。外掛程式 和設定檔專用於 Firebase 服務和 Android 的 Firebase SDK。

向 Firebase 專案註冊 Android 應用程式時,Firebase 會提供 google-services.json 設定檔,您需要將該檔案新增至應用程式的程式碼集。這個設定檔包含 Firebase SDK 與 Firebase API 通訊所需的值 (稱為「Firebase 選項」),以及要互動的資源。建構時,Google 服務 Gradle 外掛程式 (google-services) 會將這個檔案轉換為 Android 字串資源;執行時,Firebase SDK 會讀取這些資源,初始化應用程式,並將應用程式連結至 Firebase 專案及其佈建的資源 (例如資料庫和儲存空間 bucket)。其中部分值為必要 (API 金鑰、專案 ID 和 Firebase 應用程式 ID),其他值則為選用,取決於您使用的 Firebase 服務和功能。

如果您使用 Firebase 控制台、Management REST API 或 Firebase CLI,將 Firebase 新增至 Android 專案,則必須手動將 Gradle 外掛程式和 JSON 設定檔新增至 Android 專案。不過,如果您使用 Firebase Assistant for Android Studio 或 Firebase 代理技能,系統會在設定期間自動完成這些工作。

雖然名為 Google 服務 Gradle 外掛程式,但其實是 Firebase 專用,與 Google Play services SDK 不同。

如果在設定外掛程式或 JSON 設定檔時遇到建構或設定問題,請參閱 Android 疑難排解和常見問題頁面。

新增 Gradle 外掛程式

Google 服務 Gradle 外掛程式 (google-services) 有兩項主要功能:

  • 處理 google-services.json 設定檔:將 JSON 檔案中的 Firebase 設定值轉換為 Android 字串資源,Firebase SDK 會在執行階段讀取這些資源,將應用程式連結至 Firebase 專案和資源。

  • 檢查依附元件版本相容性:確認應用程式建構作業中解析的 Firebase 和 Google Play services 程式庫版本符合各程式庫的版本需求,並在偵測到不相容版本時擲回建構錯誤。

如要將 Google 服務 Gradle 外掛程式新增至 Android 專案,請按照下列步驟操作:

  1. 在根層級 (專案層級) 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
    }
  2. 在模組 (應用程式層級) 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'
      // ...
    }

自訂外掛程式

如要自訂 Google 服務 Gradle 外掛程式的行為,請在模組 (應用程式層級) Gradle 檔案中新增選用的 googleServices 區塊,並加入下列其中一或兩個選用值:

  • disableVersionCheck:

    • 停用檢查 Firebase 和 Google Play services 程式庫依附元件是否具有相容版本。
    • 有效值為 true 或 false,預設值為 false。
  • missingGoogleServicesStrategy:

    • 設定缺少 google-services.json 設定檔時,建構作業的回應方式 (例如特定建構變化版本未使用 Firebase)。

    • 有效值為 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 設定檔

google-services.json 設定檔通常位於 Android 專案的模組 (應用程式層級) 根目錄 (通常是 app/)。此外,外掛程式也支援將特定變化版本的 JSON 檔案,放在 app/src/ 下的建構類型、產品變種版本或合併變化版本目錄中。以下所有目錄結構均有效:

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 設定檔的處理方式

google-services.json 設定檔的基本結構如下:

{
  "project_info": {...},
  "client": [...]
}

project_info 物件包含 Firebase 專案的一般資訊,而 client 陣列中的每個項目則包含在 Firebase 專案中註冊的 Firebase Android 應用程式設定資訊。

處理 Android 應用程式的 JSON 檔案時,外掛程式會檢查 client 陣列中的每個項目,並使用 client_info/android_client_info/package_name 值與目前建構變化版本的 Android 套件名稱 (在模組 (應用程式層級) Gradle 檔案中設為 applicationId) 相符的 client 物件。如果 client 中沒有任何項目與 applicationId 相符,外掛程式會在建構期間擲回 No matching client found for package name 例外狀況。

產生的 XML 資源檔案

處理 google-services.json 設定檔會產生 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 路徑的 google-services.json 設定檔 (其中 YOUR_CLIENT 是指 client 陣列中與應用程式 applicationId 相符的項目)。

不同的 Firebase 服務需要不同的選項,但所有 Firebase 服務 (包括 Firebase 安裝) 都至少需要三個選項:google_app_id (Firebase 應用程式 ID)、project_id (Firebase 專案 ID) 和 google_api_key (Firebase API 金鑰)。其他值則為選用,取決於您使用的 Firebase 服務和功能。

如果 Android 專案有自訂建構設定,導致您無法使用 google-services Gradle 外掛程式,您可以在應用程式的 res/values/ 目錄中,使用這些值手動定義這些字串資源。

  • google_app_id

    • Firebase Android 應用程式 ID:這是 Firebase Android 應用程式的專屬 ID,不是 Android 套件名稱或 Android 應用程式 ID
    • 一律顯示;所有 Firebase 服務皆須提供
    YOUR_CLIENT/client_info/mobilesdk_app_id
    
  • project_id

    project_info/project_id
    
  • google_api_key

    • Firebase Android 應用程式的 Firebase API 金鑰
    • 一律顯示;所有 Firebase 服務皆須提供
    YOUR_CLIENT/api_key/current_key
    
  • gcm_defaultSenderId

    • Firebase Cloud Messaging 的預設寄件者 ID (保留舊版 gcm_ 前置字元)
    • 一律顯示
    project_info/project_number
    
  • google_storage_bucket

    • Firebase 專案的預設 Cloud Storage for Firebase bucket
    • 一律存在 (請注意,您必須在 Firebase 專案中佈建這個值區,應用程式才能使用這個值)
    project_info/storage_bucket
    
  • firebase_database_url

    • Firebase 專案的預設 Firebase Realtime Database 網址
    • 在 Firebase 專案中佈建第一個 Firebase Realtime Database 執行個體時新增
    project_info/firebase_url
    
  • default_web_client_id

    • OAuth 2.0 網頁用戶端 ID:由 Firebase Authentication 使用 (例如 Google 登入)
    • 在 Firebase 專案中設定 OAuth 2.0 網頁用戶端時新增
    YOUR_CLIENT/oauth_client/client_id (client_type == 3)