Simplify Gradle Dependency Management

Umang Chamaria
3 min readOct 5, 2019

--

Before getting into how to simplify dependency management in gradle let’s first look at the current approach and problems with the approach.

How do we add dependencies in gradle?

Typically dependencies are added in the dependencies block of the build.gradle file. Below is an example of the same.

Dependencies need to be added in this way to all the modules of the project.

Problems with this approach

  1. Duplicated configuration across modules.
  2. The duplicated configuration makes version upgrades hard as every occurrence needs an update which is very tedious.
  3. Updating occurrences is also error-prone, especially in projects which have a large number of modules.

Solution to the problem

The above problem can be solved in a few ways.

  1. Pulling out dependencies to an external file or the root level build.gradle file of the project.
  2. Moving the dependencies into the buildSrc folder.

Let’s have a look at the possible approaches with their pros and cons.

1. Move Dependency to root level gradle file

Dependencies can be pulled out to the root level build.gradle and added inside the ext block. Once we have done this any given dependency can be re-used across modules.

As shown above, by pulling out the dependencies to the root level build.gradle file solves the problem. We can now re-use the dependencies across modules and even upgrading the version number is fairly straight forward.

The problems with this approach:

  1. Code navigation: Navigating to the original file to check the actual dependency is a hassle
  2. Code Completion: IDEs do not offer code completion for in this case and one needs to keep switching between files to get the dependency name right.

2. Moving dependency to buildSrc directory

Let us first understand what is the buildSrc directory and then look at how it helps us in simplifying the dependency management problem.

What is buildSrc directory?

The directory buildSrc is treated as an included build. Upon discovery of the directory, Gradle automatically compiles and tests this code and puts it in the classpath of your build script. For multi-project builds there can be only one buildSrc directory, which has to sit in the root project directory. buildSrc should be preferred over script plugins as it is easier to maintain, refactor and test the code.

So, anything added to the buildSrc folder of the project is available for use across all the modules of the project. Hence, this is a good place to add all the dependencies and refer them in the respective modules.

How to setup buildSrc folder and dependencies?

  1. Create a buildSrc directory at the root level of your
  2. Add a build.gradle.kts file inside the folder.

3. Add the following lines of code in the gradle file and hit sync.

4. Create the src/main/java directory and start adding your Kotlin files which will hold the dependencies. The folder structure would look as shown below

Once the dependencies are added in files it can be referenced in any build.gradle without any extra configuration.

Your Dependencies.kt and build.gradle file of your app and other modules in the project will look something like

This approach solves the problems faces in the previous approach Code completion and Code Navigation. In this setup, you can easily click on the dependency declaration and navigate to the file where the dependencies are declared. IDE also provides auto-complete using which eases the task of adding dependencies.

Note: It is not necessary to use Kotlin for this approach. The same results can be achieved if one is using the Java files for maintaining the dependencies and versions.

--

--

Umang Chamaria
Umang Chamaria

No responses yet