Modularization in Android: Make your code reusable and maintainable . (Beginner)

Shalom Mathews
4 min readJul 19, 2020

--

Before I start the article I would like to mention that this is my first medium article . I would love to hear your views about it!

So let's get started .

Why the need for this article ?

As an Android developer i've realized as a project grows the code becomes difficult to handle . Most of the tutorials for an Android application consist of a single app module which is the cause of this issue.

Normal application module’s.

A normal Android app modules.

Disadvantages of this approach:

Reusability : Imagine you write the login feature for an application A in its app module and then comes another project B which requires the same login feature . Lot of refactor would be needed wouldn't it?

Maintainability : When developers write code in one application’s main module and then copy that code to another project the developers creates two instances which would mean that if a bug arose in the fix would need to happen at two places .And since we developers have attention spans of an ant we are bound to forget .

So how do we improve this ?

Modularization:

Modular architecture

The above image explains how we can create modules for login and common utils which can be used by application A and application B.

Advantages :

Reusability : The modules are completely reusable . An important point to note is modules force the developer code which is more flexible by putting constraints on the developer which are normally not present while writing code in a single module application.

Co-operation : Modules can enable developers with little Android experience to write code for an application. For example if we needed to write a complex image manipulation operation in a lower level language we could do so using the developer in the team who has the most experience in image manipulation .The developer just writes the code for the module without worrying about how it is used.

Build : We can build individual modules rather than building the whole app module everytime .

Individual builds

Testing: Testing can be improved by writing test for specific modules.

Maintainability : Decoupled code is far more maintainable.

So how do you create a module?

Code

I will be creating a simple 2 module application which will have one app module and a login module .

The login module will contain code which will enable the user to select either select Sign-in or Register .

Start by creating a new Basic Activity Project . File > New > New Project.

Create a new module File > New > New Module and then select Android library .We selected Android library as our module will contain some UI too.I choose to name the module login.

login module

Once we added the module we need to import the libraries required for the module . If we notice in the image there is build.gradle file for module login.We add the libraries in there.

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

We also need to create the directory structure to store our layout’s and string’s.

login module directory structure

Once done we need to create the LoginBottomSheet.kt which is the bottom sheet and the UI xml file. I am not going to explain the code for that simply because easy and it would increase the size of this article considerably.

The most important step is to import the login module to our app module .

We can do this by adding the code below to the build.gradle file of the app module.

implementation  project(":login")

Once this is done we use the login module anywhere .

For a test case i just imported the login module into the FirstFragment.kt .

login_button.setOnClickListener {
loginBottomSheet = LoginBottomSheet(this)
loginBottomSheet.show(childFragmentManager, "login_sheet")
}

The result should be the login bottom sheet from the login module would be shown.

Login bottom sheet from login module

And we have successfully created a simple login module!

Code : https://github.com/shalom06/MultiModule

Thanks for reading!

If you guys need a more advanced tutorial let me know!

--

--