遷移到在主模組中使用 Kotlin 擴充 (KTX) API

Firebase 正在加大對 Kotlin 的投入,我們正在努力實現 Android 生態系統的現代化,讓 Firebase 更容易使用 Kotlin。

為了實現這一現代化,我們對適用於 Android 的 Firebase SDK 進行了一些更改。本頁描述了有關此更改的重要信息,包括:

有什麼變化?

Kotlin 擴充 (KTX) API 已新增至各自的主模組。例如, firebase-perf-ktx中的所有 API 均已新增至com.google.firebase.perf套件下的firebase-perf

此變更表示 Kotlin 開發人員現在可以依賴主模組而不是 KTX 模組(當使用Firebase BoM v32.4.0+或 BoM v32.4.0+ 中列出的主模組版本時)。

作為此變更的一部分,每個 KTX 模組中的 Kotlin 擴充功能 (KTX) API 現已棄用。最早在 2024 年 4 月,我們將不再發布 KTX 模組,屆時我們也將從 Firebase Android BoM 中刪除 KTX 庫。

我們為什麼要做出這樣的改變?

Firebase 致力於為 Android 開發者打造Kotlin 優先的生態系統。這種包裝現代化具有以下優點:

  • 簡化的依賴管理:您現在只需要依賴一個模組,而無需在主模組和 Kotlin 擴充之間切換或依賴兩者。

  • 增強的 Kotlin 支援:我們所有適用於 Android 的 Firebase SDK 現在都將為 Kotlin 提供更好的支援。我們將直接在我們的主模組中包含所有新的 Kotlin 友善功能。

此更改的重要日期

了解此變更的里程碑。

2023年10月

Kotlin 擴充功能 (KTX) API 已新增至各自的主模組中,且每個 KTX 模組中的 Kotlin 擴充功能 (KTX) API 均已棄用。請參閱宣布此變更的發行說明

現在,當使用Firebase BoM v32.4.0+或 BoM v32.4.0+ 中列出的主模組版本時,您可以直接從主模組使用 KTX API。

在此棄用階段,KTX 模組中已棄用的 API 將繼續運作並維護。

最快將於 2024 年 4 月

我們將停止發布新版本的 KTX 模組,並將從 Firebase BoM 中刪除 KTX 模組。

任何先前發布的 KTX 模組或 BoM 版本都將繼續運行,但它們將進入維護結束狀態。這意味著我們將停止在 KTX 模組中添加錯誤修復、向後相容的變更和新功能。相反,Android 上 Firebase 的所有未來開發都將在主模組中完成(對於 Java 和 Kotlin)。

如何從主模組遷移到使用 KTX API

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

  1. 修改 Gradle 依賴項以依賴主模組而不是 KTX 模組。例如,如果您使用 Firebase Android BoM (建議)

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

    dependencies {
      // ...
    
      // Import the Firebase BoM as usual
      // Make sure to use Firebase BoM v32.4.0 or higher
      implementation(platform("com.google.firebase:firebase-bom:32.4.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。

    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
    

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