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。
修改 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") }
前
dependencies { // ... // Using KTX libraries for Authentication and Cloud Firestore implementation("com.google.firebase:firebase-auth-ktx:22.2.0") implementation("com.google.firebase:firebase-firestore-ktx:24.9.0") }
後
dependencies { // ... // No need to use the KTX libraries, everything is now in the main module // Make sure to use a version listed in Firebase BoM v32.4.0 or higher implementation("com.google.firebase:firebase-auth:22.2.0") implementation("com.google.firebase:firebase-firestore:24.9.0") }
更新您的程式碼,將所有出現的 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