Architecture • Android Compose
Mastering
Clean Architecture
in Android Compose
🏗️ Build robust, scalable, and
testable Android apps
💡 Why Clean Architecture?
🧠 Decoupled Layers: Isolate U
🪐 Flexible Frameworks
🚀 Team-Friendly:
Build robust Android apps with architecture used
by top teams.
Why Clean Architecture ? 1/20
Common Pain Points
🧩 Messy Codebase
Activities & Fragments with 1000+ lines
🐞 Hard to Test
Business logic tangled with UI code
💣 Breaks Easily
UI tweaks cause random crashes
♻️ Poor Reusability
Duplicated logic everywhere
How Clean Architecture Helps 🚀
Modular & Organized: Separate UI, business logic, and
data for easy maintenance
Highly Testable: Isolated logic enables simple, fast unit
testing
Flexible Swaps: Change frameworks (Room, Retrofit,
etc.)—no headaches
What is Clean Architecture 2/20
? (Android Perspective)
Why Use It?
🚀 Scalable: Easily grow your app
🧪 Testable: Write reliable tests
🛠️ Maintainable: Refactor with confidence
Core Principles
Layered Structure:
Separate into UI, Domain, and Data layers for clear boundaries
Dependency Rule:
Only depend inward—inner layers; they never require the
outer layers
Framework-Independent Core:
Business logic stays pure; no direct links to Android, Room, or
Retrofit
The Big Win: Change your UI, database, or network code—
without breaking business logic!
The Layers of Clean 3/20
Architecture in Android
🎨 UI Layer
Jetpack Compose, Activities, Fragments
Handles what the user sees & interacts with
🧑💼 Presentation Layer
ViewModels, Presenters
Holds UI state, connects UI with domain logic
🧠 Domain Layer
UseCases, Business Logic
App rules, pure Kotlin, no Android imports!
💾 Data Layer
Repositories, Room, Retrofit, DataSources
Where your data lives (DB, API, cache)
Each layer only knows about the layer directly beneath it—
never more!
Clean Architecture – 4/20
Android Edition
How Data Flows in 5/20
Clean Architecture
6/20
Step-by-Step: Data Flow
in Clean Architecture
🖐️ 1. User interacts with the UI (Compose screen)
(e.g., taps a button)
🧑💼 2. ViewModel receives the event
(prepares or validates data, calls business logic)
💎 3. UseCase executes business logic
(pure Kotlin, no Android code)
🗄️ 4. Repository handles data
(fetches/saves from Room, Retrofit, or cache)
🔄 5. Result sent back to ViewModel/UI
(UI updates, shows new state)
Clean, predictable flow — easy to test and debug!
7/20
Show Me The Code!
❌ Before: Everything Mixed Together
8/20
Show Me The Code!
✅ After: Clean Architecture
Jetpack Compose in 9/20
Clean Architecture
🎨 UI Layer: Compose Screens
• @Composable functions and UI logic
• Handles user events and displays state
🧑💼 Presentation Layer: ViewModel
• Bridge between UI and domain
• Collects events, exposes state, calls UseCases
💎 Domain Layer: UseCases
• Business rules and app logic (pure Kotlin, no Android)
• Easy to unit test
🗄️ Data Layer: Repository, Room, Retrofit
• Handles data from local DB, network, or cache
Pro Tip:
Keep UI state in ViewModel, business logic in UseCases,
and data fetching in Repositories for maximum clarity!
Jetpack Compose is just the UI—Clean Architecture
keeps your code flexible and future-proof.
Testing Made Easy with 10/20
Clean Architecture
✅ Unit Test UseCases: Mock repositories, test pure business logic
- high coverage achievable!
✅ Test ViewModels: Mock UseCases, verify UI state changes
✅ UI Tests: Focus only on critical user interactions
📊 Clean Architecture Testing Benefits:
Coverage: Significantly higher achievable
Speed: Much faster test execution
Maintenance: Dramatically easier to maintain
Flakiness: Greatly reduced test instability
🎯 Recommended Testing Focus:
Domain Layer: Highest priority (easy to test!)
ViewModels: High priority (state logic)
Repositories: Medium priority (data mappings)
UI: Lower priority (critical user flows only)
💡 Game Changer: Most of your tests become simple, fast unit
tests that run in milliseconds!
Clean Architecture makes testing enjoyable instead of painful! 🚀
Where Do MVVM, MVI, 11/20
MVP, UDF Fit in Clean
Architecture?
Clean Architecture = Layering (UI, Presentation, Domain,
Data)
MVVM, MVI, MVP, UDF, Redux = Presentation Patterns
→ These all fit INSIDE the Presentation Layer!
🧩 MVVM, MVI, MVP, UDF, Redux
Define how your UI & ViewModel/Presenter manage state
and events.
🎯 Clean Architecture
Defines the boundaries and responsibilities for each app
layer.
✅ Mix and Match:
Use MVVM, MVI, or MVP inside Clean Architecture's
Presentation Layer—they're not mutually exclusive!
Choose the Presentation Pattern that fits your team and
app.
Clean Architecture works with all of them!
12/20
Module Structure
🎯 Benefits: Faster builds • Team independence • Feature
toggles
📋 Rules: Features → Core → Never the reverse!
💡 Start Simple: Begin with app + core modules, split
features later as you grow!
Essential Tools for Clean 13/20
Architecture
Essential Tools for Clean Architecture 🛠️
💉 Dependency Injection Hilt (Google's choice) or Koin (simpler)
🌐 Networking Retrofit + OkHttp + Gson/Moshi
💾 Database Room (SQLite) + DataStore (SharedPrefs
replacement)
⚡ Async Coroutines + Flow (goodbye RxJava!)
🧪 Testing MockK + JUnit + Espresso + Turbine (Flow testing)
📊 Code Quality Detekt + KtLint + SonarQube
🎨 UI Jetpack Compose + Accompanist + Coil (images)
📱 Architecture Components ViewModel + Navigation +
WorkManager
💡 Starter Pack: Hilt + Retrofit + Room + Coroutines = 80% of your
needs covered!
Avoid: Over-engineering with too many libraries. Start simple,
add as needed.
Useful Patterns in Clean
🎯
14/20
Architecture
Useful Patterns in Clean Architecture 🎯
🏭 Repository Pattern: Abstract data sources (Room, Retrofit)
🏗️ Factory Pattern: Create complex objects cleanly
👀 Observer Pattern: Reactive data flow with Flow/LiveData
🎮 Strategy Pattern: Swap algorithms at runtime (payments, auth)
💉 Dependency Injection: Wire everything together (Hilt/Koin)
🧩 Mapper Pattern: Convert between layers (Entity ↔ DTO ↔ UI
Model)
🎭 Facade Pattern: Simplify complex subsystems
📦 Result Wrapper: Handle success/error states elegantly
💡 Pro Tip: Don't use all patterns at once! Start with Repository + DI,
add others as needed.
These patterns solve 90% of Clean Architecture challenges!
Where Most Get It Wrong 15/20
(and How to Avoid It!)
🚫 Mixing layers
Don’t put business logic in ViewModel or UI screens—keep it in
UseCases (Domain Layer).
🚫 Android imports in Domain
Your Domain Layer should be pure Kotlin—no Context, no LiveData,
no Android classes.
🚫 Over-abstracting
Don’t create interfaces just for the sake of it. Only abstract when
you really need to swap implementations.
🚫 Ignoring the Dependency Rule
Dependencies should always point inward—never let Data or
Domain depend on UI!
Pro Tips:
✅ Keep UI “dumb”—put logic in ViewModel or UseCases
✅ Use Dependency Injection (Hilt or Koin) to manage
dependencies
✅ Write tests for UseCases—they’re the heart of your business
logic
Clean Architecture isn’t about “more classes,” but about clear
boundaries.
Start simple, and refactor as your app grows!
How to Migrate Your 16/20
Existing App
1️⃣ Start Small: Pick one feature to refactor
2️⃣ Create Domain Layer: Extract business logic into UseCases
3️⃣ Add Repository Pattern: Wrap your data sources
4️⃣ Update ViewModel: Use UseCases instead of direct calls
5️⃣ Iterate: One feature at a time!
⏱️ Timeline (varies by complexity):
Simple feature: 1-3 days
Complex feature: 1-2 weeks
Full migration: Depends on app size and team
✅ Do: Test as you go, keep old code working
❌ Don't: Refactor everything at once
💡 Pro Tip: Start with your most problematic feature—clear wins
build momentum!
Remember: You don't need to stop feature development.
Migrate gradually and adjust timeline based on your specific
codebase complexity.
Real-World Impact 17/20
Benefits Teams Report 📊
🎬 Example Results:
Reduced bugs and faster feature delivery
Less code duplication, better test coverage
Fewer crashes, easier security compliance
Faster framework/library swapping
📈 Common Improvements:
Dev Speed: Significantly faster over time
Bug Rate: Substantial reduction
Test Coverage: Major improvements possible
Onboarding: Faster for new team members
💡 The Pattern is Clear: Clean Architecture = Faster
Development + Fewer Bugs + Happier Teams
Your results will vary, but the impact is consistently positive!
Recap: Clean 18/20
Architecture for Android
🎯 Core Principles:
Separation of Concerns: Each layer has one job
Dependency Rule: Dependencies point inward only
Framework Independence: Business logic doesn't know
about Android
📦 The 4 Layers:
UI: Compose screens, user interactions
Presentation: ViewModels, state management
Domain: UseCases, business rules (pure Kotlin!)
Data: Repositories, APIs, databases
✅ Key Benefits:
Highly testable code with excellent coverage potential
Easy to maintain and scale
Swap frameworks without breaking logic
Teams can work independently
🚀 Remember: Clean Architecture is a journey, not a
destination. Start with one feature, prove the value, then
expand. Your future self will thank you!
The goal: Build apps that are a joy to work on, not a pain to
maintain! 💪
Learn from Open Source 19/20
🎯 Essential Repos
📱 Now in Android - android/nowinandroid
Modern showcase with Compose + Clean Architecture
🏗️ Architecture Samples - android/architecture-samples
Google's official architecture blueprints
📝 Clean Note App - philipplackner/CleanArchitectureNoteApp
Simple Room + Compose + Hilt example
🚀 MVI Sample - mitchtabian/Open-API-Android-App
Blog app with Retrofit, Room, Dagger, MVI
🌐 Multiplatform - JetBrains/compose-multiplatform
Clean Architecture across platforms
💡 Study Focus
Module structure
Dependency injection
Testing approaches
State management
Pro Tip: Don't copy—understand their architectural patterns!
Note: Focus on patterns over popularity
20/20
What Next? Take Clean
Architecture Further!
💡 Try refactoring one feature in your app
using Clean Architecture
🔍 Explore Dependency Injection with Hilt
or Koin
📦 Break down features into their own
modules for even more scalability
📚 Keep learning: follow top Android devs,
read Clean Code, and study open-source
projects
Thank You for Reading!
Ready to dive deeper into Android, Kotlin, and
Clean Architecture?
I regularly share hands-on tutorials and
practical code tips that you can apply directly
to your projects.
Got questions or want to see specific topics
covered? Drop a comment below or send me
a DM - I'd love to hear what you're working on!