Kotlin Architecture
Good Kotlin architecture keeps business rules separate from UI and transport details. Use the language features to improve readability, not to hide complexity.
Common use cases
Kotlin's architecture patterns adapt across mobile, backend, and shared multiplatform projects while preserving clear dependency direction:
- Android apps: UI state holders (ViewModels) coordinate domain use cases and repository interfaces.
- Backend services (Spring Boot / Ktor): HTTP handlers delegate to application services, keeping SQL and external clients behind repository ports.
- Multiplatform shared modules: Pure Kotlin domain models and validation rules compile across JVM, iOS, and JS without platform imports.
Layered layout
For small-to-medium services, grouping packages by technical layer keeps entry points, business rules, and persistence adapters easy to locate:
src/
main/
kotlin/
app/ # entry points, DI wiring, routing
domain/ # entities, sealed results, business rules
data/ # database repositories, HTTP clients
ui/ # screens, ViewModels, presenters
config/ # environment and serialization setup
Feature layout
As an application grows, pure layered packages can force every feature change to touch five distant directories. Grouping top-level packages or Gradle modules by business feature (for example, billing/, catalog/, auth/) with internal layers inside each feature keeps changes localized and reduces merge conflicts.
Design rules
Follow these boundaries so Kotlin's expressiveness scales cleanly with team size:
- Keep domain models and business rules independent of UI frameworks, HTTP DTOs, and database annotations.
- Define interfaces at module boundaries so tests can substitute lightweight fakes without spinning up a database.
- Keep coroutines, dispatchers, and I/O side effects at the edges, leaving core calculations pure and synchronous where possible.
Practice
Design a clean module boundary for a small Kotlin service:
- Sketch a layered Kotlin project with a pure
domainpackage and adatarepository adapter. - Identify one growing domain area in your project that would benefit from moving to a feature module.
- Write one note explaining why pure domain functions are easier to unit test than coroutine-coupled handlers.