A Gradle build fails on classes that use Lombok annotations
Gradle build fails on Lombok annotated classesShort 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.
Fix it fast
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 annotationProcessor2Match what you see, then jump to its fix
- 1
The 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
The 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
Error: Annotation processors must be explicitly declared now.An Android module has the processor on the compile classpathFix
- 1
What does the error look like?
Gradle build fails on Lombok annotated classesError: 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/null2Fix
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 compileJavaConfirmed by projectlombok.org
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 compileTestJava2Fix
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 compileTestJavaConfirmed by projectlombok.org
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/null2Fix
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:assembleDebugConfirmed by developer.android.com
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
The build fails inside Lombok's annotation processor after a JDK upgrade
Fatal error compiling: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor
Lombok reaches into the compiler, so each new JDK needs a Lombok release that supports it.
2 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Gradle 8 fails on Report destination: switch to outputLocation
What went wrong: 'java.io.File org.gradle.api.reporting.ConfigurableReport.getDestination()'
Gradle 8.0 removed the deprecated destination and enabled properties of the Report type.
2 causes, each with a fixBack end
IntelliJ IDEA compiles with a JDK that does not know Java 17
Invalid source release: 17
The compiler that IntelliJ or Gradle starts is older than Java 17, but the project asks for Java 17 source.
3 causes, each with a fixBack end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fix