Git Product home page Git Product logo

cleanarchitectureforandroid's Introduction

Clean Architecture for Android Sample Project

This project is a loose implementation of Clean Architecture as presented in my book, Clean Architecture for Android. It is a native Android project written in Kotlin. It demonstrates the key principles presented in the book and how they apply to a real life project.

I will endeavour to keep this project up to date and use it to demonstrate the strengths of the architecture: scalability, testability and flexibility when it comes to choosing 3rd party solutions.

Features

  • Feature separation
  • Layer separation
    • UI
    • Presentation
    • Domain
    • Data
    • Data Source
  • Unit tests
  • End-to-end tests
  • Demonstrates use of Jetpack Compose
  • Demonstrates use of Coroutines including Flow
  • Demonstrates MVVM
  • Code quality checks using ktlint
  • Code quality checks using detekt
  • Continuous integration (CI) using GitHub Actions
    • Unit tests
    • Instrumentation tests
    • Linting

Choices

  • ** Mappers as classes vs. mapping extension functions **

    When mapping between models, we have several options. The primary decision is between mapper classes and mapping extension functions.

    While extension functions are more concise, using them for mapping limits our choices of testing frameworks (Mockito, for example, cannot stub static functions).

    How about injecting the mapper extension functions? We could do that. However, this removes the benefits of conciseness almost entirely. It also makes navigation to the implementation harder.

    And so, I opted for the slightly more verbose concrete mapper classes.

  • Skipping Google's Architecture Components

    The greatest issue with Google's Architecture Components is that they leak Android details into the Presentation layer. This prevents the Presentation layer from being truly UI agnostic.

    Another issue with the Architecture Components is that they give too much responsibility to the ViewModel. They make it persist state it does not own, leading to potential data synchronization bugs.

    For these reasons, while still following MVVM, this project relies on Kotlin Flows rather than LiveData, and implements pure ViewModels rather than Google's.

  • Mocking framework

    Both Mockito-Kotlin and Mockk are used in this project to demonstrate how the use of each would look.

    My personal preference remains Mockito-Kotlin. I find the code easier to read and follow when using it. At the time of writing, judging by the number of stars on each repository, the industry seems to lean towards Mockk.

  • Dependency injection framework

    A critical part of most modern apps, dependency injection (DI) helps us obtain the objects that build our app. It also helps manage their scope. The most popular choices in the Android world are Hilt (which is built on top of Dagger) and Koin.

    Hilt was chosen for two main reasons:

    1. Compile time safety - having the confidence that all my dependencies are provided before the app starts is a huge time saver and helps maintain a stable app.
    2. Simplicity - from experience, setting up and using Hilt (unlike the underlying Dagger) is considerably easier than using Koin. Hilt also introduces fewer breaking changes over time.

Links

Clean Architecture for Android on Amazon

Clean Architecture on the Clean Coder Blog

cleanarchitectureforandroid's People

Contributors

eranboudjnah avatar

Stargazers

Dennis Huaman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.