CodeBlanks Logo Icon CodeBlanks

Compose MP

Learn the Compose Multiplatform concept.

What is Compose MP?

Compose Multiplatform is a library that lets you write declarative UI with Kotlin. The same code runs on Desktop, Web, Android and iOS.

Compose describes the UI with data-bound functions rather than imperative commands. A @Composable function redraws (recomposition) as its state changes. This builds a direct bridge between UI and logic. The composeMpInfo function in this lesson shows the concept with a simple model.

Compose MP targets

Compose MP supports many targets: Desktop (JVM), Web (Canvas or HTML), Android and iOS.

Each target has its own entry point: Window on Desktop, CanvasBasedWindow on Web, setContent on Android, ComposeUIViewController on iOS. Shared @Composable functions are reused on every target. This means a single UI code base with platform-specific entry points.

Practice Write composeMpInfo

Define fun composeMpInfo(): String. It should return "Compose MP". In main, write println(composeMpInfo()).

Starter code

Main.kt
// add the composeMpInfo function

fun main() {
    // print the Compose MP info
}

Sample solution

Main.kt
fun composeMpInfo(): String = "Compose MP"
fun main() {
    println(composeMpInfo())
}

Expected output

Compose MP

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which paradigm does Compose MP use?

Main.kt
Compose describes UI with declarations.
  • Declarative Correct answer
  • Imperative
  • Pure functional
  • Procedural

Explanation: Compose builds UI with the declarative paradigm.

2 Complete the import line

Question
import androidx.compose.____
Answer
import androidx.compose.runtime

Explanation: The runtime module holds Compose state and remember.

3 Match the Compose MP parts

Term Meaning
Desktop JVM target
Web Canvas/HTML
Android Android target
iOS iOS target

All lessons in Expert