Gradle multi project build — substituting jar dependencies with local projects

Jakub Komorowski
codequest
Published in
3 min readApr 12, 2019

--

One of the projects we’re working on recently is using a multi-module architecture. Each feature module is kept in a separate Git repository, built into a jar, and uploaded to a remote repository. Those libraries are then added to the main application as Gradle dependencies.

Remote Gradle dependencies in the root project

We have come across several problems when working with such a setup. Code was hard to navigate, and simultaneous changes in multiple modules were hard to test. That’s when we came across Gradle multi-project builds and dependency substitution.

Setting up a multi-project build with multiple repositories

Gradle multi-project build is the feature used when building Android projects with multiple modules. In Gradle terminology, each Android module is Gradle project. If your Gradle projects are all in a root project directory you can include them by simply adding them in settings.gradle file in a root directory.

Local dependencies using multi project build

But what if each of my feature modules is in a separate git repository? One way to do that would be to use git submodules, but I was advised against it. The solution was to clone module repos into separate directories and specify their paths in setting.gradle file.

Mutli project build with dependency project paths specified

Dependency substitution

Setting up a gradle multi-project build when you start writing an application from scratch is super easy. But what if the project has already established structure? And what if the feature modules depend on each other?

Example project dependency structure

In this setup features depend on the Core module. If I would just clone all of the repos to my hard drive and add them as local dependencies in my application project, Feature A and Feature B would still download Core module from the remote repository.

Feature-a and feature-b projects depend on core library

Of course, I could just go through the feature modules and replace all remote dependencies with local dependencies. Luckily, there’s an easier way to do that: dependency substitution. You can easily replace remote dependencies with local projects by adding a top-level instruction to the build.gradle file in the app module:

Dependency substitution example

Using Android Studio Git plugin with multiple repositories

If you want to use Android Studio’s Git plugin for managing your repositories, go to `Settings -> Version Control` and add your dependency directories as version control roots by selecting them and clicking `+` button below. Android Studio will now see dependency projects as git repositories and will detect changes and allow Git operations on files in dependency directories.

Adding multiple repositories in Android Studio version control plugin

TL;DR

To set up a multi-project build on your machine:

  • Clone relevant repositories to your hard drive.
  • Add project dependencies to `settings.gradle` file in the root project and specify project paths, for example:
  • In the `build.gradle` project in the `app` module add an instruction for the custom dependency resolution strategy, for example:
  • Sync the project with Gradle files. At this point the dependencies project should be visible in Android Studio’s file browser:
  • (optional) If you want to use Android Studio’s version control plugin, add the dependency’s directories as version control roots in the Android Studio settings.

--

--