Android Clean Architechture from A – Z (Part 1).

Tram Ho

Clean Architechture.

Table of contents:

  1. A story of his past.
  2. General introduction about Clean Architechture.
  3. How is Android implements Clean Architechture?
  4. Apply Clean Architechture to a specific Android application.
  5. Pros and cons, compare it with MVVM, MVP.

1. A story of his past.

Thinking back to a year ago, I would have named myself GOD. Why is that? Because I am devising all applications in the form of “GOD Activity”, an activity that will do all the work of the world from logic, call api, catch data db local … (and I think regardless of us when we first started learning Android has been the same GOD ? ). Then I thought that if the code worked in the same way, the people called Senior would not be much different because both the code for the application ran. Incidentally, I went to Reddit and saw a person asking such a question:

_ “I’m a Junior, I want to become a Senior, I need to learn more RxJava or Dagger”

I don’t know how this person can determine their level is Junior ? . But there is a comment in that post that I have not forgotten until now and that comment has changed me (a special thing is a comment of Vietnamese people):

_ “None of them matters, you are an engineer, learn the Architechture first”.

When building a house it is important to build the architecture of the house, not build any kind of sand or cement. The same goes for an Android app, the most important one is its architecture and the ones like RxJava or Dagger are essentially like cement or sand in building houses, it’s like the plugins that we just injected. Go to the Architechture. If you don’t use Dagger, you can use Koin instead. If you don’t use RxJava, you can use Coroutines. Regardless of whether an Android application is large or small, Architechture is needed because a simple thing Architechture makes our application easy to “Extend”, “Maintain”, “Unit-Test”. And today I would like to introduce an Architechture in Android that satisfies all the above conditions is Clean Architechture.

2. Introduction to Clean Architechture.

_ Clean Architechture is an architectural stratified application model. The application will be divided into separate layers (this is a theory translated to me, it’s just something to read for fun and extremely confusing. ? ). Below is a drawing of the Clean Architechture in general, which any document will include when talking about Clean Architechture – these are 4 concentric circles. I will explain in a way that I understand without following any online material that I have read before. Note that this is a drawing of the general Clean Architechture (it applies to other platforms not only Android) but not the drawing to apply to Android, so I just say the most comprehensive. As for the Clean Architechture how Android is implemented, I will detail in the following.

• Some principles that pictures want to show:

_ A circle with a color corresponding to a layer

_ Components containing code unrelated to the framework will lie within the center of the circle (specifically here is the Enterprise Business Rules & Application Business Rules layer). Many of you may be wondering, “What is code not related to framwork?” I would like to briefly explain like this: If you use Android to implement, the code at these two levels requires it to be pure Java or Kotlin 100%, if any Android framework code appears on this layer ( For example: Retrofit, Realm, Room ….) then your organization of Clean Architechture failed ^^.

_ Push the components dependent on the framework (can be understood here as the UI of the WEB, IOS, Android ..) as far as the center of the circle as possible (in the picture is the Frameworks & Drivers – the blue circle) .

_ Interface Adapters: The middle layer is responsible for communicating between framework-dependent components and non-framework-dependent components (here is usually the request data and returns the framework to show to the view).

• A few questions are asked:

_ Why organize the positions of such layers? -> Organize such layers to follow the DEPENDENT LAW that Clean Archiechture says:

  1. The dependencies of the classes will be in the center of the circle (Frameworks & Drivers -> Interface Adapters -> Application Business Rules -> Enterprise Business Rules). It is this law of dependency that makes any circle when changing it will not affect smaller circles than it, but it will affect the circle bigger than it. For example, the code in the “Interface adapter” circle changes so that the smaller circles “Application Business Rules” and “Enterprise Business Rules” will not be affected. But the larger circle of “Frameworks & Drivers” will be affected. So when we organize the Clean Architechture, we have to push it as far away from the center of the circle as possible (particularly here are framework dependent components ^^). Usecases that change very little or almost never change it will be closest to the center of the circle.
  2. At any given circle, it is only allowed to use objects of the smaller circle. It is not allowed to use objects of the circle bigger or its counterpart.

_ When the actual application of Clean Architechture always has to follow 4 layers as shown in the picture. -> Answer NO. Depending on the system, there will be a number of different classes if your system is small, not necessarily separated from entities and usecase. The most important thing is that the system must follow the rule as mentioned above which is the AUTHORITY LAW.

Part 1 please stop here. Thanks to everyone for taking the time to read your article ?

Share the news now

Source : Viblo