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

本頁提供 Android 相關問題的提示和疑難排解 可協助您瞭解使用 Firebase 時可能遇到的情況

是否遇到其他挑戰,或是您的問題與下方無關?請務必勾選 請參閱 Firebase 主要常見問題 產品相關常見問題

你也可以查看 Firebase Android SDK GitHub 存放區 查看已回報的問題與疑難排解的最新清單。我們建議您 回報自己的 Firebase Android SDK 相關問題!

SHA-1 資訊 需要 Firebase 驗證 (當使用 Google 登入電話號碼登入) 和 Firebase Dynamic Links。如果不使用 您不必提供 SHA-1。

如果我們偵測到其他 Firebase 或 Google Cloud,就會發生這個錯誤 專案包含具有套件名稱的 OAuth 2.0 用戶端 ID 和 SHA-1 格式瞭解詳情 解決這個錯誤

這個錯誤通常代表應用程式缺少一或多個參考資料 Google 的 Maven 存放區請務必納入 Google 的 Maven 存放區 (google())。

  • 如果您的專案使用 plugins 語法,請加入 的 plugins 部分 (位於 settings.gradle.ktssettings.gradle 檔案。
  • 如果您的專案使用 buildscript 語法,請加入 同時位於 buildscriptallprojects 專案層級 build.gradle.ktsbuild.gradle 檔案。

2021 年 5 月 (Firebase BoM v28.0.0),Firebase 全面停用了脫糖程序 Android 程式庫 (請參閱版本資訊)。

這項變更表示使用 Android Gradle 外掛程式 (AGP) v4.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.2 版 com.google.gms.google-services
應用程式發布外掛程式 com.google.firebase:firebase-appdistribution-gradle 5.0.0 com.google.firebase.appdistribution
Crashlytics 外掛程式 com.google.firebase:firebase-crashlytics-gradle 3.0.2 版 com.google.firebase.crashlytics
Performance Monitoring 外掛程式 com.google.firebase:perf-plugin 1.4.2 版 com.google.firebase.firebase-perf

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

  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. 在「module (應用程式層級)」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'
       
    ...
    }