Kotlin Tutorial · Kotlin Projects · Lesson 30 of 31
Kotlin with Gradle, Android and Multiplatform
How real Kotlin projects are organised: Gradle builds, Android apps, server frameworks and Kotlin Multiplatform.
Gradle
A typical JVM project:
// build.gradle.kts
plugins {
kotlin("jvm") version "2.4.21"
application
}
repositories { mavenCentral() }
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
testImplementation(kotlin("test"))
}
kotlin { jvmToolchain(21) }
application { mainClass = "AppKt" }
Keep versions in a version catalog (gradle/libs.versions.toml) once a project has several modules.
Where Kotlin runs
- Android: Kotlin with Jetpack Compose for UI is the default for new apps (see the Android course).
- Servers: Spring Boot has first-class Kotlin support; Ktor is JetBrains' own coroutine-based framework.
- Kotlin Multiplatform (KMP): share business logic between Android, iOS, desktop and web in a
commonMainsource set, with platform code inandroidMain,iosMainand so on. Compose Multiplatform shares UI too.
// commonMain: shared by every platform
expect fun platformName(): String
fun greeting() = "Hello from ${platformName()}"
// androidMain
actual fun platformName() = "Android"
// iosMain
actual fun platformName() = "iOS"
The shared code is ordinary Kotlin:
// Shared business logic: no platform APIs, so it can live in commonMain
data class CartItem(val name: String, val price: Double, val quantity: Int)
fun cartTotal(items: List<CartItem>, couponPercent: Int = 0): Double {
val subtotal = items.sumOf { it.price * it.quantity }
return subtotal * (100 - couponPercent) / 100
}
fun main() {
val items = listOf(CartItem("mug", 12.0, 2), CartItem("tea", 6.5, 1))
println("%.2f".format(cartTotal(items)))
println("%.2f".format(cartTotal(items, couponPercent = 10)))
}
Output
30.50 27.45
Run this on your computer — see “Get Started” for setup.
Test yourself
In Kotlin Multiplatform, where does code shared by all platforms go?
androidMainThe commonMain source setA Java modulebuild.gradle.kts
Show answer
Answer: The commonMain source set