Multi-Module Architecture in Android

Ahmed Elkhami
4 min readMay 18, 2022

--

Understanding Multi-Module Architecture and how you can use it in Android.

Before going further in this article, you should be familiar with clean architecture concepts.

What is Multi-Module architecture?

Multi-module architecture is a way of arranging code and packages in your project but on a broader view. Usually, when you create a project, you would work in a single module, but also we can create more modules within the same app.

Why? Let’s find out.

Why Multi-Module architecture?

If you have a huge application, where it’s hard to navigate through classes and packages and sometimes get confused when navigating for packages or classes.

Then implementing multi-module architecture in your app can solve these problems; what does it solve is that it separates everything and makes every small detail easily visible to your eyes, also if you are working in a team you will face fewer conflicts while merging your code, while seeing the project separated, clean and more understandable.

But you must be careful, and you should know what you are doing as if the multi-module architecture is misused, it can be a considerable burden.

Advantages

The advantages will be acquired when you implement the multi-module architecture correctly.

  • Your code is separated, which supports the single responsibility principle.
  • As each layer is separated, you can easily test your code.
  • You can control which module can have access to the other module if you are using clean architecture, then you can’t access the code from the data layer if you are in the presentation layer.
  • Each module is a library, you can reuse it in other projects or even replace it with another module, and your app won’t be affected.
  • While working in a team and each developer is working on a module, there will be no conflicts while merging or having a code that affects another developer’s code.
  • Less Gradle build times, as Android Studio builds only the changed module and its sub modules.
  • Taking advantage of Gradle build in multi-threading.
  • You can benefit from Android Instant App Module and Dynamic feature Module.

Disadvantages

The disadvantages can occur if you have implemented the multi-module architecture the wrong way or you miss use it, as it is for the big projects with a big team of developers working on it simultaneously.

  • A lot of work for setting up and preparing.
  • Might face a lot of Gradle issues in the setting up phase.
  • Long Gradle build times, if not implemented correctly.
  • Tight coupling, if not implemented correctly.
  • Complicated structure, if not implemented correctly.

Things to think about while designing your modules

You need to handle the dependencies of each module correctly, as if you let the modules in your project depend on the main module, it will result in a very long building time.

Every module should be independent, to be able to reuse it or replace it later on, or all the advantages will turn sideways.

Multi-Module Architecture Strategies

The multi-module architecture strategies show how you will organize the modules in your project. There are two commonly used strategies. You will know the difference between them and which will be better to choose.

Modularizing by Layer

By putting each layer in a module, you will create a module for the presentation layer, another module for the domain layer and another for the data layer.

Advantages

  • You got a clear separation of layers
  • You can determine which layer can access the other and which layer cannot.

Disadvantages

  • You can’t reuse each module.
  • The modules will get crowded quickly since all your app’s code will be in each layer

Modularizing by Feature

Each feature will have its own module

Advantages

  • Easley accessing each feature.
  • Fewer conflicts when merging.
  • You can reuse these modules or replace them.

Disadvantages

  • The separation between layers is not the best, as you can access the code from other layers.

Which is better to use?

As you can see, each strategy has its advantages and disadvantages, so why don’t we mix and match these advantages while eliminating these disadvantages.

So we can create a module for each feature, like Modularizing by Feature strategy, and then on each feature, we will create three other modules, which are the three layers of clean architecture presentation, domain and data.

In this approach, we have eliminated the disadvantages of both the above strategies while keeping the advantages of both of them together.

Conclusion

Multi-module architecture is a great way to achieve code separation and provide easily testable code and a high understanding of the project structure. Still, you need to be careful, and you should use this approach when only required as it will be overkill for small projects. Also you need to be cautious while implementing and take your time when preparing.

If you came here, this means this article has finished; thank you so much for reading and I hope you have benefited from the article.

If you want to tell me something, please leave a comment, and if you find this article helpful, please feel free to 👏 and share.

--

--