Advance Architecture for Android Project
Best way to create advanced android projects using the latest android architecture and components.
Intro
In this blog, we will discuss a simple app (NavArch) using advanced android architecture. NavArch will show a list of pokemon names. Before diving into deep, I hope you are already familiar with these topics: Retrofit, Dependy injection (Hilt), Jetpack components, MVVM.
In this app, we have used android's latest architectural components like Jetpack Navigation, ViewModel, Databinding, Live data, Hilt, and Single activity structure.
Architecture
We have used Google’s recommended app architecture, where each application should have at least two layers:
- UI Layer (Presentation Layer)
- Data Layer (Repository Layer)
NavArch consists of only two layers UI layer and the Data layer. we are not focusing on the Domain layer. let’s dive deep.
UI Layer
The UI layer is the presentation layer that takes care of displaying data on the screen and handling UI elements and State. (Views, Texts, Buttons, Adapters, etc.)
Also, all the UI logic and validation will be handled in this layer. This operation can be shared between the Activity/Fragment and ViewModel.
This layer is made up of two things:
- UI Elements (Activity / Fragments / Data Binding): will perform UI operations, displaying content, validation, and getting input from the user.
- UI State Holder (Viewmodel): This will hold the UI state, handle UI logic, and holds all the data objects.
Data Layer
The Data layer is responsible for handling all the business logic. This layer is made up of two things:
- Repositories: This class is responsible for handling business logic, exposing data, fetching data from multiple data sources, and processing.
- Data Sources: Data source class is responsible for fetching data from multiple sources like API Service and Local DB.
- eg: Fetching data from Retrofit call or Room DB.
Activity/Fragment will only communicate with ViewModel, ViewModel will get the processed data from the Repository class.
Repository will fetch data from multiple data source classes, then validate the data and return it to ViewModel.
Datasource class will get the data from the source and send it back to the Repository.
Hope you are clear with these Architecture Layers, OK let's discuss this approach in our NavArch app.
Project (NavArch)
- “NavArch” app will contain a simple list(Pokemon) of data to understand the Advance architecture design.
- In this app, we have used free Pokeapi to get the pokemon list data.
- Ok, let's see it in detail.
project packages
- NavArch will contain the following package structure. (you can also create an individual module for each layer)
1) UI
- UI package will be responsible for handling the UI layer operations. All the Activities, Fragments, ViewModels, UI classes, and Adapter classes will be placed inside this package.
- Every UI flow will be added to this package.
- eg: launcher, home, view.
2) Data
- This package holds API service, Repository, Model, and DB classes.
api
- The API package will handle the API service logic and operations. It will have API request/response classes, Retrofit API service, and BaseRemoteDataSource class.
- ApiService - Interface class to handle HTTP calls and responses.
- BaseRemoteDataSource - This class will load the response from API and convert it into a Result Object. (all the responses will be encapsulated as a Result object).
model
- This package holds all the model (POJO) classes of the project.
- eg: Pokemon
repository
- Every business logic and operation have been handled inside this package. This package holds all the repository classes for each flow.
- eg: LoginRepository
3) DI
- This package holds the dependency injection(Hilt) base module classes.
- eg: NetworkModule - This DI class will provide a Retrofit API instance.
4) Common
- This package holds common classes like Constants, Utils, Helper, and Receivers.
5) Base
- This package has base classes like BaseActivity, BaseFragments, BaseSharePref, and MainApplication(Hilt).
Hope you are clear with these packages and their scope.
After creating all the packages and classes, your project structure should be like this.
Project Repo
NavArch implements an Advance android architecture and contains a Launcher page and Home page. Pokemon data is fetched from API and updated on recycler view in HomePage.
Please find the project Github repo here, hope you can get a detailed view of discussed topics.
Wrapping Up
Wow great 🎉, In this blog, we have learned about how to create an android app using Advance Architecture and components. Hope this blog is helpful.
This article helped you? Long press on the 👏 button as long as you can.
I got something wrong? Mention it in the comments. I would love to improve.
I am NavinKumar, Android and Flutter developer. You can find me on Linkedin or stalk me on GitHub or Follow me on Medium