A Note on Android Development
In this page:
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
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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!")
}
Login to try C/C++/Java/PHP code in the editor
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
// 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
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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")))
}
Login to try C/C++/Java/PHP code in the editor
- Trying to run Android-specific code (like
Activityor@Composablefunctions) in a plain Kotlin file outside Android Studio, which cannot compile without the Android SDK. - 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.
- Forgetting Jetpack Compose (
@Composablefunctions) is a UI framework layered on top of Kotlin, not a core language feature.
- 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
@Composablefunctions. - 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: