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.


Tomas Zezula

Hello! I'm a technology enthusiast with a knack for solving problems and a passion for making complex concepts accessible. My journey spans across software development, project management, and technical writing. I specialise in transforming rough sketches of ideas to fully launched products, all the while breaking down complex processes into understandable language. I believe a well-designed software development process is key to driving business growth. My focus as a leader and technical writer aims to bridge the tech-business divide, ensuring that intricate concepts are available and understandable to all. As a consultant, I'm eager to bring my versatile skills and extensive experience to help businesses navigate their software integration needs. Whether you're seeking bespoke software solutions, well-coordinated product launches, or easily digestible tech content, I'm here to make it happen. Ready to turn your vision into reality? Let's connect and explore the possibilities together.