# Modern Android Architecture: ViewModel, Flow, Hilt & Room

> Google's recommended app architecture with ViewModels, coroutines and Flow, dependency injection with Hilt, and Room for local data. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/mobile/android-architecture
Last updated: 2026-11-28

Google's recommended app architecture with ViewModels, coroutines and Flow, dependency injection with Hilt, and Room for local data.

Intermediate · about 12 hours · difficulty 4/5

Learn first: [Jetpack Compose in Depth](https://www.internhack.in/roadmaps/tracks/mobile/compose-in-depth), [App-wide State Tools](https://www.internhack.in/roadmaps/tracks/mobile/state-libraries)

Google publishes a recommended architecture that most modern Android teams follow. Learning it makes any production codebase readable to you.

### Layers

- **UI layer**: composables plus a `ViewModel` that exposes a single `StateFlow<UiState>` and accepts events.
- **Domain layer** (optional): use cases for reusable business logic.
- **Data layer**: repositories combining remote sources (Retrofit or Ktor) and local sources (**Room**), exposing `Flow`s.

### Coroutines and Flow

- `viewModelScope.launch` for work tied to the screen; it cancels when the ViewModel is cleared.
- `Flow` for streams; `stateIn` turns a flow into state for the UI; collect in Compose with `collectAsStateWithLifecycle()`.
- Use `withContext(Dispatchers.IO)` for blocking I/O inside repositories.

### Dependency injection

**Hilt** provides repositories and data sources to ViewModels (`@HiltViewModel`, `@Inject constructor`), and lets tests swap in fakes.

### Room

Define `@Entity` classes, `@Dao` interfaces with queries returning `Flow`, and migrations when the schema changes.

### Pitfalls

Exposing `MutableStateFlow` publicly lets the UI mutate state; expose read-only `StateFlow`.

**Good enough to move on:** a feature reads from Room, refreshes from the network, and the UI updates automatically through Flow.

## Key concepts

- **UI, domain and data layers**
- **ViewModel and UiState**
- **Coroutines and viewModelScope**
- **Flow and StateFlow**
- **Hilt dependency injection**
- **Room entities, DAOs and migrations**
- **Retrofit or Ktor client**
- **Multi-module projects**

## Mini project

Build a GitHub repo browser that caches results in Room, refreshes from the API, and exposes a single UiState from a Hilt-injected ViewModel.

## Check yourself

Why should a ViewModel expose StateFlow rather than MutableStateFlow?

## Free resources

- [Guide to app architecture](https://developer.android.com/topic/architecture) (Android Developers)
- [Save data with Room](https://developer.android.com/training/data-storage/room) (Android Developers)
- [Dependency injection with Hilt](https://developer.android.com/training/dependency-injection/hilt-android) (Android Developers)

Previous: [Jetpack Compose in Depth](https://www.internhack.in/roadmaps/tracks/mobile/compose-in-depth) · Next: [iOS App Lifecycle, Xcode Projects & Capabilities](https://www.internhack.in/roadmaps/tracks/mobile/ios-fundamentals)

## More in Android with Kotlin in Depth

- [Android Components, Lifecycle & Gradle](https://www.internhack.in/roadmaps/tracks/mobile/android-fundamentals)
- [Jetpack Compose in Depth](https://www.internhack.in/roadmaps/tracks/mobile/compose-in-depth)
- [Modern Android Architecture: ViewModel, Flow, Hilt & Room](https://www.internhack.in/roadmaps/tracks/mobile/android-architecture)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Mobile App Developer Roadmap](https://www.internhack.in/roadmaps/tracks/mobile)
