DEV Community

Clayton Walker
Clayton Walker

Posted on

Fix dynamic agent loading warning in Gradle

In newer versions java, dynamically loaded agents will be disallowed. Warnings like the following would be logged by your test runner.

WARNING: A Java agent has been loaded dynamically (/Users/cwalker/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy-agent/1.14.6/46e2545d7a97b6ccb195621650c5957279eb4812/byte-buddy-agent-1.14.6.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
Enter fullscreen mode Exit fullscreen mode

To fix this, we can get add a new agent configuration and map dependencies from that configuration to the -javaagent flag.

Using the new test-suites plugin, that would look like the following.

val testAgent by configurations.creating

dependencies {
    testAgent("net.bytebuddy:byte-buddy-agent:1.14.6")
}

testing {
    suites {
        val test by getting(JvmTestSuite::class) {
            useJUnitJupiter("5.10.1")
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-reflect:1.9.22")
                implementation("io.mockk:mockk:1.13.9")
            }
            targets.configureEach {
                testTask.configure {
                    jvmArgs(testAgent.files.map { "-javaagent:${it.absolutePath}" })
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

No more warnings!

Top comments (0)