Illustration by Virginia Poltrack

A little thing about Android module paths

How to configure your module paths, instead of using Gradle’s default.

Pietro Maggi
Android Developers
Published in
2 min readAug 2, 2019

--

Working to make our Android application more modular, I ended up with a sample that included a set of on-demand features grouped inside a folder:

You are going to have more meaningful names, depending on how you modularize your application

Pretty standard setup, all the on-demand modules, inside a “features” folder; clean.

These modules are included in the settings.gradle file as:

include ':app'
include ':features:module1'
include ':features:module2'
include ':features:module3'
include ':features:module4'

These setup works nicely with a single “minor” issue: an empty module named features in the Android view in Android Studio:

By default, we have an empty module in Android Studio’s Android view

I can live with that, but I would much prefer to remove that empty module from my project!

If you cannot remove it, just rename it!

At I/O I was lucky enough to attend the “Android Studio: Tips and Tricks” talk where Ivan Gravilovic, from Google, shared some amazing tips. One of these was a possible solution for my problem: setting a custom path for my modules.

Ivan Gavrilovic from Google sharing Gradle’s tips and tricks

In this particular case our settings.gradle becomes:

include ':app'
include ':module1'
include ':module2'
include ':module3'
include ':module4'
// Set a custom path for the four features modules.
// This avoid to have an empty "features" module in Android Studio.
project(":module1").projectDir=new File(rootDir, "features/module1")
project(":module2").projectDir=new File(rootDir, "features/module2")
project(":module3").projectDir=new File(rootDir, "features/module3")
project(":module4").projectDir=new File(rootDir, "features/module4")

And the layout in Android Studio is now:

No more empty modules!

Conclusion

As the title says, this is really a small thing, but it helps keep my project in order and it shows how a small Gradle configuration can help keep your project tidy.

You can find this update in the latest version of the on-demand modules codelab.

Resources

--

--