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

本頁面提供使用 Firebase 時可能遇到的 Android 專屬問題提示和疑難排解方法。

有其他問題或未在下方找到您的問題嗎?請務必查看 主要 Firebase 常見問題,進一步瞭解 Firebase 或特定產品的常見問題。

您也可以前往 Firebase Android SDK GitHub 存放區,查看已回報的問題和疑難排解方法最新清單。我們也鼓勵您在該處提交自己的 Firebase Android SDK 相關問題!

Firebase Authentication (使用 Google 登入電話號碼登入時) 和 Firebase Dynamic Links 需要SHA-1 資訊。如果您沒有使用這些功能,就不需要提供 SHA-1。

如果我們偵測到其他 Firebase 或 Google Cloud 專案包含 OAuth 2.0 用戶端 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 程式庫停用 desugaring 功能 (請參閱版本資訊)。

這項變更表示使用 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 控制台的專案一般設定中新增支援電子郵件。
  • 您已在 Firebase 主控台將 從發布/實際運作金鑰庫取得的 SHA-1 憑證指紋新增至 Firebase Android 應用程式 (請前往 專案設定,然後向下捲動至「您的應用程式」,然後選取您的 Android 應用程式)。
  • 您已將 Google Play 控制台新增的 SHA-1 憑證指紋,新增至 Firebase 控制台中的 Firebase Android 應用程式 (請前往 「Project settings」,然後向下捲動至「Your apps」,然後選取您的 Android 應用程式)。

Firebase 提供下列 Gradle 外掛程式:

外掛程式名稱 Maven 座標 最新版本 外掛程式 ID
Google Play 服務外掛程式 com.google.gms:google-services 4.4.2 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.3 com.google.firebase.crashlytics
Performance Monitoring 外掛程式 com.google.firebase:perf-plugin 1.4.2 com.google.firebase.firebase-perf

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

  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'
        ...
    }