Splitting the app into modules
Imagine a shopping mall where every shop shares one big room. Moving one shelf disturbs everyone. Give each shop its own space, with shared corridors and a shared delivery entrance, and shops can change without bothering each other.
A modular Android app usually looks like this:
:app: small. It creates the app, sets up navigation and connects the features.:feature:*modules::feature:cart,:feature:product. Each owns its screens and view models.:core:*modules::core:designsystem(theme, buttons),:core:network,:core:database,:core:data(repositories),:core:model(plain data classes).
The rule: features use core modules, never each other. When the cart needs to open a product, it doesn’t import :feature:product; :app connects them through navigation.
In each module’s build file you list its dependencies with one of two words:
implementation: “I use this inside my module.” Modules that depend on me don’t see it.api: “My public functions use these types, so whoever depends on me needs them too.”
Use implementation almost always.
The common graph (see Now in Android) is :app → :feature:* → :core:*, with :core:model (pure Kotlin data), :core:data (repositories), :core:network, :core:database, :core:designsystem and :core:testing. Feature-to-feature edges are forbidden; navigation is wired in :app from routes each feature exposes (a small :feature:x:api module with route types and entry functions when other features must reference them). Dependency configurations decide both visibility and ABI propagation: implementation keeps a dependency off consumers’ compile classpath, so a change in it doesn’t recompile them; api exposes it, which is correct only when its types appear in your public signatures (a repository returning Flow<User> from :core:model). Pure Kotlin/JVM modules (kotlin("jvm")) build faster than Android libraries and keep domain code free of Android. Hilt works across modules (@InstallIn modules are aggregated in the app), which requires :app to depend on every module contributing bindings, transitively.
// settings.gradle.kts
include(":app")
include(":core:model", ":core:network", ":core:data", ":core:designsystem")
include(":feature:cart", ":feature:product")// feature/cart/build.gradle.kts
plugins {
alias(libs.plugins.shop.android.feature) // a convention plugin: see the next part
}
android {
namespace = "com.example.shop.feature.cart"
}
dependencies {
implementation(project(":core:data")) // repositories: used inside, not exposed
implementation(project(":core:designsystem"))
implementation(libs.androidx.lifecycle.viewmodel.compose)
}// core/data/build.gradle.kts
dependencies {
api(project(":core:model")) // CartRepository returns Flow<Cart>: Cart is part of my API
implementation(project(":core:network")) // Retrofit stays hidden behind the repository
implementation(project(":core:database"))
}// app: the only place that knows both features
NavHost(navController, startDestination = ProductListRoute) {
productGraph(onOpenCart = { navController.navigate(CartRoute) })
cartGraph(onOpenProduct = { id -> navController.navigate(ProductRoute(id)) })
}settings.gradle.ktslists every module;:feature:cartis the folderfeature/cart.:feature:cartuses repositories from:core:datawithimplementation: the cart’s code can call them, but modules depending on the cart don’t see:core:datathrough it.:core:datausesapifor:core:model, because its functions returnCartandProduct. Anyone calling the repository needs those classes.- Navigation lives in
:app: each feature exposes acartGraph(...)with lambdas likeonOpenProduct, and:appdecides where they lead.
Predict the output
The app module depends on :feature:profile. :feature:profile declares implementation(project(":core:model")). Code in :app uses the User class from :core:model directly, without its own dependency on :core:model. What happens?
implementation keeps a dependency private to the module that declares it. :app gets :feature:profile's own classes, but not :core:model on its compile classpath. Either :app adds its own implementation(project(":core:model")) (usually right) or :feature:profile uses api if User is part of its public functions.
Practice: extract the design system
- Create the module: File → New → New Module → Android Library, named
:core:designsystem, packagecom.example.shop.core.designsystem. - Move the theme:
Color.kt,Type.kt,Theme.ktand shared components likeShopButtongo into the new module. Resources (colors, icons) move to itsres/folder. - Fix visibility: Kotlin’s default is
public, so the functions are already visible. Mark helpers you don’t want others to useinternal. - Add the dependency: every module that uses the theme adds
implementation(project(":core:designsystem")). - Build and run: the app looks the same, and a change in a feature module no longer rebuilds the design system.
Tap the line with the bug.
// feature/cart/build.gradle.kts
dependencies {
implementation(project(":core:data"))
implementation(project(":feature:product")) // to show ProductCard in the cart
}
// feature/product/build.gradle.kts
dependencies {
implementation(project(":core:data"))
implementation(project(":feature:cart")) // for the "Add to cart" button state
}Hint: What does Gradle do when two modules depend on each other?
Gradle refuses to build: “Circular dependency between the following tasks”. Even one direction (cart → product) would break the rule that features don’t depend on each other, and tie the two teams together.
Move what both need down into a core module, and let :app connect the rest:
ProductCard(a UI component) goes to:core:designsystemor a small:core:uimodule.- The cart state the product screen needs comes from
CartRepositoryin:core:data, which both features already use.
Keeping the graph honest
- Enforce rules in CI: a module-graph check (for example the
module-graph-assertplugin, or a small script oversettings.gradle.ktsand build files) that fails on:feature→:featureedges. - Dependency drift:
dependency-guardsnapshots each module’s resolved dependencies so unexpected transitive changes show up in review. - Dynamic feature modules (Play Feature Delivery) invert the dependency (
:dynamicdepends on:app) and add install-time complexity; use them only for large optional features, not as a modularization strategy.
:app→:feature:*→:core:*(model, data, network, database, designsystem, testing). No feature → feature edges;:appwires navigation, or features expose:apiroute modules.implementationby default (private, limits recompilation);apionly for types in your public signatures.- Pure Kotlin/JVM modules for domain code: faster, Android-free.
- Hilt aggregates
@InstallInmodules in the app::appmust (transitively) depend on all of them. - Enforce the graph in CI (module-graph assertions, dependency-guard); dynamic feature modules only for large optional features.
Check this part
The cart needs a product card UI that the product feature also uses. Where should it live?
What does implementation(project(":core:network")) mean for modules that depend on yours?
When is api the right configuration?
Why does api(libs.retrofit) in :core:network slow builds?