01
Dev.to Kotlin
作者:Thomas Künneth
發布:03-08 11:36
Refuelling your Jetpack
Use the Jetpack libraries you know—Compose, ViewModel, Navigation, Room, DataStore—inside Kotlin Multiplatform to run one architecture on Android, iOS and Desktop.
重點摘要
Jetpack evolved from Android Architecture Components into 130+ artifacts—choose the Golden Path to avoid the jungle
Compose Multiplatform, ViewModel, Navigation, Room and DataStore enable one shared architecture across platforms
Structure apps with a shared module and thin native shells, use expect/actual and a Kotlin DI (Koin) for multiplatform
Jetpack reshaped Android development by unbundling libraries, providing architectural guidance, and reducing fragmentation—but its growth produced a 'Jetpack Jungle' of 130+ artifacts. The article proposes a Golden Path: adopt Compose Multiplatform for reactive UI, ViewModel for state, Navigation Compose for a shared navigation graph, Room for a type-safe SQLite layer, and DataStore for asynchronous preferences. In Kotlin Multiplatform this stack runs on Android, iOS and Desktop from a single shared module that contains UI, navigation, viewmodels and database, while thin platform shells initialize and host the shared UI. Use expect/actual to isolate OS specifics and prefer a pure-Kotlin DI (Koin) because Hilt is Android-centric. The CMP Unit Converter demonstrates AGP 9 layout, shared resources, version-alignment pitfalls, and practical iOS quirks (e.g., doInitKoin). Follow compatibility tables, keep versions aligned, and rely on docs and samples to move Jetpack beyond Android.
为你的 Jetpack 补充动力
在 Kotlin Multiplatform 中使用 Compose、ViewModel、Navigation、Room 与 DataStore,构建一套可编译到 Android、iOS 与桌面的统一架构。
重點摘要
Jetpack 从解决碎片化走向 130+ 组件的“丛林”,需遵循 Google 推荐的黄金路径
Compose Multiplatform、ViewModel、Navigation、Room、DataStore 可共享同一代码与 API
以 shared 模块 + 薄平台壳体、expect/actual 与 Koin 实现可测试的跨平台应用
作者回顾 Jetpack 的发展:从把特性从系统中解耦并提供一致架构,到如今形成 130+ 个工件的“Jetpack 丛林”。文章提出一个黄金路径:采用 Compose Multiplatform 做响应式 UI,ViewModel 管理状态,Navigation Compose 统一路由,Room 提供类型安全的 SQLite 层,DataStore 处理异步偏好存储。在 Kotlin Multiplatform 下,这些组件可放在一个 shared 模块中,平台端仅承担初始化与宿主角色;通过 expect/actual 隐藏平台差异,并推荐使用纯 Kotlin 的依赖注入(如 Koin),因为 Hilt 目前仅限 Android。通过 CMP Unit Converter 示例说明 AGP9 结构、资源共用、版本对齐与 iOS 的命名细节(如 doInitKoin)。结论是:采用该栈可有效将 Jetpack 的能力带向多平台。
為你的 Jetpack 補充動力
在 Kotlin Multiplatform 中以 Compose、ViewModel、Navigation、Room 與 DataStore 建立同一架構,編譯並運行於 Android、iOS 與桌面。
重點摘要
Jetpack 從解耦與標準化發展成 130+ 套件,需選擇被推薦的黃金路徑
Compose Multiplatform、ViewModel、Navigation、Room、DataStore 可共享相同 API 與架構
一個 shared 模組配合薄殼平台、expect/actual 與 Koin,可實現可測試的跨平台應用
作者回顧 Jetpack 的演進:從解耦 Android 系統到建立一致架構,但套件數量膨脹成「Jetpack 叢林」。提出一條黃金路徑:Compose Multiplatform、ViewModel、Navigation、Room 與 DataStore,在 Kotlin Multiplatform 下可共享同一架構並編譯到 Android、iOS 與桌面。實作上以一個 shared 模組放置 UI、導航、ViewModel 與資料庫,平台端僅為薄殼,透過 expect/actual 隱藏平台差異;以 Koin 取代 Hilt 以確保多平台相容。以 CMP Unit Converter 範例展示流程、AGP9 結構、資源共用與注意版本對齊、測試與 iOS 命名細節。結論:投入此堆疊可跟上生態趨勢,實現同一架構跨平台運行。