Android — Clean Architecture 101
Good practices and clean architecture has became truly important in software development to build scalable and maintainable apps. Now in Android we can also apply this strategies to build greatest and well-developed apps by following clean architecture, splitting our business logic in modules and layers.
Clean Architecture — Layers
The approach of clean architecture is decouple and split our app logic into layers that are going to keep communicated. This way we can separate business logic and app logic from view, frameworks and data handling. Let’s explain the layers but in android development as context.
UI / External interfaces
This layer is on top of all of them, and this is focus on user interaction, here you can find all related to views, so, making translation to android development, it refers to Activities and Fragments.
Presentation
A good practice is to keep view and data separated through an intermediate class. Presentation layer is going to be responsible for that, here are going to be defined all classes that keep communication, like Controllers, Presenters, ViewModels…
Uses cases
App has to consume data and perform actions to interact with databases and services. In this layer are defined those business actions that can be triggered, for example, update user profile, fetch list of products or delete a shopping cart. That is for what a use case class works, and this one triggers actions to access data, performing calls to specific repositories.
Data / Entities
This layer encapsulates data handling and here is where data sources are implemented (data) and models (entities) are defined. For example:
UserRemoteDataSource, ProductLocalDataSource, User, Product.
Why should I care about it?
Imagine that you are going to develop an app for a bank company, this business model involves several kind of transactional features like deposit and withdraw money, manage accounts and cards and also be able to send money to others accounts. That’s a lot of logic and data flow to handle. Now let’s assume that you can send money through different entry points of the app, this means that send money feature is going to be used in different app sections. Apply clean architecture is going to allow you split the view from the business logic, but also split business logic in layers like domain and data. So, doesn’t matter how many screens implement send money feature, you can access data and perform actions of that related business logic through presentation layer. This is just an example but there’s another benefits of applying clean architecture in your apps. Keep your app splitted in layers and modules is going to facilitate test implementation, maintainability and gets scalable.
I have worked in several projects, in different companies, and I saw the big difference between a project where the team really cares about architecture and design, and in other hand, a project where nobody cares about it. I realized that company that doesn’t take importance in good architecture, spend a lot of time, and money, just fixing bugs and trying to solve its own mistakes
and when a new feature was coming, team suffered headaches just thinking about issues that probably would appear, because the project wasn’t designed to be scalable.
Yeah yeah, let’s be pragmatic…
Practice makes the master — Patrick Rothfuss
Let’s make a basic movie reviews app to show an example of how communication works in clean architecture.
1 — Create project in Android Studio
First open Android Studio and create a new empty project. Let’s naming MovieReviews
2 — Define directory structure
Now let’s define directory structure for our project creating the packages ui, data, domain.
As it was explained before, ui package will contains Activities, Fragments and also its related ViewModels or Presenters. Into data package there’ll exists data source and repository classes, and finally, into domain package we are going to find use cases and model classes.
3 — Add needed views
We are going to add our activities and fragments into ui package, also all related to UI as ViewModels, Presenters, Adapters or UIActions.
- SplashActivity
- HomeActivity
- DetailedMovieActivity
4 — Create models
We need to create our movie model class into domain package, here are going to persist entities related to business model.
5 — Create repositories and data sources
Our movie data source and repository are going to be added into data package. DataSource is usually an interface where calls are defined, and repository has to implement it.
6 — Create use cases
Now let’s add our use cases into domain package. As we said before, each use case is related to some specific business action.
7 — Integrate layers
Now, we need to integrate the UI with business logic. So, the call flow should work in the next way:
The Activity or Fragment is going to request data or perform actions by calling ViewModel or Presenter, and this last one is going to keep communication with use cases, so for this example the use cases will be triggered from ViewModel, then retrieved data has to be exposed to UI, in this case by LiveData.
And use cases are going to keep communication with repositories.
Data request and fetching logic is implemented into repository, for instance, retrofit calls, graphql queries or local database access.
This way UI can perform actions without having knowledge about the behind logic, just waiting for response to react on screen.
Conclusion
Clean architecture implementation is essential nowadays to ensure code quality and business scalability. Also better practices and clean code principles are part of good developer roadmap, no matter your technical area of development, these practices are going to help you to grow up. The movies project exposed here is just to see an example of directory structure and definition, and how classes and entities keep communicated to perform actions, however, you can always make it better and improve your development by integrating design patterns and other useful tools.
Thanks for reading!