Adding a New Module to an existing Gradle Project

This is the link for setting up a fresh new gradle project. If you already have a gradle project setup and want to add a new module to it, just follow these 4 steps.

If you already have a multi-module structure, then you can start from step 1. Otherwise, start from the step 0.

0. Applies the multi-module structure

If you have a project set up from the previous link, you will have a single module gradle project. Where all the settings sit in one build.gradle file. But before you adding a new module, you may want to convert it to a multi-module structure where every module can have its own build.gradle to manage its own settings. They combine with the root project’s build.gradle.

You need to extract the dependencies part that is only applicable to this existing module from the build.gradle in the root project to a new build.gradle file in the module folder. If there are other things that are different from module to module, you can put them in the module’s build.gradle as well.

1. Add the module to the project folder

  • Create a new directory for your new subproject/new module. You will potentially also need to create two subdirectories (you only need one if you plan on not running any unit tests).
    • src/main will hold the source code for your module and is a must-have.
    • src/test will hold any unit tests the new module may have.
    • You don’t need to follow the conventions, but if you don’t, then you need to describe your structure using the sourceSets{ } command in the build.gradle.

2. Configure gradle settings for this new module

  • Add a new file called “build.gradle” to the root of this module
  • Add dependencies to the “build.gradle” file. Initially, they should look like
version '1.0-SNAPSHOT'

dependencies {
   compile project(':reliedProject')
  • If further dependencies are required they can be added too. For dependencies only used during testing add the “testCompile” command before the name of the library.
  • That compile project(':reliedProject') is where you add dependencies within the project, here, project(':reliedProject') is the gradle sub-project that this new module relies on.

3. Register the new module in the settings.gradle

  • Create your project’s root directory. You must include a settings.gradle  file in the root directory.
  • Add this subproject to the project’s overall hierarchy by including the module in your “settings.gradle” file. Simply, a module can be added with the command
  • include ":NewModuleName"
    • The :NewModuleName indicates gradle can find the  source of this module in a folder named NewModuleName which will sit under the root of the project.
  • If you have a different location, you can set it up like this

def home = "home"
include ":${home}"
project(":${home}").projectDir = new File("./src/${home}")

Here, I include a new module called home, and define where to find it: "./src/${home}"

4. Refresh the whole project

  • Refresh all gradle projects using the button located in the gradle sidebar in IDEA.
  • Execute the build task on your specific new module.
    • If the build is successful you have setup your new module correctly.
    • If it’s not successful, go back and check the steps.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s