Modular Android App Architecture (Build to scale)
Hi Everyone š I am writing this blog, to sum up, my two cents on this Most heard combo of words āModular App Architectureā
One way or another we have come across these words, And a few years back it made me curious- is it seriously different from the basic architecture we use, or is it something else or what not!!
Updated Implementation is here:
before we start further, we would like to tell you that we just launched our Youtube channel and for more of our latest learnings on some interesting Android&iOS topics, you can connect to us on our brand new Native Mobile Bits youtube channel
āNow I am using this from many years and I feel happy with the benefits it gives, and today while I wait for speaking on this topic in a meetup I thought to sum up my experience on this on my blog as well.ā
*What is Modular App?
Literally, it's just an App with few modules.
*Ah! What is a Module?
A Module is basically a small part of something big
i.e. there can be multiple independent small units that can form a Program.
Okay, letās go through it again in simple termsā¦
eg.) We all love Avengers right! but what is it?
It's a group of different people called Avenger.
Here A single Avenger can be anyone Tony Stark, Hawkeye, Dr.Strange and they all do some specific thing right but when they come together they are a team and that team is called Avengers.
same way, There can be N number of independent Modules and each module can be used for some specific task like login or onboarding but when all of these modules we merge, that will form as a single unit an App (a beautiful one).
*Okay got it!! What kind of Modules are there in An Android App then?
In Android development we can create these kinds of Modules for us:
- Library Module
- Feature Module
- App Module
Library Module- It can be a third party or our own functionality developed which is kind of a utility for our main project. we can use it in multiple places in our project
Feature Module- In our project, we can divide functionality or flow into small units as a separate independent feature.
App Module- This is our Wholesome product, it will combines all the feature modules we developed and form a single entity
*What are the advantages of using this approach?
Modularisation comes up with many benefits :
- Every tiny unit is Independent inside our App
- This way when we have independent modules there will be loose coupling, we can easily change or test any specific flow.
- We can use any module across the app code that will be reusable and in case we want to change anything just change it in one place and reflect everywhere(in case needed).
- Speed up build system as Gradle can build modules in parallel and only build the module where code is changes and use cache.
- Gives us a solid development culture each member can work on something specific.
- We can do experiments too with this approach, for eg.) if we want to build some modules in java & most of them in Kotlin or vice versa this is the coolest way to do this
- We can use one module in another module by adding it as a dependency and without messing up so much code.
*Okay Sweet! let's see some code?
For explaining this concept I will be making a simple FunFacts application that will be having two modules one for Cat lovers and one for Dog lovers.
As you see in the above picture, In our basic modular application we will be having two Modules and in each module, we will have some network operations for that we will make one central networking module.
What we will use in this App :
Modular architecture, Dependency Injections HILT, MVVM, Coroutines, Lottie, Jetpack components central navigation and buildSrc for central dependencies management, Kotlin.
Letās start
1. App Network Module: (āCentral Moduleā) I call it
So we will start by creating a simple android project then I have added all network-related retrofit interceptors in a module called central.
Inside This central
module you can see this below structure here:
Here I have a DataSourceModule
it will have all the dependencies for Retrofit instances, and as in this App, we will be using two public APIs so I will be creating two different retrofit Instances here.
then I have an API service DogFactsService
interface it will contain all the API calls related to the DogFacts module.
āSimilar way I will create a different service for each module, and In our Dependency Graph, we will create respective Retrofit instances for each Service using the respective
DataSource
for thatservice
.ā
This way we will be having all network-related APIs stuff at one place and inside this place as well, we will be having separate API services
and their respective datasources
DataSource
is having the suspend functions for API calls which will be called from respective repository
and interlink API services
with their implementation.
2. ābuildSrcā ( a veronica to manage API/third party dependencies)
There is also a directory buildSrc that has all the dependencies used in our App, we can create any modules and use any third-party or Android API we can define here at one place and we can use inside all the modules it will help us maintaining similar dependencies versions throughout.
3. Let's create a new module āDogFactsā and all its related UIs or logic.
Step1.) we can create a new module inside any project like this:
Step2.) Once we do these above steps and sync our project we will see the project structure like this now.
Step3.) Now in our projectās settings.gradle.kts
file, we can see this included module
Step4.) Now as we need this module as a dependency for our main App
module so we can register this dogFunFacts
as a dependency in our buildSrc
central Dependencies.kt
file
Step5.) Now we have a new module we have also created it as a usable dependency, now we just need to use it in our App for that, we need to add it inside our Main App Module
as a dependency like this:
And Bingo :)
we have created a Feature Module šš now we can code for this feature independently here and this can be accessed inside our App anywhere.
Output App will look like this:
Full Source Code Link:
Thanks for staying till the end here with me, Hope you find this article helpful!!
Do hit the š to show some ā¤ļø
See you next time :) till then ābe you be happy + happy codingā
For more of our latest learnings on some interesting Android&iOS topics, you can connect to us on our brand new Native Mobile Bits youtube channel or LinkedIn & Github.
and on a similar note, Here is the next step from here- How we can use Progruard in our Multi-Modular App.šš
and our latest article on Android12