跪拜 Guibai
← All articles
Android

Modular Folders Aren't Architecture: Android Projects Miss the Dependency Rules

By 潜龙勿用之化骨龙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

On Android, unclear dependency boundaries double the cost of upgrading libraries and threading models; a change meant for the data layer ripples into ViewModels and UI. Enforcing these boundaries keeps swap costs low and prevents modular projects from masquerading as good design.

Summary

Dependency relationships, not folder trees, define an Android project's architecture. Splitting code into `:common`, `:user`, `:order` and clean `ui/`, `repository/`, `data/` subpackages only answers where each file sits. It says nothing about who may depend on whom, which dependencies are forbidden, or where a change should stop. A project can have an immaculate directory tree and still be riddled with circular dependencies and responsibility leaks.

Dependency inversion is the mechanism that turns physical separation into architectural separation. A ViewModel should depend on a `UserRepository` abstraction, not on Retrofit or Ktor. The data layer owns IO scheduling, translates framework exceptions into domain errors, manages its own lifecycle, and wraps callbacks into `suspend` functions or `Flow`. When those boundaries hold, swapping an HTTP client or database stays contained inside the data layer; when they leak, every caller pays for the implementation detail.

The trap is over-engineering in the name of architecture. Creating a Module, UseCase, Repository, DataSource, Factory, and Manager for a single feature often adds dozens of classes that jump between each other without introducing any real boundary. Simple package organization can be the better design. With AI generating more of this boilerplate, the scarce skill is judging why a boundary exists and who owns a scope.

Takeaways
— Physical layering only determines where code lives; it does not define dependency rules.
— A large number of modules does not indicate good architecture; dependency rules between modules matter more.
— Dependency inversion means upper layers depend on abstractions like `UserRepository`, not on concrete HTTP or database technology.
— The Data layer should own IO scheduling and expose Main-safe suspend functions.
— Exception translation should map low-level errors (IOException, HttpException) to business errors at the data boundary, while preserving `CancellationException`.
— Lifecycle ownership must be explicit; repositories should not force callers to manage start/stop.
— Network callbacks should be wrapped into `suspend` or `Flow` so async implementation doesn't leak upward.
— Over-produced UseCases, Repositories, and Modules don't create architecture; they add complexity without defining boundaries.
— AI generates code quickly but cannot judge dependency direction or change cost.
Conclusions

The gap between tidy file structure and dependency rules explains why many Android projects with beautiful modules still accumulate technical debt.

More abstractions can be as harmful as no abstractions; a boundary is only worth creating if it isolates a real change.

As AI reduces the cost of writing boilerplate, architectural judgment becomes the primary limiting factor in Android development.

Concepts & terms
Physical layering
Organizing code into folders and modules. It determines file location but not the rules for dependency and responsibility.
Dependency inversion
A principle where high-level business logic depends on abstractions (e.g., interfaces) defined by the business, while low-level implementations depend on those abstractions; this keeps technology choices from leaking upward.
Main-safe API
A suspend function that can be called from the main thread without the caller handling Dispatchers.IO; the data layer performs the thread switch internally.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗