Convert Figma logo to code with AI

android10 logoAndroid-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.

15,513
3,318
15,513
142

Top Related Projects

A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

An android boilerplate project using clean architecture

This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView

Quick Overview

Android-CleanArchitecture is a sample project demonstrating the implementation of Clean Architecture principles in Android app development. It showcases a layered architecture approach, separating concerns and promoting testability and maintainability in Android applications.

Pros

  • Promotes separation of concerns and modularity
  • Enhances testability of individual components
  • Facilitates easier maintenance and scalability of the codebase
  • Provides a clear structure for organizing code and dependencies

Cons

  • Steeper learning curve for developers unfamiliar with Clean Architecture
  • May introduce additional complexity for smaller projects
  • Requires more initial setup and boilerplate code
  • Can lead to over-engineering if not applied judiciously

Code Examples

  1. Domain layer - Use case implementation:
class GetUserList(
    private val userRepository: UserRepository
) : UseCase<List<User>, Void?>() {

    override suspend fun run(params: Void?): Either<Failure, List<User>> =
        userRepository.users()
}

This code defines a use case for retrieving a list of users, demonstrating the separation of business logic from other layers.

  1. Data layer - Repository implementation:
class UserDataRepository(
    private val mapper: UserMapper,
    private val networkHandler: NetworkHandler,
    private val service: UserService
) : UserRepository {

    override fun users(): Either<Failure, List<User>> {
        return when (networkHandler.isNetworkAvailable()) {
            true -> request(
                service.users(),
                { it.map { mapper.mapFromEntity(it) } },
                emptyList()
            )
            false, null -> Left(NetworkConnection)
        }
    }
}

This code shows a repository implementation that handles data retrieval and mapping between different layers.

  1. Presentation layer - ViewModel implementation:
class UsersViewModel(
    private val getUserList: GetUserList
) : BaseViewModel() {

    private val _users = MutableLiveData<List<UserView>>()
    val users: LiveData<List<UserView>> = _users

    fun loadUsers() = launch {
        _users.value = getUserList(None()).fold(
            { emptyList() },
            { it.map { UserView(it.id, it.name, it.email) } }
        )
    }
}

This code demonstrates a ViewModel that interacts with the use case to load and present user data to the UI.

Getting Started

  1. Clone the repository:

    git clone https://github.com/android10/Android-CleanArchitecture.git
    
  2. Open the project in Android Studio.

  3. Build and run the app on an emulator or physical device.

  4. Explore the codebase to understand the Clean Architecture implementation.

Competitor Comparisons

A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

Pros of architecture-samples

  • Official Google sample, regularly updated with latest Android practices
  • Covers multiple architectural approaches (MVI, MVVM, etc.)
  • Includes Jetpack Compose examples

Cons of architecture-samples

  • May be overwhelming for beginners due to multiple variants
  • Less focus on clean architecture principles
  • Some examples might be overly complex for simple apps

Code Comparison

Android-CleanArchitecture:

class GetUserDetails @Inject constructor(
    private val userRepository: UserRepository
) : UseCase<GetUserDetails.Params, UserDetails>() {
    override suspend fun run(params: Params) = userRepository.user(params.userId)
    data class Params(val userId: Int)
}

architecture-samples:

@HiltViewModel
class TasksViewModel @Inject constructor(
    private val tasksRepository: TasksRepository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel() {
    val tasks: StateFlow<List<Task>> = tasksRepository.getTasks().stateIn(
        viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()
    )
}

The Android-CleanArchitecture example showcases a clear separation of concerns with a use case pattern, while architecture-samples demonstrates a more direct approach using Hilt for dependency injection and StateFlow for reactive programming.

An android boilerplate project using clean architecture

Pros of android-clean-architecture-boilerplate

  • More comprehensive implementation of Clean Architecture principles
  • Includes Kotlin Coroutines for asynchronous programming
  • Better separation of concerns with distinct layers (data, domain, presentation)

Cons of android-clean-architecture-boilerplate

  • More complex project structure, potentially steeper learning curve
  • Heavier use of dependency injection, which may be overkill for smaller projects
  • Less frequently updated compared to Android-CleanArchitecture

Code Comparison

Android-CleanArchitecture:

class GetUserDetails @Inject constructor(
    private val userRepository: UserRepository
) : UseCase<GetUserDetails.Params, UserEntity>() {

    override suspend fun run(params: Params) = userRepository.user(params.userId)

    data class Params(val userId: Int)
}

android-clean-architecture-boilerplate:

class GetUser @Inject constructor(
    private val userRepository: UserRepository
) : SingleUseCase<User, GetUser.Params>() {

    override fun buildUseCaseObservable(params: Params): Single<User> {
        return userRepository.getUser(params.userId)
    }

    data class Params(val userId: String)
}

Both implementations showcase similar use case structures, but android-clean-architecture-boilerplate uses RxJava's Single for asynchronous operations, while Android-CleanArchitecture uses Kotlin's suspend functions for coroutines.

This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView

Pros of android-mvvm-architecture

  • Implements MVVM architecture with Kotlin, providing a more modern approach
  • Includes Dagger 2 for dependency injection, enhancing modularity
  • Offers a more comprehensive example with features like pagination and error handling

Cons of android-mvvm-architecture

  • May be more complex for beginners due to additional libraries and concepts
  • Less focus on clean architecture principles compared to Android-CleanArchitecture
  • Potentially higher learning curve for developers new to MVVM or Kotlin

Code Comparison

Android-CleanArchitecture (Java):

public class GetUserDetails extends UseCase<GetUserDetails.RequestValues, GetUserDetails.ResponseValue> {
    private final UserRepository mUserRepository;

    public GetUserDetails(UserRepository userRepository) {
        mUserRepository = userRepository;
    }
}

android-mvvm-architecture (Kotlin):

class PostRepository @Inject constructor(
    private val apiHelper: ApiHelper,
    private val dbHelper: DbHelper
) {
    fun fetchPosts(): Single<List<Post>> {
        return apiHelper.getPosts()
            .flatMap { posts ->
                dbHelper.insertPosts(posts)
                    .andThen(Single.just(posts))
            }
    }
}

The code comparison shows the difference in language (Java vs. Kotlin) and architectural approach. Android-CleanArchitecture focuses on use cases, while android-mvvm-architecture emphasizes repository pattern and reactive programming with RxJava.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Android-CleanArchitecture

New version available written in Kotlin:

Architecting Android… Reloaded

Introduction

This is a sample app that is part of a blog post I have written about how to architect android application using the Uncle Bob's clean architecture approach.

Architecting Android…The clean way?

Architecting Android…The evolution

Tasting Dagger 2 on Android

Clean Architecture…Dynamic Parameters in Use Cases

Demo video of this sample

Clean architecture

http://fernandocejas.com/2015/07/18/architecting-android-the-evolution/

Architectural approach

http://fernandocejas.com/2015/07/18/architecting-android-the-evolution/

Architectural reactive approach

http://fernandocejas.com/2015/07/18/architecting-android-the-evolution/

Local Development

Here are some useful Gradle/adb commands for executing this example:

  • ./gradlew clean build - Build the entire example and execute unit and integration tests plus lint check.
  • ./gradlew installDebug - Install the debug apk on the current connected device.
  • ./gradlew runUnitTests - Execute domain and data layer tests (both unit and integration).
  • ./gradlew runAcceptanceTests - Execute espresso and instrumentation acceptance tests.

Discussions

Refer to the issues section: https://github.com/android10/Android-CleanArchitecture/issues

Code style

Here you can download and install the java codestyle. https://github.com/android10/java-code-styles

License

Copyright 2018 Fernando Cejas

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

http://www.fernandocejas.com

Android Arsenal

Buy Me A Coffee