What Android and Kotlin are and how they fit together
An Android app is a set of components that the system runs in a separate sandboxed process. You write it in Kotlin, which Google recommends for Android, and build the UI with Jetpack Compose. The code is split into at least two layers: a UI layer holding the state of the screens, and a data layer.
A native Kotlin app works directly with the platform APIs and the Jetpack (AndroidX) libraries. Kotlin also suits multiplatform projects (Kotlin Multiplatform), but this reference covers Android only. If one app is needed for both Android and iOS, the approaches are compared in the Flutter reference, the section “What Flutter is and how it works”.
What an app is made of
An Android app has no single entry point such as main(): the system creates the component it needs whenever the user, the system or another app addresses it. There are four kinds of components:
Activity— a screen with a UI and the user’s entry point into the app. A modern app usually has one, and it shows every Compose screen.Service— work in the background with no UI, such as playing music.BroadcastReceiver— a response to system events outside the app’s normal use, such as an alarm going off.ContentProvider— gives other apps access to the app’s data over a URI.
Components are declared in the manifest, AndroidManifest.xml; permissions such as internet access and device requirements are listed there too. The system will not run an Activity, a Service or a ContentProvider that is missing from the manifest; a BroadcastReceiver can also be registered in code.
Every app runs in its own sandbox: by default it gets its own Linux user and its own process, and other apps cannot reach its files. The system starts the process when one of the components is needed and may kill it to free memory.
How app code is structured
Components are short-lived: an Activity is recreated when the screen is rotated, and the system may kill a background process. So neither data nor state is kept in components. The official architecture splits an app into at least two layers: the UI layer shows the data, the data layer holds the business logic and works with the sources. State flows from the data to the screen, events from the screen to the data.
How Compose differs from View
| Feature | Jetpack Compose | View (XML) |
|---|---|---|
| How the UI is described | Kotlin functions with the @Composable annotation | XML layouts, from which a tree of widgets is built |
| How the screen updates | the function runs again with new data — recomposition | code changes the widgets: findViewById(), setText() |
| Where the state lives | in function parameters and state holders | inside each widget |
| Language | Kotlin only | Kotlin or Java |
| Interoperability | embeds into a View screen | embeds into a Compose screen |
For a new screen choose Compose: the UI is described in code, starting from the data, and the widgets need no manual synchronisation. Existing View screens can be migrated gradually, one at a time, because the two systems embed into each other.
What to know beforehand
- Basics of programming: variables, functions, classes. Kotlin as Android uses it — in the section “Kotlin syntax”.
- The command line and environment variables — the Bash reference.
- HTTP requests, methods and response codes — the HTTP and REST reference.
Section sources
the section is written from these pages- Application fundamentalsdeveloper.android.com/guide/components/fundamentals(opens in a new tab)
- Guide to app architecturedeveloper.android.com/topic/architecture(opens in a new tab)
- Thinking in Composedeveloper.android.com/develop/ui/compose/mental-model(opens in a new tab)
- Migrate XML Views to Jetpack Composedeveloper.android.com/develop/ui/compose/migrate/migrate-xml-views-to-jetpack-compose(opens in a new tab)
- Android’s Kotlin-first approachdeveloper.android.com/kotlin/first(opens in a new tab)