Phase 7 · Professional & AppliedModule 38~40 min read

Kotlin for Android

Why Kotlin owns Android — project structure, lifecycle, and Jetpack Compose.

What you'll learn

Kotlin's rise is inseparable from Android. Google made it the preferred language for Android in 2017 and "Kotlin-first" in 2019, and today the modern Android toolkit — Jetpack Compose, coroutines, Flow — is built around it. This module is a conceptual map of how everything you've learned powers a real app.

By the end you'll be able to:

  • Explain why Kotlin is Android's language
  • Recognise the parts of an Android project
  • Understand the activity lifecycle at a high level
  • Read Jetpack Compose UI and see coroutines/Flow in action

Note

Building and running Android apps requires Android Studio and an emulator or device, so the snippets below are for understanding, not for the playground. Everything else you've learned — classes, null safety, lambdas, coroutines — transfers directly.

Why Kotlin owns Android

Android was historically Java, which meant verbose code and endless null-pointer crashes. Kotlin fixed exactly those pains: null safety removes a whole class of bugs, concise syntax and data classes cut boilerplate dramatically, coroutines tame asynchronous work, and extension functions make APIs pleasant. Full Java interop meant teams could migrate gradually — and most did.

Project structure

An Android project layers a few familiar and a few new pieces:

  • Kotlin source — your activities, view models, and logic
  • res/ — resources: layouts, images, strings, and themes
  • AndroidManifest.xml — declares the app's screens and permissions
  • build.gradle.kts — the Gradle build you met last module, with Android plugins

Activities & lifecycle

An Activity is a single screen. Android drives it through a lifecycle — onCreate, onStart, onResume, onPause, onStop, onDestroy — as the user navigates and the system manages memory. You override the callbacks you care about; onCreate is where a screen is set up:

MainActivity.kt
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {                 // hand the screen over to Compose
            Counter()
        }
    }
}

Jetpack Compose

Jetpack Compose is Android's modern, declarative UI toolkit — you describe the UI as a function of state, and it re-renders ("recomposes") when the state changes. A @Composable function is a piece of UI. Notice how it leans on Kotlin's trailing lambdas and property delegation:

Counter.kt
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*

@Composable
fun Counter() {
    // state that survives recomposition; changing it re-renders the UI
    var count by remember { mutableStateOf(0) }
    Column {
        Text("Count: $count")
        Button(onClick = { count++ }) {
            Text("Increment")
        }
    }
}

Key idea

Compose is Kotlin showing off: @Composable functions, trailing-lambda DSL syntax, by remember delegation, and state-driven rendering all build directly on features from earlier phases. The DSL feel you learned in Module 28 is exactly how Compose reads.

Coroutines & Flows in Android

Android forbids blocking the main (UI) thread — do so and the app freezes. Coroutines are the answer: a ViewModel holds UI state as a StateFlow and runs suspend work in viewModelScope, which is automatically cancelled when the screen goes away. Structured concurrency prevents leaks; Compose collects the StateFlow and re-renders on change:

UserViewModel.kt
class UserViewModel(private val repo: UserRepo) : ViewModel() {
    private val _user = MutableStateFlow<User?>(null)
    val user: StateFlow<User?> = _user   // expose read-only state to the UI

    fun load(id: Int) {
        viewModelScope.launch {          // scope tied to the ViewModel lifecycle
            _user.value = repo.fetch(id) // suspend call, safely off the UI thread
        }
    }
}

Recap & quick check

Key takeaways

  • Kotlin is Android's first-class language: null safety, conciseness, coroutines, and Java interop won it the role.
  • An Android project pairs Kotlin source with res/ resources, a manifest, and a Gradle build.
  • An Activity is one screen driven through lifecycle callbacks like onCreate/onResume/onDestroy.
  • Jetpack Compose is declarative UI: @Composable functions render as a function of state.
  • ViewModels expose StateFlow and run suspend work in viewModelScope, cancelled with the screen.

Quick check

1. Why did Kotlin become Android's preferred language?

2. What is an Activity?

3. How does Jetpack Compose decide what to draw?

4. Why must long work not run on the Android main thread?

5. What scope runs a ViewModel's coroutines?

Android is one target; Kotlin reaches many more. Next we'll see how one Kotlin codebase can run on iOS, the JVM, the web, and native platforms with Kotlin Multiplatform.