Modularize Android Development

Recently I have started with some android development for my new start sameepa.lk. The project is for connecting consumers directly with nearby small businesses and farmers to make sales process more effective. The project is using Java Spring boot backend and native android development. Choose Digital Ocean as cost effective cloud solutions for deploying them over Google cloud and AWS. The Android front end project codebase started small but grown faster and layout folder looked messy with hundreds of files unorganized. So wanted to bring modular approach I uses in other projects I apply in my React JS, PHP and other projects.

We all grow up in mindset to modularized by technology not by reuse. For example when you build a web application you place controllers in one folder, models in another and styles in another etc. This way of organizing will look nice within the project. But assume you are going to reuse one of the functionality in another project and you will feel the pain to separate out relevant code for particular module from entire structure. Modularization should be for easy reuse systems are smart enough to build the code by putting the output in appropriate location for you to run. So you setup the build files correctly to generate the output structure you want for entire project but organize the codebase in a way where you could easily reuse within the projects.

So coming back to the topic in most of the big android projects you will see the layout folder will have hundreds of files. So I did a quick search how I can better organize them so that I can reach faster to edit them and easier to reuse the same in other projects and wanted share what I learnt there. Android already have a feature to organize resource in different way to the default one.

Layout folder in most of the projects

For organizing the code by module first create a resource directory for each modules.

Creating resource directory in Android
Give module name and create the directory for module

By default android creates the module directory under the res folder but you are free to organize. I most of the time to move one level and keep parallel to res directory. When creating the resource directory android studio edits the build.gradle file automatically. If you change the path of the resource directory you will have to edit the file to match the path as shown below.

Mapping resource directories in build file

Once this step is complete you can use the module resource directory in same way you use use res directory and place the files relevant to the module. This will allow better organization of the code and copy paste reuse across projects.

Here is the end of this post. Please share if this post helped you. Have a happy coding.

Leave a Comment