Gradle & Code Quality

Kotlin Design Patterns for Android: Prefer Clear Ownership Over Ceremony

Useful Kotlin design patterns on Android make state, ownership and errors easier to understand. Adding factories, repositories and abstractions everywhere does not automatically improve architecture; a pattern should solve a concrete boundary problem.

By Updated 2 min read

Prefer explicit state to boolean combinations

For a loading screen, a small sealed state can prevent impossible combinations such as “loading and failed and showing success”:

KOTLIN · REFERENCE EXAMPLE
sealed interface LoadState {
    data object Idle : LoadState
    data object Loading : LoadState
    data class Ready(val itemCount: Int) : LoadState
    data class Failed(val message: String) : LoadState
}

This is an illustrative model. Avoid placing sensitive raw exception details in a user-visible message.

Make ownership visible

Design choice Question it should answer
Repository boundary Who owns data retrieval and persistence?
Dependency injection Who creates and disposes of long-lived dependencies?
Immutable state Which component may change the displayed state?
Small interface What behavior does a consumer actually need?
Structured asynchronous work When is work canceled or allowed to outlive a screen?

A global singleton holding an activity is still a lifecycle risk even if it is called a service locator. The Android memory guidance explains why retained references matter.

Use nullability as a contract

Follow Kotlin's null-safety model. A non-null type is useful only when values entering from Java, serialization or external data are validated appropriately. Replacing a failing safe call with !! often moves uncertainty into a runtime crash rather than resolving it.

Prefer explicit empty, unavailable and error states when they have different meanings to the feature.

Keep conventions boring

The Kotlin coding conventions help reduce unnecessary variation. Consistent names and small cohesive files improve review more than clever syntax that hides control flow.

Validate the design with a change

Ask how the code handles a retry, canceled request, offline state or replacement dependency. If a simple change requires editing many unrelated layers, the abstraction may be adding ceremony without protecting a useful boundary.

APKLint's Kotlin and static-code tools can flag patterns, but architecture quality requires reviewing the actual responsibilities and tests. Do not treat a zero-warning snippet as proof that lifecycle and state ownership are correct.

Sources and further reading

  1. Kotlin: Coding conventions
  2. Kotlin: Null safety
  3. Android Developers: Manage your app memory

Reference review: 22 September 2026. Examples illustrate the workflow; check your installed versions, release artifact and account-specific Console requirements before applying them. This guide is not a claim that APKLint executed your project or verified your private account.

APKLint

Android inspection tools and practical release guides. About APKLint · Report a correction