疑難排解(&A);Android 和 Firebase 常見問題

本頁提供提示和疑難排解資訊,協助您解決使用 Firebase 時可能遇到的 Android 專屬問題。

遇到其他問題,或找不到下方列出的問題嗎?如要查看更多 Firebase 或產品專屬的常見問題,請務必參閱主要 Firebase 常見問題

您也可以查看 Firebase Android SDK GitHub 存放區,取得最新的回報問題和疑難排解清單。我們也鼓勵您在該處回報 Firebase Android SDK 相關問題!

發生這項錯誤的原因可能是您使用 Firebase BoM,並將 KTX 模組指定為產品程式庫依附元件。

我們已於 2025 年 7 月停止發布 KTX 模組的新版本,並從 Firebase Android BoM (34.0.0 版) 移除 KTX 程式庫。

如果您使用先前發布的 KTX 模組中的 KTX API,建議將應用程式遷移至主要模組,改用其中的 KTX API。詳情請參閱這項計畫的常見問題

SHA-1 資訊Firebase Authentication 的必要資訊 (使用 Google 登入電話號碼登入時),也是 Firebase Dynamic Links 的必要資訊。如果您未使用這些功能,則不必提供 SHA-1。

如果我們偵測到另一個 Firebase 或 Google Cloud專案包含 OAuth 2.0 用戶端 ID,且該 ID 使用您指定的套件名稱和 SHA-1,就會發生這個錯誤。瞭解如何解決這項錯誤

這個錯誤通常表示應用程式缺少一或多個對 Google Maven 存放區的參照。請務必在 Gradle 設定檔中加入 Google 的 Maven 存放區 (google())。

  • 如果專案使用 plugins 語法,請在 settings.gradle.ktssettings.gradle 檔案的 plugins 區段中加入該語法。
  • 如果專案使用 buildscript 語法,請在專案層級 build.gradle.ktsbuild.gradle 檔案的 buildscriptallprojects 區段中加入該語法。

在 2021 年 5 月 (Firebase BoM 28.0.0 版),Firebase 為所有 Android 程式庫停用去糖化功能 (請參閱版本資訊)。

這項異動表示,使用 Android Gradle 外掛程式 (AGP) 4.2 以下版本的 Gradle 建構作業,需要啟用 Java 8 支援。否則,在新增 Firebase SDK 時,這些 Android 專案會發生下列建構失敗情形:

D8: Invoke-customs are only supported starting with Android O (--min-api 26)
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.
The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle
android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
See https://developer.android.com/studio/write/java8-support.html for details.
Alternatively, increase the minSdkVersion to 26 or above.

如要修正這個建構失敗問題,可以採取下列任一做法:

  • 將錯誤訊息中列出的 compileOptions 新增至應用程式層級build.gradle.ktsbuild.gradle 檔案。
  • 將 Android 專案的 minSdkVersion 提高至 26 以上。

發生這種情況的原因可能有兩個:您未提供支援電子郵件地址,或是缺少 SHA 金鑰。如要修正這項錯誤,請確認所有條件都符合:

Firebase 提供下列 Gradle 外掛程式:

外掛程式名稱 Maven 座標 最新版本 外掛程式 ID
Google Play 服務外掛程式 com.google.gms:google-services 4.4.3 com.google.gms.google-services
App Distribution 外掛程式 com.google.firebase:firebase-appdistribution-gradle 5.1.1 com.google.firebase.appdistribution
Crashlytics 外掛程式 com.google.firebase:firebase-crashlytics-gradle 3.0.5 com.google.firebase.crashlytics
Performance Monitoring 外掛程式 com.google.firebase:perf-plugin 2.0.0 com.google.firebase.firebase-perf

以下說明如何將 Firebase 外掛程式新增至仍使用 buildscript 語法的 Android 專案:

  1. 根層級 (專案層級) 的 Gradle 檔案 (<project>/build.gradle.kts<project>/build.gradle) 中,使用外掛程式的 Maven 座標,將外掛程式新增為依附元件:

    KotlinGroovy
    buildscript {
    
        repositories {
          // Make sure that you have the following two repositories
          google()  // Google's Maven repository
          mavenCentral()  // Maven Central repository
        }
    
        dependencies {
          ...
    
          // Add the Maven coordinates and latest version of the plugin
          classpath ("PLUGIN_MAVEN_COORDINATES:PLUGIN_VERSION")
        }
    }
    
    allprojects {
      ...
    
      repositories {
        // Make sure that you have the following two repositories
        google()  // Google's Maven repository
        mavenCentral()  // Maven Central repository
      }
    }
    
    buildscript {
    
        repositories {
          // Make sure that you have the following two repositories
          google()  // Google's Maven repository
          mavenCentral()  // Maven Central repository
        }
    
        dependencies {
          ...
    
          // Add the Maven coordinates and latest version of the plugin
          classpath 'PLUGIN_MAVEN_COORDINATES:PLUGIN_VERSION'
        }
    }
    
    allprojects {
      ...
    
      repositories {
        // Make sure that you have the following two repositories
        google()  // Google's Maven repository
        mavenCentral()  // Maven Central repository
      }
    }
    
  2. 模組 (應用程式層級) Gradle 檔案 (通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle) 中,使用外掛程式 ID 新增外掛程式:

    KotlinGroovy
    plugins {
        id("com.android.application")
    
        // Add the ID of the plugin
        id("FIREBASE_PLUGIN_ID")
        ...
    }
    
    plugins {
        id 'com.android.application'
    
        // Add the ID of the plugin
        id 'FIREBASE_PLUGIN_ID'
        ...
    }