將 Firebase 新增至您的 Android 專案

必要條件

  • 安裝或更新 Android Studio 至最新版本。

  • 請確認專案符合下列規定 (請注意,部分產品可能有更嚴格的規定):

    • 目標 API 級別 24 (Nougat) 以上版本
    • 使用 Android 7.0 以上版本
    • 使用 Jetpack (AndroidX),包括符合下列版本需求:
      • com.android.tools.build:gradle 7.3.0 以上版本
      • compileSdkVersion 28 以上版本
  • 設定實體裝置或使用模擬器執行應用程式。
    請注意,依附於 Google Play services 的 Firebase SDK 需要在裝置或模擬器上安裝 Google Play services。

如果您還沒有 Android 專案,只是想試用 Firebase 產品,可以下載快速入門導覽課程範例。


您可以透過下列任一方式,將 Android 應用程式連結至 Firebase:

  • 方法 1:(建議) 使用 Firebase 控制台設定工作流程。
  • 方法 2:使用 Android Studio Firebase Assistant (可能需要額外設定)。



方法 1:使用 Firebase 控制台新增 Firebase

如要將 Firebase 新增至應用程式,您必須在 Firebase 控制台和開啟的 Android 專案中執行工作 (例如從控制台下載 Firebase 設定檔,然後移至 Android 專案)。

步驟 1:建立 Firebase 專案

將 Firebase 新增至 Android 應用程式前,您必須先建立 Firebase 專案,才能連結至 Android 應用程式。如要進一步瞭解 Firebase 專案,請參閱「瞭解 Firebase 專案」。

步驟 2:向 Firebase 註冊應用程式

如要在 Android 應用程式中使用 Firebase,您必須向 Firebase 專案註冊應用程式。註冊應用程式通常稱為將應用程式「新增」至專案。

  1. 前往 Firebase 控制台。

  2. 在「專案總覽」頁面中間,按一下「Android」圖示 () 或「新增應用程式」,啟動設定工作流程。

  3. 在「Android 套件名稱」欄位中,輸入應用程式的套件名稱。

  4. (選用) 輸入應用程式暱稱,這是內部使用的便利 ID,只有您可以在 Firebase 控制台看到。

  5. 按一下 [Register app] (註冊應用程式)。

步驟 3:新增 Firebase 設定檔

  1. 下載應用程式的 Firebase 設定檔 (google-services.json),並新增至程式碼集:

    1. 按一下「Download google-services.json」,取得應用程式的 Firebase 設定檔。

    2. 將設定檔移到應用程式的模組 (應用程式層級) 根目錄。

  2. 須有 Google 服務 Gradle 外掛程式 (google-services),Firebase SDK 才能存取 google-services.json 設定檔中的值。

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

步驟 4:在應用程式中加入 Firebase SDK

  1. 在模組 (應用程式層級) Gradle 檔案 (通常是 <project>/<app-module>/build.gradle.kts 或 <project>/<app-module>/build.gradle) 中,新增要在應用程式中使用的 Firebase 產品依附元件。建議使用 Firebase Android BoM 控制程式庫版本。

    已啟用 Analytics

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation(platform("com.google.firebase:firebase-bom:35.2.0"))
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // Add the dependency for the Firebase SDK for Google Analytics
      implementation("com.google.firebase:firebase-analytics")
    
      // TODO: Add the dependencies for any other Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation("com.google.firebase:firebase-auth")
      implementation("com.google.firebase:firebase-firestore")
    }

    如果使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。

    未啟用Analytics

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation(platform("com.google.firebase:firebase-bom:35.2.0"))
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // TODO: Add the dependencies for Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation("com.google.firebase:firebase-auth")
      implementation("com.google.firebase:firebase-firestore")
    }

    如果使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。

  2. 新增所需產品的依附元件後,請同步處理 Android 專案和 Gradle 檔案。

大功告成!您可以直接跳到後續步驟,查看建議做法。

不過,如果設定時遇到問題,請參閱 Android 疑難排解和常見問題。



方法 2:使用 Firebase Assistant 新增 Firebase

Firebase Assistant 會使用 Firebase 專案註冊應用程式,並在 Android 專案中新增必要的 Firebase 檔案、外掛程式和依附元件,所有操作都能在 Android Studio 中完成!

  1. 在 Android Studio 中開啟 Android 專案,然後確認您使用的是最新版本的 Android Studio 和 Firebase 助理:

    • Windows/Linux:依序點選「說明」>「檢查更新」
    • macOS:依序點選「Android Studio」>「Check for updates」
  2. 依序點選「Tools」>「Firebase」,開啟 Firebase Assistant。

  3. 在「Assistant」窗格中,選擇要新增至應用程式的 Firebase 產品。展開該產品的區段,然後按一下教學課程連結 (例如「Analytics」>「Log an Analytics event」)。

    1. 按一下「Connect to Firebase」,將 Android 專案連結至 Firebase。

    2. 按一下按鈕,新增所需的 Firebase 產品 (例如「 Add Analytics to your app」)。

  4. 同步處理應用程式,確保所有依附元件皆為必要的版本。

  5. 在「Assistant」窗格中,按照所選 Firebase 產品的其餘設定操作說明進行。

  6. 您可以透過 Firebase Assistant 新增任意數量的其他 Firebase 產品!

大功告成!請務必查看建議的後續步驟。

不過,如果設定時遇到問題,請參閱 Android 疑難排解和常見問題。



可用的程式庫

本節列出 Android 支援的 Firebase 產品及其 Gradle 依附元件。進一步瞭解這些 Firebase Android 程式庫:

請注意,使用 Firebase Android BoM 時,在 Gradle 建構設定檔中宣告 Firebase 程式庫依附元件時,不必指定個別程式庫版本。

服務或產品 Gradle 依附元件 最新
版本
要新增「Analytics」嗎?
Firebase Android BoM
(Bill of Materials)
com.google.firebase:firebase-bom

最新的 Firebase BoM 版本包含每個 Firebase Android 程式庫的最新版本。如要瞭解哪些程式庫版本對應至特定 BoM 版本,請查看該 BoM 版本的版本資訊。

35.2.0
Google 服務 Gradle 外掛程式 com.google.gms:google-services 4.6.0
AdMob com.google.android.gms:play-services-ads 25.5.0
Firebase AI Logic 1 com.google.firebase:firebase-ai 18.2.0
Firebase AI Logic On-Device com.google.firebase:firebase-ai-ondevice 16.0.0-beta08
Firebase AI KSP com.google.firebase:firebase-ai-ksp-processor 17.0.0
Analytics com.google.firebase:firebase-analytics 24.0.0
App Check 個自訂供應商 com.google.firebase:firebase-appcheck 20.1.0
App Check 偵錯服務 com.google.firebase:firebase-appcheck-debug 20.1.0
App Check Play Integrity 供應商 com.google.firebase:firebase-appcheck-playintegrity 20.1.0
App Check reCAPTCHA 供應商 com.google.firebase:firebase-appcheck-recaptcha 20.1.0
App Distribution com.google.firebase:firebase-appdistribution 16.0.0-beta21
App Distribution API com.google.firebase:firebase-appdistribution-api 16.0.0-beta21
App Distribution 外掛程式 com.google.firebase:firebase-appdistribution-gradle 5.3.1
Authentication com.google.firebase:firebase-auth 25.0.0
Cloud Firestore com.google.firebase:firebase-firestore 27.2.0
Cloud Functions for Firebase Client SDK com.google.firebase:firebase-functions 23.0.0
Cloud Messaging com.google.firebase:firebase-messaging 26.0.2
Cloud Storage com.google.firebase:firebase-storage 23.0.0
Crashlytics com.google.firebase:firebase-crashlytics 21.1.0
Crashlytics NDK com.google.firebase:firebase-crashlytics-ndk 21.1.0
Crashlytics 外掛程式 com.google.firebase:firebase-crashlytics-gradle 3.0.8
支援動態功能模組 com.google.firebase:firebase-dynamic-module-support 16.0.0-beta05
In-App Messaging com.google.firebase:firebase-inappmessaging 23.0.0
(必填)
In-App Messaging 螢幕 com.google.firebase:firebase-inappmessaging-display 23.0.0
(必填)
Firebase 次安裝 com.google.firebase:firebase-installations 20.0.0
Performance Monitoring com.google.firebase:firebase-perf 23.0.1
Performance Monitoring 外掛程式 com.google.firebase:perf-plugin 2.0.2
Firebase Phone Number Verification com.google.firebase:firebase-pnv 17.0.0
Realtime Database com.google.firebase:firebase-database 23.0.0
Remote Config com.google.firebase:firebase-config 24.0.1
SQL Connect 2 com.google.firebase:firebase-dataconnect 18.0.1
已淘汰或不支援的程式庫
Dynamic Links com.google.firebase:firebase-dynamic-links 22.1.0

Firebase KTX 模組 - 已停止支援

Analytics 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-analytics-ktx
22.5.0
App Check 個自訂供應商 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-appcheck-ktx
18.0.0
App Distribution API 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-appdistribution-api-ktx
16.0.0-beta15
Authentication 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-auth-ktx
23.2.1
Cloud Firestore 請勿使用;KTX 模組程式庫已不再支援。
com.google.firebase:firebase-firestore-ktx
25.1.4
Cloud Functions for Firebase Client SDK 請勿使用;KTX 模組程式庫已不再支援。
com.google.firebase:firebase-functions-ktx
21.2.1
Cloud Messaging 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-messaging-ktx
24.1.2
Cloud Storage 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-storage-ktx
21.0.2
Crashlytics 請勿使用;KTX 模組程式庫已不再支援。
com.google.firebase:firebase-crashlytics-ktx
19.4.4
Dynamic Links 請勿使用;系統已不再支援 KTX 模組程式庫。
com.google.firebase:firebase-dynamic-links-ktx
22.1.0
In-App Messaging 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-inappmessaging-ktx
21.0.2
(必填)
In-App Messaging 螢幕 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-inappmessaging-display-ktx
21.0.2
(必填)
Firebase 次安裝 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-installations-ktx
18.0.0
Firebase ML Model Downloader API 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-ml-modeldownloader-ktx
25.0.1
Performance Monitoring 請勿使用;系統不再支援 KTX 模組程式庫。
com.google.firebase:firebase-perf-ktx
21.0.5
Realtime Database 請勿使用;系統已不再支援 KTX 模組程式庫。
com.google.firebase:firebase-database-ktx
21.0.0
Remote Config 請勿使用;系統已不再支援 KTX 模組程式庫。
com.google.firebase:firebase-config-ktx
22.1.2

Firebase ML 和 Firebase ML Kit 程式庫

Firebase ML 自訂模型 API com.google.firebase:firebase-ml-model-interpreter 22.0.4
Firebase ML Model Downloader API com.google.firebase:firebase-ml-modeldownloader 26.1.1
Firebase ML Vision API com.google.firebase:firebase-ml-vision 24.1.0
Firebase ML:圖片標籤模型 com.google.firebase:firebase-ml-vision-image-label-model 20.0.2
Firebase ML:物件偵測和追蹤模型 com.google.firebase:firebase-ml-vision-object-detection-model 19.0.6
Firebase ML:臉部偵測模型 com.google.firebase:firebase-ml-vision-face-model 20.0.2
Firebase ML:條碼掃描模型 com.google.firebase:firebase-ml-vision-barcode-model 16.1.2
Firebase ML:AutoML Vision Edge API com.google.firebase:firebase-ml-vision-automl 18.0.6
Firebase ML:Natural Language API com.google.firebase:firebase-ml-natural-language 22.0.1
Firebase ML:語言識別模型 com.google.firebase:firebase-ml-natural-language-language-id-model 20.0.8
Firebase ML:翻譯模型 com.google.firebase:firebase-ml-natural-language-translate-model 20.0.9
Firebase ML:智慧回覆模型 com.google.firebase:firebase-ml-natural-language-smart-reply-model 20.0.8

1 Firebase AI Logic 舊稱為「Vertex AI in Firebase」,套件為 com.google.firebase:firebase-vertexai。

2 Firebase SQL Connect 舊稱為「Firebase Data Connect」。



後續步驟

在應用程式中新增 Firebase 服務:

瞭解 Firebase: