When developers are working on high-complexity android applications, they need considerable skills and experience. The app should not only meet the customer’s requirements but should also be flexible, testable and maintainable. The Clean Architecture Concept, developed by Robert C. Martin in the year 2012, is capable of meeting these challenges.
Developers are often asked whether they would prefer to add a new feature to an app that has awful architecture but works well, or would they prefer an app that is buggy but has good architecture? Most of the times, developers choose the second option because adding a new feature can become very time-consuming in an app that has dependencies from everything in every class. However, if the app has a clean architecture, there are minimal dependencies and hence it is much easier to add a feature.
In this article, you will get to know the basics of a clean architecture concept for an Android app. The knowledge can be applied to every platform and language.
Features of Clean Architecture:
A mobile application that has been made with clean architecture will have the following characteristics:
- It will be testable.
- The user interface can be easily modified without changing the system.
- It will be independent of databases, frameworks, libraries and external dependencies.
However, the rule of dependency is the overriding rule that will make clean architecture work. The rule implies that nothing in an inner circle should be dependent on anything in the outer circle. Which means that application and business rules will not be dependent on the user interface, database, or the presenters. The dependency rule allows developers to build systems that are simple to maintain, because changes in the outer circle will not impact the inner circle ones.
Given below is the original clean architecture scheme suggested by Robert C. Martin
It may be difficult for some of you to understand, so given below is another rendition of the scheme, presented from UI the backend or database:
Detailed below are definitions of terms used in the image above, so that you can get more familiar with them:
Entities: They are enterprise-level business rules that encompass the general rules concerning Data Transfer Objects (DTOs). Whenever something changes externally, these rules are most unlikely to change.
Use Case: These are also called interactors, and they imply application-specific business rules of the software. This layer is not affected by changes to the database, common frameworks, or the user interface.
Interface Adapters: They convert data from a convenient format for entities and use cases, to a format that can be applied to databases and the web. This layer will include Presenters from MVP, ViewModel from MVVM, and Repositories.
Frameworks and Drivers: These are the outermost layers that consist of web framework, database, user interface, and HTTP client.
List of Technologies Used:
- Kotlin: A statically typed programming language that is rated first-class by Google, for writing Android apps. It is suitable for multi-platform apps.
- Dagger 2: An injection framework for Java and Android, which is fully static and has compile-time dependency.
- RxJava 2: Reactive Extensions for JVM, which act as a library for composing asynchronous and event-based programs which use observable sequences for Java Virtual Machine.
- Retrofit 2: It is a type-safe HTTP client that works for Android and Java.
Here’s a list of Android architecture components that will also be used in the application, as part of the clean architecture approach.
- Room: It provides an abstraction layer over SQLite, which will allow a robust database access. At the same time, it will also harness the full power of SQLite.
- ViewModel: It is designed to not only store but also manage the user interface data in a way that is life-cycle sensitive.
- LiveData: It is an observable data holder class that is life-cycle sensitive, dissimilar from a regular observable. It respects the life-cycle of other components of the application, including its activities, fragments, and services.
- Paging Library: It makes easier for developers to load data gradually and efficiently, using the app’s RecyclerView
As you can understand, clean architecture is a great solution to build high & medium-complexity applications. The approach is compatible with MVP and MVVM, and works very well with Android architecture components. By using this architecture, you can also build simpler applications.