【Compose Multiplatform】Project Migration Discussion and Development Guide

Introduction

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

Initial Migration Costs

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:
Cover

Common code is placed in commonMain:
Cover

Import the required libraries in each environment:
Cover

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

Reference for Library Migration from Compose Project to CMP Project


  • 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 實現
Potential Issues
  1. 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
  2. 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 above

    Complete notes: 【Compose Multiplatform】Implementing Local Persistent Storage with DataStore

  3. CMP library compatibility issues or bugs being continuously fixed:
    For example, SQLDelight 2.0.0 version has build errors on iOS:
Future Outlook

Google mentioned support for KMP in their blog on May 14, 2024:
Cover
This might mean more libraries will be supported in the future.

Conclusion
  • 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

You might also enjoy