1. 程式人生 > >Clean architecture for Android with Kotlin: a pragmatic approach

Clean architecture for Android with Kotlin: a pragmatic approach

Clean architecture is a topic that never gets old in the Android world, and from the comments and questions I receive, I feel it’s still not very clear.

I know there are tens (or probably hundreds) of articles related to clean architecture, but here I wanted to give a more pragmatic/simplistic approach

that can help in the first incursion to the clean architecture. That’s why I’ll be omitting concepts that may feel unavoidable to architecture purists.

My only goal here is that you understand what I consider the main (and most complicated) topic in clean architecture: the dependency inversion. Once you get that, you can go to other articles to fill in the little gaps that I may have left outside.

Clean architecture: why should I care?

Even if you decide not to use architectures in your Apps, I think that learning them is really interesting, because they will help you understand important programming and OOP concepts.

Architectures allow decoupling different units of your code in an organized manner

. That way the code gets easier to understand, modify and test.

But complex architectures, like the pure clean architecture, can also bring the opposite effect: decoupling your code also means creating lots of boundaries, models, data transformations… that may end up increasing the learning curve of your code to a point where it wouldn’t be worth it.

So, as you should do with everything you learn, try it in the real world and decide what level of complexity you want to introduce. It will depend on the team, the size of the App, the kind of problems it solves…

So let’s start! First, let’s define the layers that our App will use.

Want to learn Kotlin?

Check my free guide to create your first project in 15 minutes!

The layers for a clean architecture

You can see different approaches from different people. But for simplicity, we’re sticking to 5 layers (it’s complex enough anyway