Most architecture articles show a login screen. Real apps have twenty features, and the structure that looked clean at feature three is a mess by feature fifteen.
Over the past months I built Shoply, a complete Android shopping app: sign-in with email, Google, Facebook and phone, a catalog, search with filters, a cart with promo codes and stock limits, a 4-step checkout, orders with a status timeline, a profile, and light/dark themes. It is about 520 Kotlin files, written test-first.
This post is about the four decisions that kept it maintainable. None of them are new. What made them work was making each one checked by the build, not by memory.
Building a shopping app for a client? Everything in this post ships ready to use in Shoply Pro: the full app source, a web admin panel and a 15-minute setup guide. From $49.
1. Package by feature, layer inside the feature
Every feature lives in its own package, and splits into the same three layers:
com.shopping.shoply ├─ cart/ │ ├─ data/ Firestore repositories, mappers │ ├─ domain/ models, rules, use cases (pure Kotlin) │ └─ presentation/ screens, ViewModels, contracts ├─ checkout/ ├─ orders/ └─ ... 20 more features The dependency rule is the usual one: presentation and data both point at domain, and domain points at nothing. It doesn't import data, presentation, or anything from android.* or androidx.*.
The problem is that in a single-module app, nothing stops you from breaking it. One tired evening, a ViewModel imports a Firestore repository directly, and the rule is gone.
So the rule is a test. Konsist reads the source and fails the build when a layer imports the wrong thing:
@Test fun `domain does not depend on data, presentation, or the Android framework`() { layerFiles("domain").assertFalse { file -> file.imports.any { import -> val layerLeak = import.name.startsWith(BASE) && (import.name.contains(".data.") || import.name.contains(".presentation.")) val frameworkLeak = import.name.startsWith("android.") || import.name.startsWith("androidx.") layerLeak || frameworkLeak } } } Three tests like this (domain, data, presentation) gave me most of the benefit of a multi-module setup, with none of the Gradle overhead. When I later added product photos and stock limits across nine screens, the tests kept every new rule in domain/ where it belonged.
2. One contract shape for every screen
Every screen has the same three types: a State data class, an Intent sealed interface for what the user does, and an Event sealed interface for one-off effects like snackbars and navigation. Here is the real contract for the Change Email screen:
data class ChangeEmailState( val step: ChangeEmailStep = ChangeEmailStep.ENTER, val newEmail: String = "", val password: String = "", val emailError: UiText? = null, val isSending: Boolean = false, val resendSecondsLeft: Int = 0, ) { // A data class toString would print the password into logs and crash reports. override fun toString() = "ChangeEmailState(step=$step, isSending=$isSending)" } sealed interface ChangeEmailIntent { data class ChangeNewEmail(val email: String) : ChangeEmailIntent data object SendLink : ChangeEmailIntent data object ResendLink : ChangeEmailIntent data object CheckIfConfirmed : ChangeEmailIntent } sealed interface ChangeEmailEvent { data class Error(val message: UiText) : ChangeEmailEvent data object LinkResent : ChangeEmailEvent } The ViewModel exposes state as a StateFlow, events as a Flow backed by a buffered Channel, and takes input through one function, onIntent(intent). Nothing else is public.
Two things made this pay off. First, every screen reads the same way, so jumping into a feature I hadn't touched in a month took minutes. Second, ViewModel tests become a list of "send this intent, expect this state", which is exactly what TDD wants.
The toString() override is a small habit worth stealing. Data classes print every field, and a state holding a password will end up in a crash report sooner or later.
3. Repositories return errors, they don't throw them
Every repository function that can fail returns a Result, never throws:
sealed interface Result<out D, out E : RootError> { data class Success<out D, out E : RootError>(val data: D) : Result<D, E> data class Error<out D, out E : RootError>(val error: E) : Result<D, E> } sealed interface DataError : Error { data object Network : DataError data object OutOfStock : DataError enum class Authentication : DataError { WRONG_PASSWORD, EMAIL_ALREADY_IN_USE, /* ... */ } } Firebase throws a dozen exception types. The data layer catches them once and maps each to a DataError. From there up, the compiler forces every caller to handle the failure, and a when over the sealed type tells you when you missed a case.
When I added stock limits, "out of stock" became one new DataError case. The compiler then pointed me at every place that had to show it.
4. Security lives in the rules, not in the app
The app talks to Firestore directly, so the client can never be trusted. The Firestore rules carry the real logic:
- Everything under
users/{uid}(cart, orders, addresses, wishlist) is readable and writable only by that user. - The catalog is read-only for shoppers.
- Admins are users with a document in
admins/{uid}, which no client can write. Only admins can edit the catalog. - An admin can move an order forward, but the rule only allows changing
statusand adding exactly one matching timeline entry. Everything else in the order stays frozen.
That last rule is what makes a web admin panel safe to ship next to the app. Even with a bug in the panel, the rules won't let it rewrite an order's total or items.
What I'd tell myself at the start
- Make the rule a test on day one. A convention that isn't checked lasts until the first deadline.
- Same shape everywhere beats the perfect shape per screen. Boring consistency is what lets you move fast in month three.
- Put errors in the type system. Sealed
Resultand error types turn "I forgot that case" into a compile error. - Treat security rules as part of the app. They are the only code a malicious client can't change.
Skip the months of setup: Shoply Pro
Everything above took months to get right. If you build shopping apps for clients, or want your own branded store app, you don't have to start from an empty project.
Shoply Pro is the complete, tested source of the app in this post, plus everything you need to ship it:
- The full Android app: sign-in (email, Google, Facebook, phone), catalog, search, cart, 4-step checkout, orders, profile, light/dark themes
- A web admin panel: manage products, photos, stock, stores, promos, promo codes and orders, with a sales dashboard
- One-command demo store: a seed script fills a new Firebase project with 29 products, 7 stores and 9 categories
- Step-by-step guides: running in about 15 minutes, then rebranding and publishing to Google Play
- Clean, enforced architecture: the exact patterns above, with tests that keep them that way
| Regular | Extended | |
|---|---|---|
| Price | $49 | $149 |
| Use for | 1 app (you or one client) | Unlimited apps and client work |
If it doesn't build by following the quick start and support can't fix it, you get your money back.
Want to look around first? The core app is free on my GitHub: github.com/MohamedSamir21/Shoply. Questions about any of the patterns are welcome in the comments.
