KC Blog

【Compose Multiplatform】Project Migration Discussion and Development Guide

3 min read
CrossPlatform#CMP#Kotlin

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

{% include table/compose-multiplatform-category.html %}

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:

<img src="/images/compose/009.png" alt="Cover" style="width: 30%"/ class="prose-img">

Common code is placed in commonMain:

<img src="/images/compose/010.png" alt="Cover" style="width: 30%"/ class="prose-img">

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

{% include table/compose-multiplatform-compare.html %}

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:

    • 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

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