Android architecture guide

Material Design Android Development

Learn how to architect, implement, theme, and test modern Android applications using Material Design 3 and Jetpack Compose.

Architecture layers

Layered architecture for Compose apps

UI Layer

Composable screens, stateless components, ViewModels holding immutable UI state, and adaptive scaffolds.

Domain Layer

Reusable use cases encapsulating complex business logic and state operations.

Data Layer

Repositories acting as the single source of truth, backed by local (DataStore, Room) or remote (Ktor, Retrofit) data sources.

Core Design System

Centralized MaterialTheme, color palettes, typography scales, shape tokens, and spring motion definitions.

State management

Unidirectional Data Flow (UDF) with StateFlow

Model UI state as immutable data classes and expose them via StateFlow:

data class ProfileUiState(
    val isLoading: Boolean = false,
    val user: User? = null,
    val errorMessage: String? = null
)

class ProfileViewModel(private val repository: UserRepository) : ViewModel() {
    private val _uiState = MutableStateFlow(ProfileUiState(isLoading = true))
    val uiState: StateFlow<ProfileUiState> = _uiState.asStateFlow()

    fun loadProfile() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true) }
            try {
                val user = repository.getUser()
                _uiState.update { it.copy(isLoading = false, user = user) }
            } catch (e: Exception) {
                _uiState.update { it.copy(isLoading = false, errorMessage = e.message) }
            }
        }
    }
}

Consume state in composables with collectAsStateWithLifecycle() for lifecycle awareness.

Theming

One Expressive theme from one settings model

@Composable
fun AppTheme(state: ThemeState, content: @Composable () -> Unit) {
    val dark = when (state.mode) {
        ThemeMode.SYSTEM -> isSystemInDarkTheme()
        ThemeMode.LIGHT -> false
        ThemeMode.DARK -> true
    }
    val context = LocalContext.current
    val scheme = remember(state, dark) {
        val base = when {
            state.source == AccentSource.WALLPAPER && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
                if (dark) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
            state.source == AccentSource.CUSTOM -> seedColorScheme(state.customSeed, dark)
            else -> seedColorScheme(state.preset.seed, dark)
        }
        if (dark && state.pureBlack) base.toPureBlack() else base
    }
    CompositionLocalProvider(LocalReducedMotion provides state.reducedMotion) {
        MaterialExpressiveTheme(
            colorScheme = scheme,
            typography = AppTypography, // One variable font, weights as instances.
            shapes = AppShapes,
            motionScheme = if (state.reducedMotion) ReducedMotionScheme else MotionScheme.expressive(),
            content = content,
        )
    }
}
  • Generated color: build the full scheme from one seed with Material Color Utilities, so presets and custom colors keep proper contrast. Never hand-pick a whole scheme.
  • Stored settings migrate: when the settings model changes shape, read the old keys once and map them onto the new values.
  • No wrong first frame: keep the splash screen until stored settings arrive, with a short ceiling.

Component design

State hoisting and reusability

  • Separate Stateful & Stateless: Keep reusable UI components stateless (receiving data and emitting event lambdas).
  • Modifier Conventions: Accept a modifier: Modifier = Modifier as the first optional parameter.
  • No Controller Leaks: Never pass NavController or ViewModel instances down to child composables.

Accessibility & UX

Inclusive design standards

  • Touch Targets: Ensure a minimum interactive touch target of 48x48 dp.
  • Semantics: Provide contentDescription for icon buttons and images; merge compound item semantics.
  • Font Scaling: Design layouts that support up to 200% system font enlargement without clipping.
  • Edge-to-Edge: Enable enableEdgeToEdge() and handle window insets gracefully.

Testing & Quality

Automated testing and linting

.\gradlew.bat testDebugUnitTest
.\gradlew.bat lintDebug
.\gradlew.bat :app:assembleDebug
.\gradlew.bat :app:connectedDebugAndroidTest

Instrumented tests count only when a device or emulator ran them; compiling them is not the same as running them.

API coverage

Accountable to the whole official API

  1. A Gradle task reads the pinned sources jars, and the newest stable release, and writes a checked-in inventory of every public symbol with its stability.
  2. A ledger lists every symbol no catalog entry cites yet, as planned or excluded with a reason, and a generated report shows coverage per artifact.
  3. Unit tests check every catalog reference for existence, deprecation, stability, opt-in, availability, and reviewed version, and fail when a symbol is untracked.

After any Material dependency bump, regenerate the inventory and reconcile the ledger. Newly published symbols arrive as new and fail the tests until each is triaged.

.\gradlew.bat :app:generateMaterialApiInventory
.\gradlew.bat :app:updateApiCoverageLedger