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 32.5.0 以上版本或 BoM 32.5.0 以上版本中列出的模組版本時)。
在本次異動中,每個 KTX 模組中的 Kotlin 擴充功能 (KTX) API 現已淘汰。最快在 2024 年 9 月,我們就會停止發布 KTX 模組,並在該時從 Firebase Android BoM 中移除 KTX 程式庫。
我們進行這項異動的原因為何?
Firebase 致力於為 Android 開發人員提供 Kotlin 優先生態系統。這種包裝現代化方式具有下列優點:
簡化依附元件管理:現在您只需依附單一模組,無須在主要模組和 Kotlin 擴充功能之間切換,或同時依附這兩者。
強化 Kotlin 支援功能:所有 Android 版 Firebase SDK 現已提供更完善的 Kotlin 支援功能。我們會直接在主要模組中加入所有新的 Kotlin 友善功能。
這項異動的重要日期
2023 年 10 月
Kotlin 擴充功能 (KTX) API 已新增至各自的主模組,也就是說,您現在可以使用 Firebase BoM 32.5.0 以上版本或 BoM 32.5.0 以上版本中列出的主模組版本,直接從主模組使用 KTX API。
與此同時,KTX 模組中的 Kotlin 擴充功能 (KTX) API 已淘汰 (請參閱說明此變更的版本說明)。在已淘汰階段,KTX 模組中的已淘汰 API 將繼續運作並接受維護。
最快於 2024 年 9 月
我們將停止發布新版 KTX 模組,並從 Firebase BoM 中移除 KTX 模組。
任何先前發布的 KTX 模組或 BoM 版本都會繼續運作,但會進入維護結束狀態。也就是說,我們將停止在 KTX 模組中新增錯誤修正、回溯相容性變更和新功能。未來所有 Android 版 Firebase 的開發作業都會在主要模組中完成 (適用於 Java 和 Kotlin)。
如何從主要模組遷移至使用 KTX API
如果您使用 Kotlin 擴充功能 (KTX) API,請在應用程式中進行以下更新,開始使用主要模組中的 API,而非 KTX 模組。
修訂 Gradle 依附元件,以便依賴主要模組,而非 KTX 模組。舉例來說,如果您使用 Firebase Android BoM (建議):
BEFORE
dependencies { // ... // Import the Firebase BoM implementation(platform("com.google.firebase:firebase-bom:33.10.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:33.10.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") }
如果您未使用 Firebase Android BoM
BEFORE
dependencies { // ... // Using KTX libraries for Authentication and Cloud Firestore implementation("com.google.firebase:firebase-auth-ktx:23.2.0") implementation("com.google.firebase:firebase-firestore-ktx:25.1.2") }
AFTER
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.5.0 or higher implementation("com.google.firebase:firebase-auth:23.2.0") implementation("com.google.firebase:firebase-firestore:25.1.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