What You'll Build in This Kotlin Multiplatform Tutorial
This Kotlin Multiplatform tutorial for beginners builds Split the Bill: type a bill amount, pick a tip and the number of people, and see what each person pays. The maths, the input parsing, the rupee formatting and the screen itself are written once in Kotlin and run on both Android and iOS. Along the way you will use the three ideas every KMP project is built on: source sets, expect/actual, and shared tests.
Prerequisites
- Basic Kotlin: variables, functions, classes and data classes. New to Compose? Skim Layouts and Jetpack Compose first.
- Android Studio or IntelliJ IDEA with the Kotlin Multiplatform plugin installed.
- To run the iOS app you need a Mac with Xcode. On Windows or Linux you can still build the Android app and run the shared tests.
- Helpful: read topics 1–4 of the Kotlin Multiplatform track.
Step by Step
1 Create the project with the KMP wizard
Open the Kotlin Multiplatform wizard, either on the web at kmp.jetbrains.com or from File > New > New Project > Kotlin Multiplatform in your IDE. Name the project SplitBill, tick Android and iOS, and choose to share UI with Compose Multiplatform. Let the wizard pick the versions: it generates a gradle/libs.versions.toml file with Kotlin, Compose Multiplatform and Android Gradle Plugin versions that are known to work together, which saves you from the most common beginner build errors.
Since 2026, JetBrains' default project structure gives each platform its own app module, because Android Gradle Plugin 9 no longer lets an Android app live inside a multiplatform module:
SplitBill/
├── shared/ // KMP library: shared logic + shared Compose UI
│ └── src/
│ ├── commonMain/kotlin/ // code for every platform
│ ├── commonTest/kotlin/ // tests for every platform
│ ├── androidMain/kotlin/ // Android-only code
│ └── iosMain/kotlin/ // iOS-only code
├── androidApp/ // Android entry point (MainActivity)
└── iosApp/ // Xcode project, SwiftUI entry point
Older tutorials put everything in a single composeApp module. The code below works the same in either layout; only the folder names differ.
2 Use expect/actual for the platform name
Shared code sometimes needs something only the platform knows, like the OS version. You declare it with expect in commonMain and provide an actual in each platform source set. The wizard already generates something like this; open the files and read them:
// shared/src/commonMain/kotlin/Platform.kt
interface Platform {
val name: String
}
expect fun getPlatform(): Platform
// shared/src/androidMain/kotlin/Platform.android.kt
import android.os.Build
class AndroidPlatform : Platform {
override val name: String = "Android ${Build.VERSION.SDK_INT}"
}
actual fun getPlatform(): Platform = AndroidPlatform()
// shared/src/iosMain/kotlin/Platform.ios.kt
import platform.UIKit.UIDevice
class IOSPlatform : Platform {
override val name: String =
UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}
actual fun getPlatform(): Platform = IOSPlatform()
Notice that the iOS file calls Apple's UIDevice API directly from Kotlin. Kotlin/Native exposes iOS frameworks under the platform.* packages, so you only need Swift for the app entry point.
3 Write the shared business logic
Create BillSplitter.kt in shared/src/commonMain/kotlin. Money is stored as whole paise in a Long, never as a Double, so ₹0.10 + ₹0.20 is always exactly ₹0.30 on both platforms.
// shared/src/commonMain/kotlin/BillSplitter.kt
data class SplitResult(
val tipPaise: Long,
val totalPaise: Long,
val perPersonPaise: Long,
val remainderPaise: Long
)
class BillSplitter {
fun split(billPaise: Long, tipPercent: Int, people: Int): SplitResult {
require(billPaise >= 0) { "Bill cannot be negative" }
require(tipPercent in 0..100) { "Tip must be between 0 and 100%" }
require(people >= 1) { "At least one person must pay" }
val tip = (billPaise * tipPercent + 50) / 100 // round to the nearest paisa
val total = billPaise + tip
return SplitResult(
tipPaise = tip,
totalPaise = total,
perPersonPaise = total / people,
remainderPaise = total % people
)
}
}
/** "1,250.5" -> 125050 paise. Returns null for anything that isn't a valid amount. */
fun parseRupeesToPaise(text: String): Long? {
val clean = text.trim().replace(",", "")
if (clean.isEmpty()) return null
val parts = clean.split(".")
if (parts.size > 2) return null
val rupees = parts[0].ifEmpty { "0" }.toLongOrNull() ?: return null
if (rupees < 0) return null
val paiseText = parts.getOrNull(1) ?: ""
if (paiseText.length > 2 || paiseText.any { !it.isDigit() }) return null
return rupees * 100 + paiseText.padEnd(2, '0').toLong()
}
fun formatRupees(paise: Long): String {
val rupees = paise / 100
val rest = (paise % 100).toString().padStart(2, '0')
return "₹$rupees.$rest"
}
Everything here is plain Kotlin from the standard library, so it compiles for Android and iOS without a single actual. That is the sweet spot for KMP: pure logic that would otherwise be written twice, once in Kotlin and once in Swift.
4 Test it once in commonTest
Tests in commonTest use kotlin.test and run on every target, so one test file checks the Android build and the iOS build. Create BillSplitterTest.kt:
// shared/src/commonTest/kotlin/BillSplitterTest.kt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
class BillSplitterTest {
private val splitter = BillSplitter()
@Test
fun splitsEvenlyWithTip() {
val result = splitter.split(billPaise = 100_000L, tipPercent = 10, people = 4)
assertEquals(10_000L, result.tipPaise)
assertEquals(110_000L, result.totalPaise)
assertEquals(27_500L, result.perPersonPaise)
assertEquals(0L, result.remainderPaise)
}
@Test
fun keepsLeftoverPaise() {
val result = splitter.split(billPaise = 1_000L, tipPercent = 0, people = 3)
assertEquals(333L, result.perPersonPaise)
assertEquals(1L, result.remainderPaise)
}
@Test
fun rejectsZeroPeople() {
assertFailsWith<IllegalArgumentException> { splitter.split(1_000L, 10, 0) }
}
@Test
fun parsesAndFormatsRupees() {
assertEquals(125_050L, parseRupeesToPaise("1,250.5"))
assertNull(parseRupeesToPaise("12.345"))
assertNull(parseRupeesToPaise("abc"))
assertEquals("₹1250.50", formatRupees(125_050L))
}
}
Run the tests with the green arrow next to the class name, or from a terminal with ./gradlew :shared:allTests. iOS test tasks only run on a Mac; elsewhere Gradle skips them. Exact task names depend on the targets the wizard set up, so the IDE's run arrow is the easiest option.
5 Build the shared Compose UI
Replace the wizard's sample App.kt in shared/src/commonMain/kotlin with this screen. It is ordinary Jetpack Compose code; Compose Multiplatform renders the same function on Android and iOS. The wizard already adds the Material 3 dependency.
// shared/src/commonMain/kotlin/App.kt
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
@Composable
fun App() {
MaterialTheme {
var billText by remember { mutableStateOf("") }
var tipPercent by remember { mutableStateOf(10f) }
var people by remember { mutableStateOf(2) }
val splitter = remember { BillSplitter() }
val billPaise = parseRupeesToPaise(billText)
val result = billPaise?.let { splitter.split(it, tipPercent.toInt(), people) }
Column(
modifier = Modifier.fillMaxSize().safeContentPadding().padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Text("Split the Bill", style = MaterialTheme.typography.headlineMedium)
Text("Running on ${getPlatform().name}", style = MaterialTheme.typography.bodySmall)
OutlinedTextField(
value = billText,
onValueChange = { billText = it },
label = { Text("Bill amount (₹)") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
isError = billText.isNotBlank() && billPaise == null,
singleLine = true,
modifier = Modifier.fillMaxWidth()
)
Text("Tip: ${tipPercent.toInt()}%")
Slider(
value = tipPercent,
onValueChange = { tipPercent = it },
valueRange = 0f..30f,
steps = 29 // stops at every whole percent
)
Row(verticalAlignment = Alignment.CenterVertically) {
Text("People: $people", modifier = Modifier.weight(1f))
OutlinedButton(onClick = { if (people > 1) people-- }) { Text("-") }
Spacer(Modifier.width(8.dp))
OutlinedButton(onClick = { people++ }) { Text("+") }
}
if (result != null) {
Card(modifier = Modifier.fillMaxWidth()) {
Column(Modifier.padding(16.dp)) {
Text("Tip: ${formatRupees(result.tipPaise)}")
Text("Total: ${formatRupees(result.totalPaise)}")
Text(
"Each person pays ${formatRupees(result.perPersonPaise)}",
style = MaterialTheme.typography.titleLarge
)
if (result.remainderPaise > 0) {
Text("${result.remainderPaise} paise left over: one person rounds up.")
}
}
}
}
}
}
}
State lives in remember { mutableStateOf(...) } exactly as on Android. The screen calls the shared BillSplitter and the expect function getPlatform(), so the "Running on" line is the only thing that differs between phones.
6 Run it on Android
The Android app module only needs to show App(). The wizard's MainActivity looks like this:
// androidApp/src/main/kotlin/MainActivity.kt
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
setContent { App() }
}
}
Pick the androidApp run configuration and press Run. Type 1250, leave the tip at 10% and set 4 people: each person pays ₹343.75.
7 Run it on iOS
On iOS, Compose UI is wrapped in a UIViewController in iosMain, and a few lines of SwiftUI host it. Again, the wizard generates both:
// shared/src/iosMain/kotlin/MainViewController.kt
import androidx.compose.ui.window.ComposeUIViewController
fun MainViewController() = ComposeUIViewController { App() }
// iosApp/iosApp/ContentView.swift
import SwiftUI
import Shared // your framework's name (baseName in shared/build.gradle.kts)
struct ComposeView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
struct ContentView: View {
var body: some View {
ComposeView().ignoresSafeArea(.keyboard)
}
}
Choose the iosApp run configuration in your IDE (or open iosApp in Xcode) and run it on a simulator. You should see the same screen, with "Running on iOS" and your simulator's version. The Kotlin function MainViewController() appears in Swift as MainViewControllerKt.MainViewController() because top-level Kotlin functions are grouped under a class named after their file.
Practice Task
Extend the app on your own, then check yourself with the quiz below.
- Add a "Round up to the nearest ₹10" option to
BillSplitter, with a new test incommonTestbefore you touch the UI. - Add an
expect fun currentTimeLabel(): Stringand print when the bill was split, usingjava.timeon Android andNSDateon iOS. (Hint: thekotlinx-datetimelibrary can do this in common code instead; compare the two approaches.) - Keep the Compose screen on Android but build the iOS screen in SwiftUI, calling
BillSplitter().split(...)directly from Swift. This is the "share logic, keep native UI" approach many teams start with.
Kotlin Multiplatform Quiz
Five quick questions on this tutorial.
Frequently Asked Questions
Can I learn Kotlin Multiplatform without a Mac?
Yes for the shared code and the Android app: you can write commonMain code, run the shared tests and build for Android on Windows or Linux. Building and running the iOS app needs a Mac with Xcode.
Do I have to share the UI with Compose Multiplatform?
No. You can share only the logic (like BillSplitter) and keep native UI on each platform: Jetpack Compose on Android and SwiftUI on iOS. Many teams start that way and add shared UI later.
Is Kotlin Multiplatform good for beginners?
It is a good next step once you know basic Kotlin. The shared code is ordinary Kotlin, and the wizard sets up the build, so the new ideas are mainly source sets, expect/actual and how each platform's app starts.
What is the difference between Kotlin Multiplatform and Compose Multiplatform?
Kotlin Multiplatform is the technology for sharing Kotlin code across platforms. Compose Multiplatform is JetBrains' UI framework on top of it that lets you share Compose screens as well.