0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lesson

Inter-Project Dependencies

Manage dependencies between subprojects and ensure correct build order and artifact resolution.

Inter-Project Dependencies is a free Groovy & Gradle: JVM Automation and Build Engineering lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Groovy & Gradle: JVM Automation and Build Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction to Project Dependencies

In a multi-project Gradle build, different subprojects often need to work together. One subproject might produce a library that another subproject uses.

This is where inter-project dependencies come in! They define how subprojects rely on each other, ensuring the correct build order and artifact sharing.

  • Inter-project dependencies: How one subproject uses code or outputs from another subproject.
  • Crucial for modular applications.

Declaring a Basic Dependency

Declaring that one subproject depends on another is straightforward in Gradle. You use the project() function within your dependencies block.

For example, if your app subproject needs code from your lib subproject, you'd add this to app/build.gradle:

// app/build.gradle
dependencies {
    implementation project(':lib')
}

The :lib part refers to the path of the lib subproject relative to the root.

Setting Up Our Example

Let's imagine a simple multi-project setup. We'll have a root project, an app subproject, and a lib subproject.

Your settings.gradle would look like this:

// settings.gradle
rootProject.name = 'my-multi-project'
include 'app', 'lib'

This tells Gradle about our two subprojects, app and lib.

`implementation` vs. `api`

When declaring inter-project dependencies, you'll typically use configurations like implementation or api.

  • implementation: The most common choice. The dependency is used internally by the subproject and is not exposed to consumers of that subproject.
  • api: Exposes the dependency to consumers. If app depends on lib via api, and otherApp depends on app, then otherApp will also see lib's API.

For internal use between subprojects, implementation is generally preferred for better encapsulation and faster builds.

The Library Subproject (`lib`)

First, let's create a simple class in our lib subproject that the app subproject will use. This class will provide a basic utility.

Inside lib/src/main/groovy/com/coddykit/LibUtils.groovy:

package com.coddykit

class LibUtils {
    static String getGreeting() {
        return "Hello from LibUtils!"
    }
}

App Subproject Using `lib`'s Code

Now, let's make our app subproject use the LibUtils class from our lib subproject. Remember, we declared implementation project(':lib') in app/build.gradle.

Try running this example:

package com.coddykit

import com.coddykit.LibUtils

class AppMain {
    static void main(String[] args) {
        String message = LibUtils.getGreeting()
        System.out.println(message)
    }
}

Automatic Build Order

One of the biggest advantages of declaring inter-project dependencies is that Gradle automatically figures out the correct build order.

  • If app depends on lib, Gradle ensures that lib is compiled and its artifacts are available before app starts its compilation.
  • This saves you from manually managing build sequences, especially in complex multi-project setups.

When you run gradle build from the root, Gradle will build lib first, then app.

Understanding Transitive Effects

What if your lib subproject itself depends on another subproject, say common?

// lib/build.gradle
dependencies {
    implementation project(':common')
}

// app/build.gradle
dependencies {
    implementation project(':lib')
}

If app depends on lib, and lib depends on common, Gradle will ensure that common is also built and its artifacts are available transitively to app's classpath for compilation, but not necessarily for its API if implementation is used for lib.

Maintain Clean Dependencies

To keep your multi-project builds manageable and efficient, follow these best practices:

  • Minimal Dependencies: Only declare dependencies on subprojects you truly need.
  • Use implementation: Prefer implementation over api for internal subproject dependencies to improve encapsulation and reduce rebuilds.
  • Clear Boundaries: Design your subprojects with clear responsibilities to avoid circular dependencies.
  • Avoid Deep Paths: Keep subproject paths concise (e.g., :module:submodule is fine, but avoid excessively long paths).

Dependency Declaration Check

You have a multi-project build with a web subproject and a core subproject. The web subproject needs to use classes from the core subproject for its internal logic, but not expose core's API to any further consumers of web.

Inter-Project Dependencies Recap

Great job! In this lesson, you learned how to manage dependencies between subprojects in a Gradle multi-project build.

  • We covered how to declare dependencies using project(':subproject').
  • We discussed the importance of implementation for internal use.
  • You saw how Gradle automatically handles build order.
  • We explored best practices for clean dependency management.

Mastering inter-project dependencies is key to building large, modular applications efficiently with Gradle!

Frequently asked questions

Is the “Inter-Project Dependencies” lesson free?

Yes — the full text of “Inter-Project Dependencies” is free to read here on the web, and the Groovy & Gradle: JVM Automation and Build Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Groovy & Gradle: JVM Automation and Build Engineering course, upgrade to CoddyKit PRO.

What will I learn in “Inter-Project Dependencies”?

Manage dependencies between subprojects and ensure correct build order and artifact resolution. You practise Groovy & Gradle: JVM Automation and Build Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Groovy & Gradle: JVM Automation and Build Engineering?

No prior experience is required. Groovy & Gradle: JVM Automation and Build Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inter-Project Dependencies” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Groovy & Gradle: JVM Automation and Build Engineering lesson?

Yes. Every Groovy & Gradle: JVM Automation and Build Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Monorepo Project Structure
  2. Subprojects & Configurations
  3. Inter-Project Dependencies
  4. Composite Builds and Build Composition
← Back to Groovy & Gradle: JVM Automation and Build Engineering