Open In App

What is Clean Architecture in Android?

Last Updated : 19 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

With the growing period of technology, it is critical to get the architecture right if you want to build a fantastic software program, which is why it’s so essential. If you’re interested in creating android apps with the help of clean architecture, then you have arrived at the right place. Here, we aren’t just providing you the MVVM (Model View-View model) tutorial, but we will be using MVVM for connecting it through a Clean Architecture. This article will look at how to leverage this design to create decoupled, tested, and maintainable code. Before we jump into our main discussion, let’s understand MVVM first.

MVVM

The Model-View-ViewModel (MVVM) is a software design framework that separates program logic from user interface controls. It was introduced by two well-known Microsoft architects Ken Cooper, and John Gossman, in 2005. Besides, MVVM is also known as a model-view-binder. Like many other design patterns, MVVM aids in the organization of code and the separation of programs into modules, making code development, update, and reuse easier and faster. The design is often used in Windows and web graphics presentation software. The Windows Presentation Foundation (WPF) runs on Microsoft’s .NET platform, employs the MVVM design. MVVM is also used by Silverlight, a Microsoft WPF internet comparable multimedia plug-in.  

Clean Architecture  in Android

Clean architecture is a method of software development in which you should be able to identify what a program performs merely by looking at its source code. The programming language, hardware, and software libraries needed to achieve the program’s goal should be rendered obsolete. Like other software design philosophies, Clean architecture aims to provide a cost-effective process for developing quality code that performs better, is easier to alter, and has fewer dependencies. Robert C. Martin established clean architecture and promoted it on his blog, Uncle Bob, in 2011.

Well, you must be wondering Why Architecture is essential? Let’s dig inside. The purpose of all architectures is to manage the complexity of your application. On a minor project, you may not need to worry about it, but it can be a lifesaver on larger ones. I believe you may have already seen this picture.

Clean-Architecture-in-Android

The above rings represent diverse levels of software in your app. Before we move ahead, there are two crucial points to keep in mind:

  1. The most abstract circle is in the center, while the most concrete ring is on the outside. The Abstraction Principle is what this is referred to as. According to the Abstraction Principle, inner circles should include business logic, whereas outer rings should contain implementation details.
  2. The Dependency Rule is another Clean Architecture principle. This rule states that each circle can only rely on the inner circle closest to it, allowing the architecture to function.

Perhaps, now is the time to understand Why MVVM with a Clean Architecture?

MVVM with Clean Architecture

Your view, i.e. (Activity and Fragments), is separated from your business logic using MVVM. For small projects, MVVM is sufficient, but as your codebase grows more extensive, your ViewModels begin to bloat. It becomes challenging to separate responsibilities. In such instances, MVVM with Clean Architecture is a suitable choice. It goes a step further in segregating your code base’s responsibilities. The logic of the actions that can be performed in your app is abstracted. Now, Clean Architecture can also be combined with the Model-View-Presenter (MVP) architecture. So, we’re going with MVVM over MVP because Android Architecture Components already has a built-in ViewModel class—no MVVM framework necessary!

Before we move ahead, you need to be clear that using Clean Architecture can have its Advantages & Disadvantages. Let’s check it out one by one.

Advantage of Clean Architecture

  • Code is easy to test than with standard MVVM.
  • Perfectly curated separation (the most considerable advantage).
  • User-friendly package structure.
  • Easy to keep the project running.
  • Your team will be able to implement new features even faster.

Disadvantages of Clean Architecture

  • The learning curve is a little steep. It may take some time to learn how all levels interact, especially if you’re coming from architectures like simple MVVM or MVP.
  • It contains many extra classes, so it’s not suitable for applications with a low level of sophistication.

Now, let’s check out the Layers involved in this process.

Combining MVVM with Clean Architecture

Below, we’re showing you the graph representation of MVVM with Clean Architecture.

Point to Remember:

  • Only the outer layers may rely on the inner layers for communication.
  • The amount of layers is entirely up to you: Make it fit your requirements.
  • In intimate circles, things grow more abstract.

Layers of MVVM  

There are differing viewpoints on the number of levels that Clean Architecture should have. The design does not specify separate layers but instead lays the groundwork. The concept is that you adjust the number of layers according to your need. Here, to keep it short, we will use five, have a look at them.

  • Presentation: This is a layer that interacts with the (UI)user interface.
  • Domain: The app’s business logic is stored here.
  • Use cases: Interactors are another name for them.
  • Data: All data sources are defined in a broad sense.
  • Framework: Implements interface with the Android SDK as well as concrete data layer implementations.

When to use Clean Architecture?

It’s crucial to remember that clean architecture isn’t a remedy but can be applied to any platform. Based on the project, you should determine if it meets your requirements. If your project is large and sophisticated, with a lot of business logic, for example, Clean architecture has many advantages. However, for smaller and simpler tasks, the benefits may not be worth it — you’ll end up writing more code and increasing complexity with all the layers, as well as investing more time.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads