Architecture & DI

ViewModel, repositories, Hilt for DI, and the Android Jetpack recommended layering. Testability and separation are what senior Android loops grade.

About 21 min read3-question knowledge check

The mental model

Think of a restaurant. The waiter (UI) takes orders and brings food but never touches the stove. The kitchen (domain/business logic) decides how a dish is made. The pantry and suppliers (data layer) hold and fetch ingredients. Each has one job, and they talk through a narrow, well-defined pass-through window, not by reaching into each other's stations. If the waiter cooked, restocked, and served, one busy night would collapse the whole operation — which is exactly what a "God Activity" that does networking, parsing, business logic, and UI in one file does on a busy screen.

**Android architecture is the discipline of separating those stations so each is independently understandable, testable, and replaceable.** The modern Android recommended structure is three layers: a **UI layer** (Compose/Views + a ViewModel that holds UI state), an optional **domain layer** (use cases for reusable business logic), and a **data layer** (repositories that expose data and hide their sources). The arrows point one way — UI depends on domain depends on data — and nothing lower ever knows about anything higher. The ViewModel never imports a networking type; the repository never imports a UI type.

The second half of the discipline is **dependency injection (DI)**. Objects should not build their own dependencies (a ViewModel newing up a Retrofit client welds them together and makes the ViewModel untestable). Instead, dependencies are *provided* from outside — by Hilt, the recommended DI framework — so you can swap a real repository for a fake in tests and a mock backend in previews. DI is what makes the layering actually pay off, because a layer you can't substitute is a layer you can't test in isolation.

The idea interviewers grade — and the one senior Android loops are built around — is **testability through separation**. A junior ships a screen that works; a senior ships a screen whose business logic runs in a fast JVM unit test with no device, no network, and no Context, because the logic lives in a ViewModel/use case that depends on interfaces, not on Android. If your logic can only be tested by launching the app, your architecture failed.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision