改用在主模組中使用 Kotlin 擴充功能 (KTX) API

Firebase 致力於支援 Kotlin,並積極更新 Android 生態系統,讓 Kotlin 更容易使用,並與 Firebase 完美搭配。

為完成這項現代化作業,我們將對 Android 適用的 Firebase SDK 進行幾項變更。本頁面說明這項異動的重要資訊,包括:


瞭解如何遷移應用程式

異動內容

Kotlin 擴充功能 (KTX) API 已新增至各自的主要模組。舉例來說,firebase-perf-ktx 中的所有 API 都已新增至 com.google.firebase.perf 套件下的 firebase-perf

這項異動實施後,Kotlin 開發人員現在可以依附於主要模組,而非 KTX 模組 (使用 Firebase BoM v32.5.0 以上版本BoM v32.5.0 以上版本中列出的主要模組版本時)。

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

我們進行這項異動的原因為何?

Firebase 致力於為 Android 開發人員提供以 Kotlin 為優先的生態系統。包裝現代化可帶來下列優點:

  • 簡化依附元件管理:您現在只需要依附於單一模組,不必在主要模組和 Kotlin 擴充功能之間切換,也不必同時依附於兩者。

  • 強化 Kotlin 支援:所有 Android 版 Firebase SDK 現在都能更完善地支援 Kotlin。我們會在主要模組中直接加入所有新的 Kotlin 友善功能。

這項異動的重要日期

2023 年 10 月

2023 年 10 月,Kotlin 擴充功能 (KTX) API 已新增至各自的主要模組,因此使用 Firebase BoM v32.5.0 以上版本BoM v32.5.0 以上版本中列出的主要模組版本時,現在可以直接從主要模組使用 KTX API。

同時,Kotlin 擴充功能 (KTX) API 在 KTX 模組中已遭淘汰 (請參閱版本說明,瞭解這項變更)。在淘汰階段,KTX 模組中已淘汰的 API 仍可運作並維持。

2025 年 7 月

我們已於 2025 年 7 月停止發布 KTX 模組的新版本,並從 Firebase BoM 中移除 KTX 模組 (從 BoM v34.0.0 開始)。

先前發布的任何 KTX 模組或 BoM 版本都會繼續運作,但現在已停止維護。也就是說,我們不會在 KTX 模組中新增錯誤修正、回溯相容性變更或新功能。今後,Android 版 Firebase 的所有開發作業都只會在主要模組中進行 (適用於 Java 和 Kotlin)。

如何從主要模組遷移至 KTX API

如果您使用 Kotlin 擴充功能 (KTX) API,請在應用程式中進行下列更新,開始使用主要模組的 API,而非 KTX 模組。

  1. 修訂 Gradle 依附元件,改為依附主要模組,而非 KTX 模組。舉例來說,如果您使用 Firebase Android BoM (建議)

    BEFORE

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation(platform("com.google.firebase:firebase-bom:34.0.0"))
    
      // Using KTX libraries for Authentication and Cloud Firestore
      implementation("com.google.firebase:firebase-auth-ktx")
      implementation("com.google.firebase:firebase-firestore-ktx")
    }

    AFTER

    dependencies {
      // ...
    
      // Import the Firebase BoM as usual
      // Make sure to use Firebase BoM v32.5.0 or higher
      implementation(platform("com.google.firebase:firebase-bom:34.0.0"))
    
      // No need to use the KTX libraries; everything is now in the main module
      implementation("com.google.firebase:firebase-auth")
      implementation("com.google.firebase:firebase-firestore")
    }


  2. 更新程式碼,將所有 KTX API 替換為 com.google.firebase 套件下主要模組中的重新定位 API。

    BEFORE

    import com.google.firebase.auth.ktx.auth
    import com.google.firebase.firestore.ktx.firestore
    import com.google.firebase.firestore.ktx.toObject
    import com.google.firebase.ktx.Firebase

    AFTER

    import com.google.firebase.auth.auth
    import com.google.firebase.firestore.firestore
    import com.google.firebase.firestore.toObject
    import com.google.firebase.Firebase