Gradle Multi-Module Builds: Setting Up Shared Dependencies
When you are managing a multi-module Gradle project, more often than not you’d want to share certain dependencies across different modules. This is particularly true for test dependencies. You can achieve this by defining common dependencies in the root build file and applying them to all subprojects. However, a problem often encountered is the dreaded Unresolved reference: testImplementation
error. This blog post suggests a simple trick that resolves the issue. Give it a go and happy testing ever after!
The error Unresolved reference: testImplementation
or Unresolved reference: implementation
etc. (depending on what you’re trying to achieve) usually comes up because these configurations are provided by the Java or Kotlin plugin which may not have been applied yet. In other words, it’s a timing issue. Let’s see how to tackle it.
Unresolved Reference Problem
Suppose the following initial setup of our build.gradle.kts
file.
subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
}
tasks.withType<Test> {
useJUnitPlatform()
}
}
This seems correct but once we try to build, we get an error regarding unresolved references testImplementation
and testRuntimeOnly
.
The Fix
The problem can be resolved by enclosing testImplementation
and testRuntimeOnly
inside quotation marks – like this.
subprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
repositories {
mavenCentral()
}
dependencies {
"testImplementation"("org.junit.jupiter:junit-jupiter-api:5.7.0")
"testRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine:5.7.0")
}
tasks.withType<Test> {
useJUnitPlatform()
}
}
These quotation marks are used to avoid an issue with unresolved references which happen due to the order of applying the plugins.
Applying this simple trick in your main build.gradle.kts
file allows you to declare a common set of test dependencies for your entire multi-module project.
I hope you find this useful for your blog post. If you need further refinements or additional related topics covered, feel free to ask in the comment section below.