{"id":178,"date":"2026-10-19T11:36:00","date_gmt":"2026-10-19T11:36:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=178"},"modified":"2026-11-06T08:48:28","modified_gmt":"2026-11-06T08:48:28","slug":"clean-architecture-in-android-with-kotlin-jetpack-compose-complete-guide","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/clean-architecture-in-android-with-kotlin-jetpack-compose-complete-guide\/","title":{"rendered":"Clean Architecture in Android with Kotlin &#038; Jetpack Compose: Complete Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Modern Android applications are becoming increasingly complex.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A typical production application may include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Multiple screens<\/li>\n\n\n\n<li>REST API integration<\/li>\n\n\n\n<li>Local databases<\/li>\n\n\n\n<li>User authentication<\/li>\n\n\n\n<li>Background synchronization<\/li>\n\n\n\n<li>Offline caching<\/li>\n\n\n\n<li>Business rules<\/li>\n\n\n\n<li>Dependency injection<\/li>\n\n\n\n<li>Unit testing<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As the application grows, keeping all these responsibilities organized becomes challenging.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without a clear architecture, developers may encounter tightly coupled components, duplicated logic, difficult testing, and expensive maintenance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Clean Architecture<\/strong> provides principles for separating application responsibilities and managing dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll learn how to implement Clean Architecture in Android using Kotlin, Jetpack Compose, MVVM, StateFlow, repositories, and use cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also build a practical Product Catalog application to demonstrate how these components work together.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Clean Architecture is<\/li>\n\n\n\n<li>Why Clean Architecture matters<\/li>\n\n\n\n<li>Clean Architecture vs MVVM<\/li>\n\n\n\n<li>Data, Domain, and Presentation layers<\/li>\n\n\n\n<li>The Dependency Rule<\/li>\n\n\n\n<li>Repository interfaces and implementations<\/li>\n\n\n\n<li>Use Cases<\/li>\n\n\n\n<li>Domain models and data models<\/li>\n\n\n\n<li>Dependency inversion<\/li>\n\n\n\n<li>StateFlow-based UI state<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Manual dependency injection<\/li>\n\n\n\n<li>Testing use cases and ViewModels<\/li>\n\n\n\n<li>Recommended project structure<\/li>\n\n\n\n<li>Common architecture mistakes<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Clean Architecture?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture is a software architecture approach that organizes application code around responsibilities and dependency boundaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Its central principle is that <strong>business rules should not depend on implementation details such as databases, networking libraries, or UI frameworks<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Android, a practical layered structure commonly includes:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Presentation Layer<\/li>\n\n\n\n<li>Domain Layer<\/li>\n\n\n\n<li>Data Layer<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Architecture Overview<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------------+\n|          Presentation Layer          |\n|                                      |\n| Jetpack Compose                      |\n| ViewModel                            |\n| UI State                             |\n+--------------------------------------+\n                  |\n                  v\n+--------------------------------------+\n|             Domain Layer             |\n|                                      |\n| Use Cases                            |\n| Domain Models                        |\n| Repository Interfaces                |\n+--------------------------------------+\n                  ^\n                  |\n+--------------------------------------+\n|              Data Layer              |\n|                                      |\n| Repository Implementations           |\n| Remote Data Sources                  |\n| Local Data Sources                   |\n| DTOs and Entity Mappers              |\n+--------------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Presentation layer handles user interaction and UI state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer contains application business rules and abstractions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Data layer implements data access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that both Presentation and Data can depend on Domain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer should remain independent of those outer implementation details.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Use Clean Architecture?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture can improve the maintainability of applications with growing complexity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Benefits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Separation of Concerns<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each layer has a clearly defined responsibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Testability<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Business rules can be tested without Android UI components or real network requests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Maintainability<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Changes to one implementation can often be isolated from unrelated components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Scalability<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additional features can be organized using consistent boundaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Flexibility<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Data implementations can be replaced without changing domain business rules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine your application initially loads products from a REST API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Later, you decide to support offline storage using Room.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With appropriate abstractions, the UI and domain logic may require little or no modification.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Data layer can coordinate the new storage implementation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Clean Architecture vs MVVM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture and MVVM solve related but different problems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MVVM primarily organizes presentation responsibilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture defines broader boundaries between business rules and implementation details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comparison<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>MVVM<\/th><th>Clean Architecture<\/th><\/tr><tr><td>Primary focus<\/td><td>Presentation organization<\/td><td>Application dependency boundaries<\/td><\/tr><tr><td>Main concepts<\/td><td>Model, View, ViewModel<\/td><td>Layers, dependency rule, abstractions<\/td><\/tr><tr><td>UI state management<\/td><td>Common responsibility<\/td><td>Usually handled in Presentation<\/td><\/tr><tr><td>Use Cases<\/td><td>Optional<\/td><td>Common in Domain<\/td><\/tr><tr><td>Repository interfaces<\/td><td>Useful<\/td><td>Common dependency boundary<\/td><\/tr><tr><td>Jetpack Compose support<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Can work together<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For larger Android applications, MVVM can be used within the Presentation layer of Clean Architecture.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Clean Architecture\n       |\n       +-- Presentation\n       |       |\n       |       +-- Jetpack Compose\n       |       +-- ViewModel\n       |\n       +-- Domain\n       |       |\n       |       +-- Use Cases\n       |       +-- Repository Interfaces\n       |\n       +-- Data\n               |\n               +-- Repository Implementations\n               +-- API \/ Database<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture does not replace MVVM.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They can complement each other.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Understanding the Dependency Rule<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Dependency Rule is one of the most important principles of Clean Architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Source-code dependencies should point toward the inner business rules, not outward toward implementation details.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Presentation ------&gt; Domain &lt;------ Data<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Presentation layer depends on domain abstractions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Data layer implements domain abstractions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer does not depend on Presentation or Data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Incorrect Dependency<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Domain ---&gt; Retrofit\nDomain ---&gt; Room\nDomain ---&gt; Jetpack Compose<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These dependencies introduce framework-specific implementation details into the business layer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Approach<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Domain ---&gt; Repository Interface\n\nData ---&gt; Repository Interface\nData ---&gt; Retrofit \/ Room<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture is about dependency direction, not simply creating packages named data, domain, and presentation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Understanding the Presentation Layer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Presentation layer manages the application&#8217;s user-facing behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In modern Android development, it commonly contains:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Jetpack Compose screens<\/li>\n\n\n\n<li>ViewModels<\/li>\n\n\n\n<li>UI state classes<\/li>\n\n\n\n<li>UI events<\/li>\n\n\n\n<li>Navigation-related presentation logic<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface ProductUiState {\n\n    data object Loading : ProductUiState\n\n    data class Success(\n        val products: List&lt;Product&gt;\n    ) : ProductUiState\n\n    data class Error(\n        val message: String\n    ) : ProductUiState\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel exposes this state to Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Responsibilities<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Receive user actions<\/li>\n\n\n\n<li>Execute appropriate use cases<\/li>\n\n\n\n<li>Transform results into UI state<\/li>\n\n\n\n<li>Display loading and error states<\/li>\n\n\n\n<li>Render the interface<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid placing direct Retrofit, Room, or file operations inside composables.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Understanding the Domain Layer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer represents application-specific business concepts and rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It may contain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Domain models<\/li>\n\n\n\n<li>Repository interfaces<\/li>\n\n\n\n<li>Use Cases<\/li>\n\n\n\n<li>Business validation rules<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example Domain Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class Product(\n    val id: Int,\n    val name: String,\n    val price: Double\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Product represents an application concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not depend on Android Context, Compose, Retrofit, or Room.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the Domain layer independent of Android-specific frameworks whenever practical.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every Android application needs a separate Domain layer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For small applications with simple data operations, a Presentation and Data architecture may be sufficient.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Introduce additional layers when they provide meaningful value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Understanding the Data Layer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Data layer is responsible for obtaining and managing application data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It may include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Repository implementations<\/li>\n\n\n\n<li>Retrofit services<\/li>\n\n\n\n<li>Room DAOs<\/li>\n\n\n\n<li>DataStore<\/li>\n\n\n\n<li>Remote data sources<\/li>\n\n\n\n<li>Local data sources<\/li>\n\n\n\n<li>Data Transfer Objects<\/li>\n\n\n\n<li>Database entities<\/li>\n\n\n\n<li>Data mappers<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProductRepositoryImpl(\n    private val remoteDataSource: ProductRemoteDataSource\n) : ProductRepository {\n\n    override suspend fun getProducts(): List&lt;Product&gt; {\n\n        return remoteDataSource\n            .getProducts()\n            .map { dto -&gt;\n                dto.toDomain()\n            }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository obtains data from a remote source.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It converts data-layer models into domain models.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rest of the application does not need to know which networking library is used.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Repository Interfaces in Clean Architecture<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Repository interfaces define the operations required by business logic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A common Clean Architecture approach places repository interfaces in the Domain layer and implementations in the Data layer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Domain Layer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface ProductRepository {\n\n    suspend fun getProducts(): List&lt;Product&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Data Layer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProductRepositoryImpl(\n    private val dataSource: ProductDataSource\n) : ProductRepository {\n\n    override suspend fun getProducts(): List&lt;Product&gt; {\n\n        return dataSource.getProducts()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dependency inversion<\/li>\n\n\n\n<li>Easier unit testing<\/li>\n\n\n\n<li>Flexible implementations<\/li>\n\n\n\n<li>Reduced coupling<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repository interfaces should expose operations meaningful to their consumers rather than unnecessarily mirroring every database or networking function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. What Is a Use Case?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Use Case represents an application operation or business action.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GetProductsUseCase<\/li>\n\n\n\n<li>LoginUserUseCase<\/li>\n\n\n\n<li>SaveExpenseUseCase<\/li>\n\n\n\n<li>CalculateBudgetUseCase<\/li>\n\n\n\n<li>ValidateEmailUseCase<\/li>\n\n\n\n<li>SyncUserDataUseCase<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class GetProductsUseCase(\n    private val repository: ProductRepository\n) {\n\n    suspend operator fun invoke(): List&lt;Product&gt; {\n\n        return repository.getProducts()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Use Case calls the repository and returns products.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Use Cases?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use Cases become especially useful when operations involve:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Multiple repositories<\/li>\n\n\n\n<li>Business validation<\/li>\n\n\n\n<li>Data transformation<\/li>\n\n\n\n<li>Reusable business rules<\/li>\n\n\n\n<li>Complex workflows<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Use Case that only delegates to a repository is acceptable for teaching architecture, but it may be unnecessary in a small production application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid creating abstraction layers without a clear purpose.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Domain Models vs Data Models<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A data model often represents information received from a specific source.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A domain model represents the application&#8217;s business concept.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Remote DTO<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class ProductDto(\n    val product_id: Int,\n    val product_name: String,\n    val product_price: Double\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Domain Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class Product(\n    val id: Int,\n    val name: String,\n    val price: Double\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Mapper<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun ProductDto.toDomain(): Product {\n\n    return Product(\n        id = product_id,\n        name = product_name,\n        price = product_price\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The mapper converts a remote data representation into a domain representation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This prevents API field names and data formats from spreading throughout the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Introduce separate models when they protect useful boundaries or represent genuinely different concerns.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Understanding Dependency Inversion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency Inversion means higher-level business logic depends on abstractions rather than concrete implementation details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Without Dependency Inversion<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class GetProductsUseCase {\n\n    private val repository =\n        ProductRepositoryImpl()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Problem<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Use Case directly creates a specific repository implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This makes replacement and testing harder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">With Dependency Inversion<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class GetProductsUseCase(\n    private val repository: ProductRepository\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flexible implementations<\/li>\n\n\n\n<li>Better testing<\/li>\n\n\n\n<li>Reduced coupling<\/li>\n\n\n\n<li>Easier maintenance<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Dependency inversion is one of the key mechanisms that makes Clean Architecture practical.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Practical Mini-Project: Product Catalog Using Clean Architecture<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s implement a Product Catalog application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Application Features<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Product list<\/li>\n\n\n\n<li>Loading state<\/li>\n\n\n\n<li>Error state<\/li>\n\n\n\n<li>Retry functionality<\/li>\n\n\n\n<li>Product filtering<\/li>\n\n\n\n<li>Use Case<\/li>\n\n\n\n<li>Repository abstraction<\/li>\n\n\n\n<li>ViewModel<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Jetpack Compose UI<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Technology Stack<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin<\/li>\n\n\n\n<li>Jetpack Compose<\/li>\n\n\n\n<li>Material 3<\/li>\n\n\n\n<li>ViewModel<\/li>\n\n\n\n<li>Kotlin Coroutines<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Clean Architecture<\/li>\n\n\n\n<li>MVVM<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll use an in-memory data source to keep the project focused on architecture.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Recommended Project Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>com.example.cleanarchitecture\/\n\u2502\n\u251c\u2500\u2500 data\/\n\u2502   \u251c\u2500\u2500 datasource\/\n\u2502   \u2502   \u2514\u2500\u2500 ProductLocalDataSource.kt\n\u2502   \u2502\n\u2502   \u2514\u2500\u2500 repository\/\n\u2502       \u2514\u2500\u2500 ProductRepositoryImpl.kt\n\u2502\n\u251c\u2500\u2500 domain\/\n\u2502   \u251c\u2500\u2500 model\/\n\u2502   \u2502   \u2514\u2500\u2500 Product.kt\n\u2502   \u2502\n\u2502   \u251c\u2500\u2500 repository\/\n\u2502   \u2502   \u2514\u2500\u2500 ProductRepository.kt\n\u2502   \u2502\n\u2502   \u2514\u2500\u2500 usecase\/\n\u2502       \u2514\u2500\u2500 GetProductsUseCase.kt\n\u2502\n\u251c\u2500\u2500 presentation\/\n\u2502   \u2514\u2500\u2500 product\/\n\u2502       \u251c\u2500\u2500 ProductUiState.kt\n\u2502       \u251c\u2500\u2500 ProductViewModel.kt\n\u2502       \u251c\u2500\u2500 ProductViewModelFactory.kt\n\u2502       \u2514\u2500\u2500 ProductScreen.kt\n\u2502\n\u2514\u2500\u2500 MainActivity.kt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This structure separates responsibilities clearly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For larger projects, consider organizing code by feature and introducing Gradle modules where appropriate.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Step 1 \u2014 Create the Domain Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/model\/Product.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.domain.model\n\ndata class Product(\n    val id: Int,\n    val name: String,\n    val price: Double,\n    val category: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This model represents a product independently of the UI and data source.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. Step 2 \u2014 Create the Repository Interface<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/repository\/ProductRepository.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.domain.repository\n\nimport com.example.cleanarchitecture.domain.model.Product\n\ninterface ProductRepository {\n\n    suspend fun getProducts(): List&lt;Product&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer defines what data operations it needs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not specify how the products are retrieved.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Step 3 \u2014 Create the Use Case<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>domain\/usecase\/GetProductsUseCase.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.domain.usecase\n\nimport com.example.cleanarchitecture.domain.model.Product\nimport com.example.cleanarchitecture.domain.repository.ProductRepository\n\nclass GetProductsUseCase(\n    private val repository: ProductRepository\n) {\n\n    suspend operator fun invoke(\n        category: String? = null\n    ): List&lt;Product&gt; {\n\n        val products = repository.getProducts()\n\n        return if (category.isNullOrBlank()) {\n\n            products\n\n        } else {\n\n            products.filter { product -&gt;\n\n                product.category.equals(\n                    category,\n                    ignoreCase = true\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Use Case loads products through the repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It also applies an optional category filter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is an example of placing reusable application logic in the Domain layer.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Step 4 \u2014 Create the Local Data Source<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/datasource\/ProductLocalDataSource.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.data.datasource\n\nimport com.example.cleanarchitecture.domain.model.Product\nimport kotlinx.coroutines.delay\n\nclass ProductLocalDataSource {\n\n    suspend fun getProducts(): List&lt;Product&gt; {\n\n        delay(800)\n\n        return listOf(\n\n            Product(\n                id = 1,\n                name = \"Android Phone\",\n                price = 19999.0,\n                category = \"Electronics\"\n            ),\n\n            Product(\n                id = 2,\n                name = \"Bluetooth Speaker\",\n                price = 2499.0,\n                category = \"Audio\"\n            ),\n\n            Product(\n                id = 3,\n                name = \"Smart Watch\",\n                price = 4999.0,\n                category = \"Electronics\"\n            ),\n\n            Product(\n                id = 4,\n                name = \"Wireless Headphones\",\n                price = 2999.0,\n                category = \"Audio\"\n            )\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The data source returns sample products.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The delay simulates asynchronous loading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a real application, this data source could use Room or another local storage mechanism.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For simplicity, this example returns domain models directly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In larger applications, data-specific models and mappers may be appropriate.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Step 5 \u2014 Create the Repository Implementation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>data\/repository\/ProductRepositoryImpl.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.data.repository\n\nimport com.example.cleanarchitecture.data.datasource.ProductLocalDataSource\nimport com.example.cleanarchitecture.domain.model.Product\nimport com.example.cleanarchitecture.domain.repository.ProductRepository\n\nclass ProductRepositoryImpl(\n    private val dataSource: ProductLocalDataSource\n) : ProductRepository {\n\n    override suspend fun getProducts(): List&lt;Product&gt; {\n\n        return dataSource.getProducts()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ProductRepositoryImpl implements the domain repository interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The repository obtains products from the data source.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer remains independent of this implementation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Step 6 \u2014 Create the UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/product\/ProductUiState.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.presentation.product\n\nimport com.example.cleanarchitecture.domain.model.Product\n\nsealed interface ProductUiState {\n\n    data object Loading : ProductUiState\n\n    data class Success(\n        val products: List&lt;Product&gt;\n    ) : ProductUiState\n\n    data class Error(\n        val message: String\n    ) : ProductUiState\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI state describes the possible screen conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Presentation layer decides how these states should be displayed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Step 7 \u2014 Create the ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/product\/ProductViewModel.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.presentation.product\n\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\n\nimport com.example.cleanarchitecture.domain.usecase.GetProductsUseCase\n\nimport kotlinx.coroutines.CancellationException\nimport kotlinx.coroutines.Job\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.launch\n\nclass ProductViewModel(\n    private val getProductsUseCase: GetProductsUseCase\n) : ViewModel() {\n\n    private val _uiState =\n        MutableStateFlow&lt;ProductUiState&gt;(\n            ProductUiState.Loading\n        )\n\n    val uiState: StateFlow&lt;ProductUiState&gt; =\n        _uiState.asStateFlow()\n\n    private var loadJob: Job? = null\n\n    init {\n        loadProducts()\n    }\n\n    fun loadProducts(\n        category: String? = null\n    ) {\n\n        loadJob?.cancel()\n\n        loadJob = viewModelScope.launch {\n\n            _uiState.value = ProductUiState.Loading\n\n            try {\n\n                val products =\n                    getProductsUseCase(category)\n\n                _uiState.value =\n                    ProductUiState.Success(products)\n\n            } catch (exception: CancellationException) {\n\n                throw exception\n\n            } catch (exception: Exception) {\n\n                _uiState.value =\n                    ProductUiState.Error(\n                        \"Unable to load products\"\n                    )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel depends on the Use Case rather than the repository implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It manages the UI state and coordinates product loading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not know whether the data comes from an API, database, or in-memory source.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. Step 8 \u2014 Create the ViewModel Factory<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/product\/ProductViewModelFactory.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.presentation.product\n\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.ViewModelProvider\n\nimport com.example.cleanarchitecture.domain.usecase.GetProductsUseCase\n\nclass ProductViewModelFactory(\n    private val getProductsUseCase: GetProductsUseCase\n) : ViewModelProvider.Factory {\n\n    override fun &lt;T : ViewModel&gt; create(\n        modelClass: Class&lt;T&gt;\n    ): T {\n\n        if (\n            modelClass.isAssignableFrom(\n                ProductViewModel::class.java\n            )\n        ) {\n\n            @Suppress(\"UNCHECKED_CAST\")\n            return ProductViewModel(\n                getProductsUseCase\n            ) as T\n        }\n\n        throw IllegalArgumentException(\n            \"Unknown ViewModel class\"\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The factory provides the Use Case dependency.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is manual dependency injection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In larger applications, Hilt can simplify dependency provisioning.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Step 9 \u2014 Create the Jetpack Compose Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/product\/ProductScreen.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture.presentation.product\n\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.foundation.lazy.LazyColumn\nimport androidx.compose.foundation.lazy.items\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp\n\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle\n\nimport com.example.cleanarchitecture.domain.model.Product\n\n@OptIn(ExperimentalMaterial3Api::class)\n@Composable\nfun ProductScreen(\n    viewModel: ProductViewModel\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    Scaffold(\n        topBar = {\n\n            TopAppBar(\n                title = {\n                    Text(\"Clean Architecture\")\n                }\n            )\n        }\n    ) { paddingValues -&gt;\n\n        Column(\n            modifier = Modifier\n                .fillMaxSize()\n                .padding(paddingValues)\n        ) {\n\n            Row(\n                modifier = Modifier\n                    .fillMaxWidth()\n                    .padding(16.dp),\n                horizontalArrangement =\n                    Arrangement.spacedBy(8.dp)\n            ) {\n\n                Button(\n                    onClick = {\n                        viewModel.loadProducts()\n                    }\n                ) {\n\n                    Text(\"All\")\n                }\n\n                Button(\n                    onClick = {\n                        viewModel.loadProducts(\"Electronics\")\n                    }\n                ) {\n\n                    Text(\"Electronics\")\n                }\n\n                Button(\n                    onClick = {\n                        viewModel.loadProducts(\"Audio\")\n                    }\n                ) {\n\n                    Text(\"Audio\")\n                }\n            }\n\n            Box(\n                modifier = Modifier\n                    .fillMaxSize()\n            ) {\n\n                when (val state = uiState) {\n\n                    ProductUiState.Loading -&gt; {\n\n                        CircularProgressIndicator(\n                            modifier = Modifier.align(\n                                Alignment.Center\n                            )\n                        )\n                    }\n\n                    is ProductUiState.Success -&gt; {\n\n                        LazyColumn(\n                            contentPadding = PaddingValues(16.dp),\n                            verticalArrangement =\n                                Arrangement.spacedBy(12.dp)\n                        ) {\n\n                            items(\n                                items = state.products,\n                                key = { it.id }\n                            ) { product -&gt;\n\n                                ProductCard(product)\n                            }\n                        }\n                    }\n\n                    is ProductUiState.Error -&gt; {\n\n                        Column(\n                            modifier = Modifier.align(\n                                Alignment.Center\n                            ),\n                            horizontalAlignment =\n                                Alignment.CenterHorizontally,\n                            verticalArrangement =\n                                Arrangement.spacedBy(12.dp)\n                        ) {\n\n                            Text(state.message)\n\n                            Button(\n                                onClick = {\n                                    viewModel.loadProducts()\n                                }\n                            ) {\n\n                                Text(\"Retry\")\n                            }\n                        }\n                    }\n                }\n            }\n        }\n    }\n}\n\n@Composable\nfun ProductCard(\n    product: Product\n) {\n\n    Card(\n        modifier = Modifier.fillMaxWidth()\n    ) {\n\n        Column(\n            modifier = Modifier.padding(16.dp),\n            verticalArrangement =\n                Arrangement.spacedBy(8.dp)\n        ) {\n\n            Text(\n                text = product.name,\n                style = MaterialTheme.typography.titleMedium\n            )\n\n            Text(\n                text = product.category,\n                style = MaterialTheme.typography.bodyMedium\n            )\n\n            Text(\n                text = \"\u20b9${product.price}\",\n                color = MaterialTheme.colorScheme.primary\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Compose screen observes StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It displays loading, success, and error states.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The category buttons send user actions to the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel executes the Use Case.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI does not directly access the repository or data source.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Step 10 \u2014 Create MainActivity<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>MainActivity.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.cleanarchitecture\n\nimport android.os.Bundle\n\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.runtime.remember\nimport androidx.lifecycle.viewmodel.compose.viewModel\n\nimport com.example.cleanarchitecture.data.datasource.ProductLocalDataSource\nimport com.example.cleanarchitecture.data.repository.ProductRepositoryImpl\nimport com.example.cleanarchitecture.domain.usecase.GetProductsUseCase\nimport com.example.cleanarchitecture.presentation.product.ProductScreen\nimport com.example.cleanarchitecture.presentation.product.ProductViewModel\nimport com.example.cleanarchitecture.presentation.product.ProductViewModelFactory\n\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n\n        super.onCreate(savedInstanceState)\n\n        setContent {\n\n            MaterialTheme {\n\n                val factory = remember {\n\n                    val dataSource =\n                        ProductLocalDataSource()\n\n                    val repository =\n                        ProductRepositoryImpl(dataSource)\n\n                    val useCase =\n                        GetProductsUseCase(repository)\n\n                    ProductViewModelFactory(useCase)\n                }\n\n                val productViewModel: ProductViewModel =\n                    viewModel(factory = factory)\n\n                ProductScreen(\n                    viewModel = productViewModel\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MainActivity acts as a simple composition root.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It creates the data source, repository, use case, and ViewModel factory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The dependencies are assembled from the outer layer inward.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For larger applications, use Hilt or another dependency injection solution to manage object creation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Required Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following dependencies to your Android app module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"androidx.activity:activity-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-ktx:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-runtime-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"org.jetbrains.kotlinx:kotlinx-coroutines-android:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.compose.material3:material3\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace version placeholders with compatible stable versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your project uses the Compose BOM, use it to manage compatible Compose library versions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Expected Application UI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Loading State<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|       Clean Architecture       |\n|                                |\n| &#91; All ] &#91; Electronics ] &#91;Audio]|\n|                                |\n|              \u25cb                 |\n|                                |\n|       Loading products...      |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Success State<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|       Clean Architecture       |\n|                                |\n| &#91; All ] &#91; Electronics ] &#91;Audio]|\n|                                |\n| +----------------------------+ |\n| | Android Phone              | |\n| | Electronics                | |\n| | \u20b919999.0                   | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Bluetooth Speaker          | |\n| | Audio                      | |\n| | \u20b92499.0                    | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Smart Watch                | |\n| | Electronics                | |\n| | \u20b94999.0                    | |\n| +----------------------------+ |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Filtered State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the user selects Audio:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|       Clean Architecture       |\n|                                |\n| &#91; All ] &#91; Electronics ] &#91;Audio]|\n|                                |\n| +----------------------------+ |\n| | Bluetooth Speaker          | |\n| | Audio                      | |\n| | \u20b92499.0                    | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Wireless Headphones        | |\n| | Audio                      | |\n| | \u20b92999.0                    | |\n| +----------------------------+ |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. How the Complete Application Works<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: User Opens the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MainActivity starts and displays ProductScreen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: ViewModel Initializes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ProductViewModel calls loadProducts().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use Case Executes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">GetProductsUseCase requests products from ProductRepository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Repository Loads Data<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ProductRepositoryImpl obtains products from ProductLocalDataSource.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Data Returns to the Domain Layer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository returns the product list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Use Case Applies Business Rules<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If a category was provided, the Use Case filters products.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: ViewModel Updates StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel publishes ProductUiState.Success.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 8: Compose Updates the UI<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ProductScreen displays the products.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 9: User Selects a Category<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI forwards the category selection to the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same flow executes again with the selected filter.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Testing the Domain Layer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One major benefit of Clean Architecture is the ability to test business logic independently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s test GetProductsUseCase.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fake Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class FakeProductRepository : ProductRepository {\n\n    override suspend fun getProducts(): List&lt;Product&gt; {\n\n        return listOf(\n\n            Product(\n                id = 1,\n                name = \"Phone\",\n                price = 10000.0,\n                category = \"Electronics\"\n            ),\n\n            Product(\n                id = 2,\n                name = \"Speaker\",\n                price = 2000.0,\n                category = \"Audio\"\n            )\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Use Case Test<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class GetProductsUseCaseTest {\n\n    @Test\n    fun `filters products by category`() = runTest {\n\n        val repository =\n            FakeProductRepository()\n\n        val useCase =\n            GetProductsUseCase(repository)\n\n        val result =\n            useCase(\"Audio\")\n\n        assertEquals(\n            1,\n            result.size\n        )\n\n        assertEquals(\n            \"Speaker\",\n            result.first().name\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The test verifies category filtering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No Android UI, database, or network connection is required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer can be tested as ordinary Kotlin code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Testing the Presentation Layer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel can also be tested using fake dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@OptIn(ExperimentalCoroutinesApi::class)\nclass ProductViewModelTest {\n\n    @get:Rule\n    val mainDispatcherRule = MainDispatcherRule()\n\n    @Test\n    fun `loads products successfully`() = runTest {\n\n        val repository =\n            FakeProductRepository()\n\n        val useCase =\n            GetProductsUseCase(repository)\n\n        val viewModel =\n            ProductViewModel(useCase)\n\n        advanceUntilIdle()\n\n        val state =\n            viewModel.uiState.value\n\n        assertTrue(\n            state is ProductUiState.Success\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example reuses the MainDispatcherRule introduced in our Kotlin Flow testing tutorial.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The test verifies the ViewModel&#8217;s state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel uses a real Use Case with a fake repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This demonstrates how Clean Architecture supports isolated testing.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Clean Architecture with Hilt<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Manual dependency injection works well for small applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, larger applications may contain dozens of repositories, use cases, and ViewModels.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hilt helps manage dependency creation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Repository Binding<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Module\n@InstallIn(SingletonComponent::class)\nabstract class RepositoryModule {\n\n    @Binds\n    abstract fun bindProductRepository(\n        implementation: ProductRepositoryImpl\n    ): ProductRepository\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@HiltViewModel\nclass ProductViewModel @Inject constructor(\n    private val getProductsUseCase: GetProductsUseCase\n) : ViewModel()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is a conceptual Hilt example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For it to work, ProductRepositoryImpl and its dependencies must also be injectable, and the application must have the required Hilt configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll cover complete Hilt integration in a dedicated tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. Clean Architecture with Retrofit and Room<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a production application, the Data layer may coordinate remote and local sources.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Presentation Layer\n       |\n       v\nProductViewModel\n       |\n       v\nGetProductsUseCase\n       |\n       v\nProductRepository\n       ^\n       |\nProductRepositoryImpl\n       |\n       +---- Retrofit API\n       |\n       +---- Room Database<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Typical Offline-First Flow<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>UI observes products from the repository.<\/li>\n\n\n\n<li>Repository exposes locally stored products.<\/li>\n\n\n\n<li>Repository synchronizes data from the network.<\/li>\n\n\n\n<li>Remote results are saved to Room.<\/li>\n\n\n\n<li>Room emits updated data.<\/li>\n\n\n\n<li>ViewModel updates UI state.<\/li>\n\n\n\n<li>Compose renders the latest products.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Offline support<\/li>\n\n\n\n<li>Centralized data access<\/li>\n\n\n\n<li>Reactive updates<\/li>\n\n\n\n<li>Reduced unnecessary network requests<\/li>\n\n\n\n<li>Better user experience<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Define a clear source-of-truth strategy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For many offline-first applications, the local database is the source of truth observed by the UI.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">30. Clean Architecture and Multi-Module Android Projects<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As an application grows, Clean Architecture boundaries can be enforced through Gradle modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/\n\u2502\n\u251c\u2500\u2500 core\/\n\u2502   \u251c\u2500\u2500 model\/\n\u2502   \u251c\u2500\u2500 common\/\n\u2502   \u251c\u2500\u2500 network\/\n\u2502   \u2514\u2500\u2500 database\/\n\u2502\n\u251c\u2500\u2500 feature\/\n\u2502   \u251c\u2500\u2500 products\/\n\u2502   \u251c\u2500\u2500 profile\/\n\u2502   \u2514\u2500\u2500 settings\/\n\u2502\n\u2514\u2500\u2500 domain\/\n    \u2514\u2500\u2500 products\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each module has a focused responsibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Module dependencies can enforce architectural boundaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not introduce multi-module architecture only because a project uses Clean Architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For smaller applications, packages inside one module are often sufficient.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31. Common Clean Architecture Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Creating Too Many Layers<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every application needs separate modules and dozens of interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Mixing Framework Dependencies into Domain<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid unnecessary Android, Retrofit, Room, or Compose dependencies in domain business logic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Putting Business Logic Inside ViewModel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Move reusable or complex business rules into appropriate domain components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Creating Use Cases for Every Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Introduce Use Cases when they improve clarity, reuse, or testability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Exposing DTOs Directly to UI<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Map source-specific models when necessary to protect application boundaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Ignoring Dependency Direction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Dependencies should respect architectural boundaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Creating Repositories Inside ViewModels<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer constructor injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Making Repository Interfaces Too Generic<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Design interfaces around meaningful application operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Skipping Unit Tests<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test business rules and presentation behavior independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Overengineering Small Applications<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Architecture should solve actual problems, not create unnecessary complexity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. Clean Architecture Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep responsibilities clearly separated.<\/li>\n\n\n\n<li>Respect the Dependency Rule.<\/li>\n\n\n\n<li>Keep business rules independent of framework details.<\/li>\n\n\n\n<li>Use repository abstractions where they provide value.<\/li>\n\n\n\n<li>Prefer constructor injection.<\/li>\n\n\n\n<li>Use Use Cases for meaningful business operations.<\/li>\n\n\n\n<li>Keep ViewModels focused on presentation logic.<\/li>\n\n\n\n<li>Expose immutable UI state.<\/li>\n\n\n\n<li>Use StateFlow for observable screen state.<\/li>\n\n\n\n<li>Use lifecycle-aware collection in Compose.<\/li>\n\n\n\n<li>Handle coroutine cancellation correctly.<\/li>\n\n\n\n<li>Keep data-source implementations in the Data layer.<\/li>\n\n\n\n<li>Introduce DTO and domain model mapping when appropriate.<\/li>\n\n\n\n<li>Test domain business rules independently.<\/li>\n\n\n\n<li>Use fake repositories for unit testing.<\/li>\n\n\n\n<li>Avoid unnecessary abstraction layers.<\/li>\n\n\n\n<li>Scale architecture gradually as the project grows.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Clean Architecture Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Component<\/th><th>Responsibility<\/th><\/tr><tr><td>Presentation Layer<\/td><td>UI and presentation state<\/td><\/tr><tr><td>Domain Layer<\/td><td>Business rules and abstractions<\/td><\/tr><tr><td>Data Layer<\/td><td>Data access implementations<\/td><\/tr><tr><td>Domain Model<\/td><td>Application business concept<\/td><\/tr><tr><td>DTO<\/td><td>External data representation<\/td><\/tr><tr><td>Repository Interface<\/td><td>Data access contract<\/td><\/tr><tr><td>Repository Implementation<\/td><td>Concrete data access behavior<\/td><\/tr><tr><td>Use Case<\/td><td>Application operation or business rule<\/td><\/tr><tr><td>ViewModel<\/td><td>Presentation state management<\/td><\/tr><tr><td>StateFlow<\/td><td>Observable state<\/td><\/tr><tr><td>Dependency Injection<\/td><td>External dependency provisioning<\/td><\/tr><tr><td>Hilt<\/td><td>Android dependency injection framework<\/td><\/tr><tr><td>Retrofit<\/td><td>HTTP networking<\/td><\/tr><tr><td>Room<\/td><td>Local database<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">34. Practical Exercises<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Domain Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a User domain model.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Repository Interface<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create UserRepository with a getUsers() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Use Case<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create GetUsersUseCase.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: Data Source<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Implement an in-memory user data source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Repository Implementation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Connect the repository implementation to the data source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: ViewModel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create UserViewModel with StateFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: Compose UI<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Display the users using LazyColumn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: Filtering<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add a Use Case that filters users by name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Unit Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test the filtering logic with a fake repository.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Complete Project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build the Product Catalog application from this tutorial and extend it with a Room database.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">35. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Clean Architecture in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture is an approach to organizing software around responsibilities and dependency boundaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It helps keep business rules independent of UI and data implementation details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are the main layers of Clean Architecture?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A common Android implementation uses Presentation, Domain, and Data layers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Clean Architecture the same as MVVM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MVVM focuses on presentation organization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture defines broader application dependency boundaries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They can be used together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Clean Architecture suitable for Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose can serve as the UI technology in the Presentation layer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the Domain layer?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Domain layer contains application business rules, domain models, and relevant abstractions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a Use Case?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Use Case represents an application operation or business action.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Where should repository interfaces be placed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In a common Clean Architecture approach, repository interfaces are placed in the Domain layer, while implementations are placed in the Data layer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Hilt required for Clean Architecture?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Manual dependency injection and other dependency injection approaches can also be used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should every Android application use Clean Architecture?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not necessarily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The level of architectural complexity should match the application&#8217;s requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Clean Architecture improve testing?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Separating business logic from framework implementations makes isolated unit testing easier.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we explored:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clean Architecture fundamentals<\/li>\n\n\n\n<li>Clean Architecture vs MVVM<\/li>\n\n\n\n<li>The Dependency Rule<\/li>\n\n\n\n<li>Presentation Layer<\/li>\n\n\n\n<li>Domain Layer<\/li>\n\n\n\n<li>Data Layer<\/li>\n\n\n\n<li>Repository Pattern<\/li>\n\n\n\n<li>Use Cases<\/li>\n\n\n\n<li>Domain models and DTOs<\/li>\n\n\n\n<li>Dependency inversion<\/li>\n\n\n\n<li>Manual dependency injection<\/li>\n\n\n\n<li>StateFlow and ViewModel<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Practical Product Catalog application<\/li>\n\n\n\n<li>Unit testing<\/li>\n\n\n\n<li>Hilt integration concepts<\/li>\n\n\n\n<li>Retrofit and Room integration<\/li>\n\n\n\n<li>Multi-module architecture<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Clean Architecture provides a structured approach to building maintainable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining Kotlin, Jetpack Compose, MVVM, StateFlow, repositories, and Use Cases, developers can build applications with clearer responsibilities and better testability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key is to apply architectural principles pragmatically and avoid unnecessary complexity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial, we&#8217;ll explore:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Article 40 \u2014 Repository Pattern in Android with Kotlin &amp; Jetpack Compose: Single Source of Truth, Caching &amp; Offline-First<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Repository Pattern fundamentals<\/li>\n\n\n\n<li>Repository interfaces and implementations<\/li>\n\n\n\n<li>Local and remote data sources<\/li>\n\n\n\n<li>Single Source of Truth<\/li>\n\n\n\n<li>Offline-first architecture<\/li>\n\n\n\n<li>Room and Retrofit integration<\/li>\n\n\n\n<li>Cache synchronization<\/li>\n\n\n\n<li>Error handling<\/li>\n\n\n\n<li>StateFlow integration<\/li>\n\n\n\n<li>MVVM and Clean Architecture<\/li>\n\n\n\n<li>Practical offline-first Android mini-project<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Clean Architecture in Android with Kotlin and Jetpack Compose. Learn Data, Domain, and Presentation layers, Dependency Rule, Repository Pattern, Use Cases, StateFlow, and build a practical Android application.<\/p>\n","protected":false},"author":1,"featured_media":179,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[8,3,333],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-architecture","category-android-development","category-android-tutorials"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions\/180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/179"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}