Building Gradle modules based on Flag in a Multi Module project

Khalid Hamid
3 min readJan 15, 2021
Gradle Build Tools

When you are working on a project that start growing and you start thinking about scalability and maintainability. And you go in the direction of breaking down app into feature based sub modules which is owned by different team, and for a large project it can be 100+. Building so many module you will run into issues like build time, IDE would take too long to load and sync your project.

In general, at any given time a developer is working on a single or a few module’s at best. A developer don’t care about other modules and shouldn’t be spending any time building and rebuilding them while working on respective targeted modules.

In this article I will discuss about how to selectively build gradle modules based on flags. Demo project can be found on github link below.

There are few way’s this can be achieved. That is by
1. File dependency
2. Local maven
3. Remote dependency

Demo

Follow along with the demo project.

List all modules and respective aar file names in modules global property in
gradle-build-flags.gradle file as below.

There are four feature modules in demo project as listed above with respective aar filenames that build configuration will look for.

gradle-build-flags.gradle file has some utility functions used for build configuration.
module-helper.gradle file has includeAAROrModule(“<module-name>”) that adds a dependency as module or AAR depending on flags in local.properties

In your projects local.properties file you can add two flags as below.

Now in settings.gradle file for each feature module instead of using default
include(:<module>) function use as below without the colon.

This allows build configuration to add any module only if it is useLocalAAR=true and that module is listed under devModules=featureA

In module build.gradle file instead declaring dependency as below
implementation project(“:<module>”)

Use new function as below.

The includeAAROrModule(<module-name>) will add given dependency as AAR if useLocalAAR=true and if that module is not listed under localModules
and added as module dependency otherwise.

If useLocalAAR=false build configuration will simply add all modules as local modules regardless of devModules.

Brief about includeAAROrModule function.

This function will add given module as local module if

  1. useLocalAAR flag is set false
  2. useLocalAAR flag is set false and module is listed under devModules flag.

For any other case it will try to find respective AAR file first in app modules
build/localAARs folder, then in respective module’s build/output/aar folder
and if it fails to find in both location then it throws IllegalStateException with message on steps to resolve the error as shown below.

Once turn off localAAR flag and run makeAARs gradle task it will generate all feature module AAR ( remember to declare your feature module build task at dependsOn parameter) then you can proceed to use them from cache.

makeAARs task will only be configured if useLocalAAR flag is false.

Final note: You would have to manage transitive dependency declaration for each feature module that is added as AAR.

That’s all folks (: now your IDE don’t have to work so hard to build all unwanted modules every time you run sync !

--

--