Modular way of building Android apps

A. A.
ProAndroidDev
Published in
5 min readApr 2, 2019

--

Having only one module in an Android app is very common and many projects are structured this way. Maybe because Android studio creates one module by default (app module) and many people don’t even think about it.

In contrast, we can construct our app to use multiple modules to work together to make the app. When we add libraries to build.gradle file, we are using other peoples modules in our code. We can do the same for our own code as well.

What are the benefits of modular development?

  • Maintainance of the project gets easier
    When we add more and more code to a project, the project gets more and more complicated and as a result, maintaining the project gets harder, adding new features require more time and energy and fixing bugs get cumbersome. In fact, this is not even a linear relationship but rather an exponential one! So more code equals more complexity equals more headaches!
    Using a modular approach greatly reduces this problem because instead of having one big project we have multiple smaller one that easier to manage.
  • Modules can be reused
    If you have more than one project, it is very likely that some code is repeated between them, by using a modular approach you can write your code just once and use them as many times as you want. This will hugely decrease the development time, especially when you want to make some changes to the code.
  • You write better code and better APIs
    Working with modular approach forces you to think more about the code and relationship between the different parts, hence letting you write better code.
  • You can take advantage of open source community
    You probably don’t want to share your whole project on the internet and even if you do that may not be very helpful to other developers but you can share the parts of your project that are more general, like the part that crops the photo or uploads files or whatever. By doing this you get all the benefits of open sourcing without exposing your project or your idea to the world.

There are three different ways we can add modules to our projects.

Adding modules directly in our project

We can simply add as many modules as we want to a single project. In Android Studio it is as simple as going through File -> New -> New Module

This way all the modules are inside the same project and usually one of them (app modules) uses the others. The advantage of this approach is, it’s super easy to edit the modules and see the result instantly. However, the main reason we want to go with modularization is to have a smaller more manageable projects that are easier to maintain and this approach doesn’t provide that since all the files are still in the same project. Not only that, we can not reuse these modules in other projects. So I don’t recommend doing it this way and this is not what I mean by modular development!

Developing modules as separate libraries

We can create our modules separately in their own project, upload them on a server and then use them like any other library by adding a dependency to build.gradle file. There are many online services to host your libraries publicly or privately. The most famous one is Bintray.com. You can also host your own server if you want.

I like this approach a lot better since our project is split in multiple smaller more manageable projects and we can developed a module (library) once, and use it in as many projects as we want and hence radically reduce the development time.

This approach has a big disadvantage though! Each time we want to make a change in the module we have to do the change the code, test it, build the new version of the library and publish it on the server and update the build.gradle on the main module to see the effect. This is really time consuming and frustrating. So unless the library is your main project I don’t recommend this either.

Using gradle composite build

Gradle composite build is my favourite feature of gradle witch bring the best of both worlds together! And this is what I recommend for doing modular development. The way it works is very simple. While developing, composite build uses your local modules letting you make changes as you would in a single module project and see the result instantly (like solution one) while the modules are developed separately in their own projects and can be developed, published and used separately. (like solution two)

Configuring composite build is simple as well. You just have to edit settings.gradle file and let the gradle know which libraries you want to replace with local versions while developing. Here is an example from one of my projects.

It code simply says if the project directory on ../..Android-Lib/ProgressButton exists use it instead of the dependency com.alirezaahmadi:progress-button defined in build.gradle file which is the library published on jCenter.

Now any change to the module directory is instantly available to our project. Also since Android Studio has full support for composite build, it will show all of them in one place and makes editing even easier.

Should we always use modular development?

Modular development requires a bit of extra work, especially at the beginning of a project. So if your project is very simple or you don’t want to share code between the projects you might want to stick with a single module project.

However, I believe many projects can benefit from a more modular approach, something that is missing in the Android community.

--

--