Feature and Core Modules
Draw module boundaries.
Two Kinds of Modules
Most modularized Android apps organize code into two main kinds of modules: feature modules and core modules, plus a thin :app module on top.
- Feature modules hold a user-facing slice of the app (a screen or flow).
- Core modules hold shared infrastructure used by many features.
In this lesson you will learn how to draw these boundaries well.
Anatomy of a Feature Module
A feature module like :feature:profile contains everything a single feature needs: its Compose screens, its ViewModel, and its UI state. It is vertical: it owns the full slice from UI down to its view-model.
It depends on core modules for shared pieces, but it should not depend on other feature modules.
// feature/profile/ProfileScreen.kt
@Composable
fun ProfileScreen(viewModel: ProfileViewModel = hiltViewModel()) {
val state by viewModel.uiState.collectAsStateWithLifecycle()
when (state) {
is ProfileUiState.Loading -> CircularProgressIndicator()
is ProfileUiState.Success -> ProfileContent((state as ProfileUiState.Success).user)
is ProfileUiState.Error -> ErrorMessage()
}
}All lessons in this course
- Why Modularize
- Feature and Core Modules
- Managing Module Dependencies
- Navigation Across Modules