Kotlin Clean Architecture with MVVM prototype app for Android

We as developers are always in the search for better ways to build our apps. The need to build flexible to adapt high quality software usually comes from constantly changing business requirements. To do that our goal should be to write decoupled, maintainable and testable code as much as we can. That depends a lot on how we organise our code base, what architecture we use.

Prototype app

To study this concept I have build a simple single screen demonstration native Android app based on Clean Architecture with MVVM written using Kotlin programming language.

It’s on GitHub

Prototype app “VenuesNearby”

What is MVVM architecture?

  • Model: This represents the data and business logic of the app. It cannot directly talk to the View. Generally, it’s recommended to expose data through observables to be decoupled completely from ViewModel.
  • View: It represents the UI of the application. The view role in this pattern is to observe (subscribe to) a ViewModel observable to get data in order to update UI elements accordingly.
  • ViewModel: It acts as a link between the Model and the View. It’s responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View. It will ask for the data from the Model. ViewModel should not be aware about the view who is interacting with.

MVVM pattern is recommended one by Google as it is supported by Android Jetpack Architecture Components.

What is Clean Architecture?

Clean Architecture describes the overall application architecture: how the various layers of an app (business objects, use cases, presenters, data storage, and UI) communicate with one another. With it, code is separated into layers. The layers should not know anything about the outer layers. They can depend only on other inner layers for them.

Following Clean Architecture idea all project code should be separated into different layers such as domain, storage, presentation. However the architecture doesn’t strictly define exact layers and you should adapt the number of layers to your needs.

My demo project VenuesNearby was separated to these six layers — packages:

Domain — a layer to store the business logic of your app. Here we have our Use Case classes — the operations which your app can perform. This layer also contain Unit tests for these classes.

Data — a layer which is used to control the flow of data between the remote (external data) and cache (local data) layers through to the Domain layers operational requests. This layer contains Unit tests to ensure that this control of data is operating as intended.

Remote — a layer which contains all of the remote operations with the third party API that is used for the project. Unit tests here to ensure that these operations are working as intended.

Cache — a layer which stores our applications data in a local database using the Room architecture component. This allows our app to appear working at all time even while being offline.

Presentation — a layer to house the presentation logic of our application. MVVM pattern is used in this layer and built with lifecycle-aware architecture components such as ViewModel, LiveData. Also view model class is covered by own tests.

UI — a layer contains the user interface for our mobile application. Here an activity is used to display the data. Espresso tests ensures the operation as intended.

Clean Architecture is the most modulated app development architecture which is perfect for continuously developed app and team work. Besides it allows to build on top on other efficient architectures such as MVVM.

Advantages:

  • Clear separation of concerns.
  • Decoupled responsibilities good for team work.
  • Easier to maintain.
  • Code more testable.
  • Works well for large complex projects so it can scale.

Final words

Ačiū! Thanks for reading! I originally published this post for my personal blog www.baruckis.com on December 22, 2019.

Android Developer 👨‍💻 baruckis.com | Co-Founder devsengine.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store