← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 5 of 7

A Note on Android Development

Android apps are usually written in Kotlin too, but they use extra Android-only tools that only work inside a real Android project, not a plain script.

Kotlin Powers Android Development

Since 2019, Kotlin has been Google's preferred language for Android development, and everything covered in this course -- syntax, classes, null safety, coroutines -- applies directly to writing Android apps.

Note: This is a reference snippet only -- run it in Android Studio, not with kotlinc.

Warning: This is a non-executable Android reference snippet -- it depends on the Android SDK/Jetpack Compose and cannot compile or run in a plain JVM/Kotlin sandbox. Run it in Android Studio.

Example: Kotlin Powers Android Development

markup
// This snippet illustrates a typical Android Activity class.
// It requires the Android SDK and cannot compile or run outside Android Studio.
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Jetpack Compose for UI

Jetpack Compose is Android's modern declarative UI toolkit, where screens are built from @Composable functions rather than XML layout files.

Note: Run this inside an Android Studio project configured with Jetpack Compose.

Warning: This is a non-executable Android reference snippet -- it depends on the Android SDK/Jetpack Compose and cannot compile or run in a plain JVM/Kotlin sandbox. Run it in Android Studio.

Example: Jetpack Compose for UI

markup
// This snippet illustrates a Jetpack Compose UI function.
// It requires the Android SDK and Compose libraries; not runnable here.
@Composable
fun Greeting(name: String) {
    Text(text = "Hello, $name!")
}

ViewModel and Android Architecture

Android apps commonly use a ViewModel to hold UI-related data that survives configuration changes, working together with Kotlin coroutines to load data asynchronously.

Warning: This is a non-executable Android reference snippet -- it depends on the Android SDK/Jetpack Compose and cannot compile or run in a plain JVM/Kotlin sandbox. Run it in Android Studio.

Example: ViewModel and Android Architecture

markup
// This snippet illustrates an Android ViewModel with coroutines.
// It requires the Android SDK and lifecycle libraries; not runnable here.
class UserViewModel : ViewModel() {
    fun loadUser() {
        viewModelScope.launch {
            val user = fetchUserFromNetwork()
            _userState.value = user
        }
    }
}

What Transfers Directly from This Course

The plain Kotlin language skills from this entire course -- null safety, data classes, lambdas, coroutines, extension functions -- transfer directly into Android code; only the Android SDK's own classes and lifecycle concepts are new on top.

Example: What Transfers Directly from This Course

markup
data class User(val id: Int, val name: String)

fun formatUserLabel(user: User): String = "#${user.id}: ${user.name}"

fun main() {
    // This same plain Kotlin code works identically inside an Android app
    println(formatUserLabel(User(1, "Ana")))
}
Common Mistakes
  1. Trying to run Android-specific code (like Activity or @Composable functions) in a plain Kotlin file outside Android Studio, which cannot compile without the Android SDK.
  2. Assuming everything learned about plain Kotlin syntax in this course applies identically to Android UI code; the language is the same, but Android adds its own framework and lifecycle concepts on top.
  3. Forgetting Jetpack Compose (@Composable functions) is a UI framework layered on top of Kotlin, not a core language feature.
Chapter Summary
  • Android apps are commonly written in Kotlin, but rely on Android-specific classes (Activity, ViewModel) and the Android SDK, not just the plain language.
  • Jetpack Compose is Android's modern, Kotlin-based UI toolkit, built around @Composable functions.
  • Android-specific code cannot run in a plain JVM sandbox; it requires the Android SDK/emulator provided by Android Studio.
  • Everything else in this course -- syntax, null safety, classes, coroutines -- transfers directly to Android development.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.