This project demonstrates the implementation of Clean Architecture principles in an Android application using Kotlin. The UI is built with Jetpack Compose, providing a modern, declarative approach to building Android UIs.
The project follows Clean Architecture principles with a clear separation of concerns across three main layers:
- UI Components: Built with Jetpack Compose
- ViewModels: Handle UI-related data and state management
- Navigation: Handles screen navigation using Jetpack Navigation Compose
com.example.androidcleanarchitecture.presentation
├── navigation # Navigation components and screen routes
├── ui # Compose UI screens and components
│ ├── theme # Theme definitions (colors, typography, shapes)
│ ├── UserListScreen.kt
│ └── UserDetailScreen.kt
└── viewmodel # ViewModels for each screen
├── UserListViewModel.kt
└── UserDetailViewModel.kt
- Entities: Core business models
- Use Cases: Business logic operations
- Repository Interfaces: Define data operation contracts
com.example.androidcleanarchitecture.domain
├── entity # Business models
│ └── User.kt
├── repository # Repository interfaces
│ └── UserRepository.kt
├── usecase # Business logic use cases
│ ├── DeleteUserUseCase.kt
│ ├── GetAllUsersUseCase.kt
│ ├── GetUserByIdUseCase.kt
│ └── SaveUserUseCase.kt
└── util # Domain utilities
└── Resource.kt # Operation state wrapper
- Repositories: Implementation of domain repositories
- Data Sources: Local and network data providers
- Models: Data transfer objects
- Mappers: Convert between data and domain models
- Network: API services and network utilities
com.example.androidcleanarchitecture.data
├── datasource # Data source implementations
│ ├── UserDataSource.kt # Common interface
│ ├── UserLocalDataSource.kt # Local storage implementation
│ └── UserNetworkDataSource.kt # Network implementation
├── mapper # Data mappers
│ ├── UserMapper.kt
│ └── UserNetworkMapper.kt
├── model # Data models
│ ├── UserModel.kt
│ └── UserNetworkModel.kt
├── network # Network components
│ ├── ApiServiceFactory.kt
│ ├── NetworkConnectivityChecker.kt
│ ├── NetworkResult.kt
│ └── UserApiService.kt
└── repository # Repository implementations
└── UserRepositoryImpl.kt
- ServiceLocator: Simple DI implementation
- ViewModelFactory: Factory for creating ViewModels with dependencies
com.example.androidcleanarchitecture.di
├── ServiceLocator.kt
└── ViewModelFactory.kt
This project implements Clean Architecture, a software design philosophy that separates concerns into distinct layers:
- Independence of Frameworks: The business logic doesn't depend on UI, database, or external frameworks.
- Testability: Business rules can be tested without UI, database, or external dependencies.
- Independence of UI: The UI can change without changing the business rules.
- Independence of Database: Business rules don't depend on the database implementation.
- Independence of External Agencies: Business rules don't know anything about the outside world.
The dependencies flow from outer layers to inner layers:
- Presentation Layer → depends on → Domain Layer
- Data Layer → depends on → Domain Layer
- Domain Layer depends on nothing (it's the core)
This ensures that inner layers (domain) remain independent of implementation details in outer layers (data, presentation).
- UI triggers an action
- ViewModel processes the action and calls appropriate Use Case
- Use Case executes business logic using Repository interface
- Repository Implementation coordinates data from Data Sources
- Data Sources interact with local storage or network API
- Data flows back up through the layers, with each layer mapping to its own models
This project uses Jetpack Compose, Android's modern toolkit for building native UI:
- Declarative UI: UI components are defined as functions that transform state into UI
- State Management: Using StateFlow with collectAsState() for reactive UI updates
- Composable Functions: Reusable UI components built with @Composable functions
- Material Design Components: Using Material3 design system
- Navigation: Implemented with Navigation Compose
- UserListScreen: Displays a list of users with loading, error, and empty states
- UserDetailScreen: Shows user details and provides editing capabilities
- Theme: Custom theme with colors, typography, and shapes
The network layer has been implemented following clean architecture principles with proper separation of concerns:
- UserNetworkModel: Represents user data as received from the API
- UserApiService: Defines API endpoints using Retrofit annotations
- ApiServiceFactory: Creates and configures Retrofit and OkHttp clients
- UserNetworkDataSource: Implements UserDataSource interface for network operations
- NetworkResult: Sealed class for handling network responses (Success, Error, Loading)
- Resource: Domain-level wrapper for operation states (Success, Error, Loading)
The implementation uses a network-first strategy:
- Try to fetch data from the network first
- If successful, cache the data locally
- If network fails, fall back to local data
- For save/delete operations, attempt both network and local operations