Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

A Gradle build fails on classes that use Lombok annotations

ErrorGradle build fails on Lombok annotated classes

Short answer

Lombok is two things for Gradle: a compile-time library and an annotation processor. Gradle compiles with the processor only when it sits on the annotationProcessor configuration. Declare Lombok as compileOnly and as annotationProcessor, and do the same for tests with testCompileOnly and testAnnotationProcessor.

  • Applies to Gradle 8.x and 9.x, Lombok 1.18.48, Java plugin and Android Gradle plugin 8.x and 9.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Show what Gradle has on the annotation processor classpath. If Lombok is not listed there, the processor never runs.

    ./gradlew dependencies --configuration annotationProcessor
  2. 2Match what you see, then jump to its fix

    1. 1The dependencies output for annotationProcessor does not list org.projectlombok:lombok, while the main code uses @Getter, @Builder or @DataLombok is declared, but not as an annotation processorFix
    2. 2The build fails only in the test compile step, on test classes or test helpers that use Lombok annotationsMain code compiles, but the test source set has no LombokFix
    3. 3Error: Annotation processors must be explicitly declared now.An Android module has the processor on the compile classpathFix

What does the error look like?

  • Gradle build fails on Lombok annotated classes
  • Error: Annotation processors must be explicitly declared now.

Cause 1 of 3

Lombok is declared, but not as an annotation processor

1Check

The Lombok setup page for Gradle shows two lines for main code: compileOnly and annotationProcessor. The Android documentation adds that Gradle 5.0 and higher ignores annotation processors found on the compile classpath, so a line such as implementation alone never runs Lombok.

grep -n "lombok" build.gradle build.gradle.kts 2>/dev/null

2Fix

Add both lines to the dependencies block, with the same version, and make sure mavenCentral() is in repositories.

repositories {
    mavenCentral()
}

dependencies {
    compileOnly("org.projectlombok:lombok:1.18.48")
    annotationProcessor("org.projectlombok:lombok:1.18.48")
}

3Verify

The processor classpath now lists Lombok, and the main code compiles.

./gradlew dependencies --configuration annotationProcessor
./gradlew compileJava

Confirmed by projectlombok.org

Did this fix it?

Cause 2 of 3

Main code compiles, but the test source set has no Lombok

1Check

Each source set has its own compile and processor classpath. The Lombok setup page lists separate lines for tests, so main-code lines do not cover them.

./gradlew compileTestJava

2Fix

Add the two test lines next to the main ones.

dependencies {
    testCompileOnly("org.projectlombok:lombok:1.18.48")
    testAnnotationProcessor("org.projectlombok:lombok:1.18.48")
}

3Verify

Run the test compile again.

./gradlew compileTestJava

Confirmed by projectlombok.org

Did this fix it?

Cause 3 of 3

An Android module has the processor on the compile classpath

1Check

The Android Gradle plugin treats a dependency as a processor when its jar carries the processor service file, and it fails the build if that jar is on the compile classpath. Look for a Lombok line that is not paired with an annotationProcessor line.

grep -n "lombok" app/build.gradle app/build.gradle.kts 2>/dev/null

2Fix

Follow the Android pattern: the library that holds the annotations goes to compileOnly, and the processor goes to annotationProcessor.

dependencies {
    compileOnly("org.projectlombok:lombok:1.18.48")
    annotationProcessor("org.projectlombok:lombok:1.18.48")
}

3Verify

Sync the project and build the debug variant.

./gradlew :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Keep the Lombok version in one place, such as a version catalog, so the compileOnly and annotationProcessor lines cannot drift apart.
  • Add the test lines in the same change as the main ones.
  • Build with ./gradlew from a terminal before you trust a green IDE; Gradle's result is the one CI will see.
  • Re-read the Lombok setup page when you upgrade Gradle, so the dependency lines match what it shows.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Gradle runs no Cucumber scenarios from my feature files

Feature files exist under src/test but nothing runs

Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.