SkillByAIOpen interactive version →

Lesson 22 / 25

Kotlin on the Server and Android

Ktor, Spring Boot and Jetpack Compose at a glance.

Where Kotlin runs in production

On the server, Ktor is a JetBrains framework built around coroutines, with routing, plugins and serialization via kotlinx.serialization. Spring Boot has first-class Kotlin support, including coroutine-based controllers and a Kotlin bean DSL; many teams use it with existing Java ecosystems. On Android, Kotlin is the recommended language, and Jetpack Compose builds UIs with @Composable functions that describe UI from state; when state changes, Compose recomposes the affected parts. Android Jetpack libraries provide Kotlin-friendly APIs such as viewModelScope and Flow integration. APIs evolve quickly, so check the docs for your version.

From code to shipped software

Kotlin powers servers, Android apps and shared multiplatform code, backed by testing and style tools.

Figure 8.1 — Shared Kotlin code feeding server, Android, iOS and web.

A Ktor route and a Compose screen

Two sketches; dependencies and imports trimmed.

// Server: Ktor routing (inside an Application module)
fun Application.module() {
    routing {
        get("/health") {
            call.respondText("ok")
        }
        get("/users/{id}") {
            val id = call.parameters["id"]?.toLongOrNull()
                ?: return@get call.respond(HttpStatusCode.BadRequest)
            call.respond(userService.find(id))   // suspend call, serialized as JSON
        }
    }
}

// Android: Jetpack Compose
@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }
    Button(onClick = { count++ }) {
        Text("Clicked $count times")
    }
}

One language, several workshops

Learning Kotlin is like learning one set of tools that fits several workshops: the server room, the mobile studio and the shared parts bench all use the same wrenches.

Quick check: What is Jetpack Compose?

  • A build tool replacing Gradle
  • A server framework from JetBrains
  • A Kotlin linter
  • Android's declarative UI toolkit built on @Composable functions
Answer

Android's declarative UI toolkit built on @Composable functions — Compose describes UI as a function of state.