跪拜 Guibai
← Back to the summary

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

Many developers mistake physical layering for architecture

Working with Android for a long time, you often see projects like this:

:app
:common
:network
:database
:user
:order
:payment

Modules are split very finely.

Open :user, it also has:

ui/
viewmodel/
repository/
data/

Now look at the project structure:

:app
  ↓
:user
  ↓
:network

At a glance, it looks very much like architecture.

But there's an easily overlooked problem here:

If the code is separated, has architecture really formed?

Not necessarily.

Because:

Physical layering does not equal architecture layering.

1. Physical layering just puts code in different places

The most common physical layering:

activity/
fragment/
viewmodel/
repository/
api/
database/
utils/

Activities go into Activity.

ViewModels go into ViewModel.

Repositories go into Repository.

APIs go into API.

Databases go into Database.

Looks very tidy.

But this structure only answers one question:

Where should the code go?

It does not answer:

So a project can have a very beautiful directory structure while being full of circular dependencies, reverse dependencies, and messy responsibilities.

The directory can be clean, and the architecture can be messy.

2. More modules does not mean better architecture

This is another place where illusions easily form.

For example:

:app
:common
:network
:database
:user
:order
:payment

Many people see a project like this and subconsciously think modularization is done well.

But modules are just a means of physical isolation.

What really matters is:

Why does :user depend on :network?

Why does :order depend on :user?

Why does :payment depend on :order?

Why can everyone depend on :common?

If it ends up like:

:user ─────→ :network
   ↓
:order ────→ :user
   ↓
:payment ──→ :order

Or even worse, with circular dependencies, reverse dependencies, and a universal common module.

Then no matter how many modules there are, it just spreads the mess to more places.

So:

More modules does not mean good architecture.

The real architecture is not about how many walls there are, but about the rules between the walls.

3. The core of real architecture is dependency inversion

Let's look at a simple business scenario again.

A ViewModel needs to load user information.

One design is:

ViewModel
    ↓
Retrofit API
    ↓
Network

The ViewModel directly knows low-level implementation details like Retrofit, HTTP, and network exceptions.

Suppose one day we need to replace Retrofit with Ktor, or replace it with another data service.

The upper layers may have to change as well.

Another design:

UI / ViewModel
       ↓
Repository abstraction
       ↑
Repository implementation
       ↓
Network / Database

Here it needs special attention:

The Repository abstraction is defined by what the upper business logic needs, and the Data layer provides the concrete implementation.

For example:

interface UserRepository {
    suspend fun getUser(): User
}

Data layer implementation:

class UserRepositoryImpl(
    private val api: UserApi
) : UserRepository {
    override suspend fun getUser(): User {
        return api.getUser().toDomain()
    }
}

The ViewModel only depends on UserRepository; it doesn't need to know whether the underlying layer uses Retrofit or Ktor.

This is what dependency inversion actually tries to solve.

It's not about creating a few extra Interfaces; it's about:

Letting upper business logic no longer depend on lower-level technology implementation.

4. The value of architecture is to give change a clear boundary

This is also the most interesting part on the right side of the diagram.

Suppose the underlying layer changes:

Retrofit → Ktor

Room → another database

third-party SDK → self-developed SDK

If the architecture has clear boundaries, these changes should be confined as much as possible to the Data layer.

UI / ViewModel
       │
       │  business abstraction
       │
───────┼───────
       │
Data Layer / Repository
       │
       │
Network / Database

When the underlying implementation changes, the upper layer should not change along with it.

Of course, this is not to say that all changes can be isolated by the Repository.

If the business contract itself changes, the upper layer naturally may also need adjustment.

But changes in underlying technology implementation should not propagate to the business layer without reason.

This is the value of boundaries:

Changes should be limited to the scope they belong to.

5. In an Android project, where do boundaries actually show up?

Dependency boundaries are not an abstract concept.

They directly affect many designs in daily development.

1. IO scheduling should belong to the Data layer

If every ViewModel needs to write:

viewModelScope.launch {
    withContext(Dispatchers.IO) {
        repository.loadData()
    }
}

Then the caller must know which thread data access needs to run on.

This means low-level implementation details have leaked to the upper layer.

A more reasonable way:

interface UserRepository {
    suspend fun loadData(): User
}

The Data layer handles IO scheduling internally and provides a Main-safe API.

The caller doesn't need to care which Dispatcher is used underneath.

2. Exception translation should belong to clear boundaries

The underlying layer can have:

IOException
HttpException
SocketException

These are exceptions specific to the technology implementation.

The Data layer should, according to the business contract, translate the exceptions that need to be exposed upward into error types the upper layer can understand.

For example:

NetworkException
       ↓
Data Layer
       ↓
NetworkFailure
       ↓
ViewModel

Of course, CancellationException cannot be swallowed like a normal business exception; coroutine cancellation semantics must be preserved.

3. Lifecycle should have a clear owner

If a Repository internally starts coroutines, registers listeners, and holds resources, but requires the caller to manually execute:

repository.start()
repository.stop()

Then the caller must know how the Repository manages its lifecycle.

That is also a responsibility leak.

It should be clear who owns the resources, who manages the coroutines, and how to clean up on cancellation.

Structured concurrency helps us build clear lifecycle relationships rather than manually managing tasks everywhere.

4. The Data layer should not leak callbacks upward

If the underlying SDK uses callbacks, and the upper business logic has to understand their registration, deregistration, and callback ordering, then the boundary isn't clean.

The Data layer can wrap traditional asynchronous callbacks into suspend or Flow:

interface UserRepository {
    suspend fun getUser(): User
    fun observeUser(): Flow<User>
}

What asynchronous mechanism the underlying layer uses is the Data layer's own implementation detail.

The upper layer only uses asynchronous APIs that fit its business needs.

These seemingly separate issues of coroutines, exceptions, and lifecycle all point to the same thing:

Don't let one layer take on responsibility that isn't its own, and don't let implementation details cross boundaries arbitrarily.

6. Don't create architecture for the sake of architecture

Here we need to correct another common misconception.

When many people talk about Clean Architecture, they immediately start to:

Create a Domain Module
Create a Repository
Create a UseCase
Create a DataSource
Create various Interfaces

As if the more components, the more standard the architecture.

But these things themselves don't represent architectural quality.

A small team of three people, to conform to some architecture template:

one feature
→ one Module
→ one UseCase
→ one Repository
→ one DataSource
→ one Factory
→ one Manager

In the end, dozens of classes jump around each other.

This design is not necessarily better than a simple Package organization.

Modules, Packages, Classes, Interfaces are all means.

What should really be considered is:

Simplicity is not the absence of architecture; it is the absence of unnecessary complexity.

7. In the AI era, understanding architecture boundaries is even more important

Today, AI can generate very quickly:

Module
Repository
UseCase
ViewModel
Flow
Coroutine
DI

The code looks very standard too.

But AI can easily tell you:

You should add a UseCase here.

You should create a Repository here.

You should use withContext(Dispatchers.IO) here.

You should create a CoroutineScope here.

What developers really need to judge is:

Why?

What is the boundary of this Repository?

Why is the caller responsible for IO?

Who owns this Scope?

What is its lifecycle?

AI can generate code quickly, but it cannot replace the developer's judgment about dependency relationships, responsibility boundaries, and change costs.

Code is getting cheaper, and judgment is getting more expensive.

Conclusion

Back to the original question:

Why can a project with very finely split modules and a very beautiful directory structure still be a terrible architecture?

Because physical layering only solves the problem of code organization.

It does not automatically solve the problems of dependency relationships, responsibility ownership, and change isolation.

So when evaluating an Android project, don't just look at:

How many modules?
How many Repositories?
How many UseCases?
Is the directory structure pretty?

Instead, look at:

Who depends on whom?

Who is responsible for what?

Which dependencies should be forbidden?

After an underlying change, where does the impact stop?

Physical layering is separating code.

Architecture layering is separating responsibilities, dependencies, and change boundaries.

True architecture is never in the directory.

It's in the boundaries.