The Coding Flow

Code Clean Or Die Tryin'

Help! Gradle is using a different version of a library to build my extension/plug-in!

Just a note for myself that might also help others.

Symptoms: When building your Gradle extension/plug-in, strange compile errors occur. E.g. the compiler can’t find methods, which are obviously there when you look into the library’s code.

Problem: Gradle carries a lot libraries. It is likely that you want to use one of those libraries in a more recent version. Gradle adds its libraries to the classpath when you add compile gradleApi() to your dependencies. Gradle also does this implicitely when you use the buildSrc mechanism to organize your extension/plug-in code.

Solution:

  • Do not put code directly into buildSrc, use subprojects within buildSrc instead.
  • Make sure to include the subprojects into buildSrc’s runtime classpath to make them available in the build script of your project at build time:
// in buildSrc/build.gradle
dependencies {
    runtime subprojects.collect { owner.project(it.path) }
}
  • Use compile files(gradleApi().resolve().findAll { !it.name.contains("<the library you want to use in a different version>") }) instead of compile gradleApi() in the subprojects of buildSrc.
  • Do not add apply plugin: 'java-gradle-plugin' in any of buildSrc’s subprojects because it implicitely adds compile gradleApi().