【Compose Multiplatform】Project Migration Discussion and Development Guide
Compose Multiplatform (CMP) provides developers with a powerful cross-platform development tool
But migrating from a Compose project to CMP also faces some challenges
This article will detail key points and considerations in the migration process
目錄
- 【Compose Multiplatform】專案轉移探討與開發指南
- 【Compose Multiplatform】手機使用依賴注入Koin
- 【Compose Multiplatform 】跨平台App但Android需要context作法並搭配Koin
- 【Compose Multiplatform】手機本地持久化儲存DataStore實作
- 【Compose Multiplatform】手機資料庫SqlDelight實作
- 【Compose Multiplatform】CMP中使用ROOM開發資料庫 - [KSP2] Annotation value is missing in nested annotations
- 【Compose Multiplatform】CMP專案中導入CocoaPods及無CocoaPods情況下使用IOS Swift/Obj-C
Initially, you need to understand how CMP achieves cross-platform functionality
So it takes some time to understand
the project structure
Let’s quickly go through it below
When developing with CMP, you need to be familiar with the structure of multiple folders:
Common code is placed in commonMain:
Import the required libraries in each environment:
Since it uses lib.version.toml for configuration by default
you need to understand .toml
but it’s actually very easy
the official defaults work fine
unless you need special configurations
Here are some notes I wrote before
For reference
- Assuming we originally used some common libs or officially recommended ones for Android projects (as shown on the left of the table)
After trying to write with CMP, we’ll find some benefits in the migration cost of the libraries we use (as shown on the right of the table)
Because most of them are things you’ve used when writing Compose
Feature | Compose Project | CMP Project |
---|---|---|
API | okhttp + retrofit2 | Ktor + Ktorfit |
navigation | navigation-compose | navigation-compose |
design | material 3 | material 3 |
DI | hilt | koin |
UI | jetpack compose | jetpack compose |
JSON | kotlinx.serialization | kotlinx.serialization |
Database | room | SqlDelight |
Image | coil | coil |
Push | FCM, UM, HW | 可能需自行實作各平台 |
Splash | splashscreen | 可能需自行實作或用 Compose 實現 |
- Cross-platform requirement differences:
For example, Android needs Context, iOS doesn’t:
Complete notes: 【Compose Multiplatform】Cross-Platform App with Android Context Implementation Using Koin -
Platform-specific implementations:
For example, mobile often uses local persistent storage
In Android, we use DataStore to handle this issue
So how do we use it across multiple platforms?
Using expect and actual keywords:
Despite needing separate implementations
some common libraries
are supported by CMP with Kotlin implementations
so even with separate platform implementations, you can still write in pure .kt
just like the DataStore implemented in iosMain aboveComplete notes: 【Compose Multiplatform】Implementing Local Persistent Storage with DataStore
- CMP library compatibility issues or bugs being continuously fixed:
For example, SQLDelight 2.0.0 version has build errors on iOS:
- Solution 1: Import stately-common
- Solution 2: Upgrade to version 2.0.1 or above
The reason can be found in this discussion thread: Click here
Complete SQLDelight notes: 【Compose Multiplatform】Implementing SqlDelight Database
- Solution 1: Import stately-common
Google mentioned support for KMP in their blog on May 14, 2024:
This might mean more libraries will be supported in the future.
- CMP provides powerful cross-platform development capabilities, but requires adaptation to the new project structure
- Most common libraries have corresponding CMP versions
For example, those commonly used in Compose App development can be used directly
DataStore, Room, etc. - When handling platform differences, using expect and actual keywords is very helpful
-
Pay attention to library version compatibility issues
Currently in development
I’ve encountered several configuration compatibility issues
Such as: Room compatibility issues with Kotlin 2.0.0
Errors with embedAndSign when configuring CocoaPods in CMP - Keep an eye on Google’s latest updates for more support and resources
I’ve tried asking GPT directly
but it may not be that accurate
Many compatibility issues still require researching on your own
or perhaps when more data becomes available in the future, it might provide more precise answers