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 accomplish discussed goal we can combine Model-View-ViewModel (MVVM) software architectural pattern together with the concept of Clean Architecture. Applying clean architecture concept is much better way to develop apps. With it changes can be done quickly, code can be understood better as its readability improves and development costs are reduced. Testability improves drastically. It becomes easy to find bugs.

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:

VenuesNearby – is a demonstration app that allows a user to enter a place name, submit, and then view the recommended venues nearby. The venues are provided by the Foursquare API which you can integrate with. You will need to sign up as a Foursquare developer and create an app in order to use their services.

Prototype app “VenuesNearby”

What is MVVM architecture?

MVVM is a software architectural pattern. It stands for Model–View–ViewModel.

  • 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 exists at a higher abstraction level than the MVVM presentation architecture pattern. Usually MVVM is enough to use for small projects, but when our project codebase expands and becomes huge we need something better for separating all the responsibilities. Clean Architecture is enforcing separation of concerns and SOLID principles. Actually these principles is core concept for Clean Architecture. Check out my previous article about SOLID.

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

In this article I have just quickly covered the concepts of Clean Architecture with MVVM. Please understand that we don’t necessary need to have any architecture at all for our apps development. The app can be completely made without it and the end user wont notice any visual difference. But I hope that when your boss, your client or your colleague will ask whats the point of it, you won’t doubt, be confident and explain all the benefits it brings. The biggest difference here is always going to be the long-term effect architecture it gives. At last in Android development word we have not only the professional tools provided but also matured blueprint for building highest quality apps. There have been no better time to try building such one by your own.    

This post was also republished on Medium. Show your support by clicking the clap button 👏 on the story page here. 😇🙏

Share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.