Dan Tech Academy

Compose Multiplatform: một UI cho Android và iOS, cái gì dùng chung được, cái gì không

Phần nào của app Compose Multiplatform viết một lần cho Android và iOS, phần nào vẫn phải viết riêng: điểm khởi động, cấu hình, API hệ điều hành và view native.

Compose Multiplatform là gì

Compose Multiplatform là bản đa nền tảng của Jetpack Compose, do JetBrains phát triển. Bạn viết composable trong commonMain, và cùng code đó chạy trên Android lẫn iOS.

JetBrains công bố Compose Multiplatform cho iOS đạt Stable ở bản 1.8.0, ngày 6/5/2025. Lúc viết bài, bản stable mới nhất là 1.12.2. Code trong bài mình build trên Compose Multiplatform 1.11.1 với Kotlin 2.4.10.

Câu hỏi thực tế khi bắt đầu là: phần nào viết một lần cho cả hai nền tảng, phần nào vẫn phải viết riêng.

Phần dùng chung được

Một màn hình viết trong commonMain:

// commonMain
package com.example.cmp

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue

@Composable
fun ScoreBoard(score: Int, onAdd: () -> Unit) {
    Column {
        Text("Score: $score")
        Button(onClick = onAdd) {
            Text("+1")
        }
    }
}

@Composable
fun App() {
    MaterialTheme {
        var score by remember { mutableIntStateOf(0) }
        ScoreBoard(score = score, onAdd = { score++ })
    }
}

Đây là Compose bình thường. remember, state, Column, Material 3 đều dùng được trong code chung. Theo bảng so sánh trong tài liệu Kotlin, những thư viện sau cũng có bản dùng được từ code chung:

  • Lifecycle và ViewModel của Jetpack.
  • Navigation Compose.
  • Resources: chuỗi, ảnh, font đặt trong composeResources và truy cập qua class Res, thay cho class R của Android.

Khi build cho Android, Gradle tự dùng artifact Jetpack Compose do Google phát hành. Ví dụ compose.material3 sẽ thành androidx.compose.material3:material3 trên Android và org.jetbrains.compose.material3:material3 trên các target khác. App Android của bạn vẫn chạy Jetpack Compose thật.

Phần vẫn phải viết riêng

Điểm khởi động của app. Android cần một Activity:

// androidMain
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { App() }
    }
}

iOS cần một UIViewController bọc Compose, rồi app Swift hiển thị controller đó:

// iosMain/kotlin/com/example/cmp/MainViewController.kt
fun MainViewController() = ComposeUIViewController { App() }
// iosApp, SwiftUI
import SwiftUI
import Shared

struct ComposeView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        MainViewControllerKt.MainViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

Shared là tên framework mà module shared export ra, đặt trong baseName của Gradle. Tên class MainViewControllerKt đi theo tên file Kotlin.

Cấu hình app. AndroidManifest.xml, Info.plist, quyền truy cập camera hay vị trí, icon, splash screen vẫn nằm trong project của từng nền tảng. Riêng iOS có một key dễ quên: tài liệu Kotlin yêu cầu thêm CADisableMinimumFrameDurationOnPhone vào Info.plist để bật tần số quét cao, thiếu key này app sẽ crash lúc chạy.

API của hệ điều hành. Clipboard, camera, thông báo đẩy, đăng nhập bằng Apple hay Google... Bạn gọi chúng qua expect/actual hoặc qua một interface có implementation riêng cho từng nền tảng.

View native. Một số thứ chỉ có dưới dạng view native. Trên iOS, UIKitView nhúng một UIView vào giữa cây Compose. Ví dụ trong tài liệu Kotlin nhúng bản đồ của Apple:

// iosMain
UIKitView(
    factory = { MKMapView() },
    modifier = Modifier.size(300.dp),
)

Chiều ngược lại cũng được: nhúng màn hình Compose vào app SwiftUI hoặc UIKit có sẵn. Cách này cho phép đưa Compose vào từng màn hình một, không cần viết lại cả app.

Một chỗ cần kiểm tra sớm là thư viện bên thứ ba. Ví dụ bảng của tài liệu Kotlin ghi Maps Compose của Google chỉ có cho Jetpack Compose, chưa có cho Compose Multiplatform. Trước khi chọn thư viện, tra trên klibs.io xem nó có hỗ trợ iOS không.

Giao diện trên iOS trông thế nào

Compose Multiplatform tự vẽ giao diện bằng Skia, không đổi composable thành UIKit view. Nút Material 3 trên iOS vẫn là nút Material 3. Nếu bạn muốn app trông giống iOS, đó là việc của design system bạn tự xây, không phải thứ framework làm hộ.

Theo bài công bố bản 1.8.0, JetBrains đã làm cho phần hành vi khớp với iOS: cuộn theo vật lý của iOS, chọn và sửa text kiểu native, cử chỉ back, hỗ trợ VoiceOver và cỡ chữ hệ thống.

Chia code thế nào cho gọn

Một cách chia hay dùng:

  • commonMain: màn hình, ViewModel, state, logic, resources.
  • androidMain và iosMain: điểm khởi động, implementation của những interface cần API hệ điều hành, và vài view native nếu có.
  • androidApp và iosApp: project của từng nền tảng, chứa cấu hình app.

Phần riêng của mỗi nền tảng càng mỏng thì càng ít chỗ hai bản app chạy khác nhau.

Học tiếp

Khóa Kotlin Mobile 101 dạy nền tảng Kotlin và Compose Multiplatform trên một app hoàn chỉnh chạy được trên máy thật.

Đọc blog thì vui. Đi theo lộ trình thì đến đích.

Kotlin Android Roadmap sắp xếp các bài Android Mastery, OOP và Design Patterns thành 5 cấp độ - từ dòng Kotlin đầu tiên đến app lên store.

Kèm bài viết mới mỗi tuần. Huỷ bất cứ lúc nào.