Kapt "IllegalStateException: failed to analyze" when encountering a large amount of errors

Whenever kapt encounters an annotation processing error in our project (incorrect Dagger annotation, databinding issue, etc.) the underlying exception is not logged to the Gradle console. This makes debugging the underlying issue very time-consuming.

We have artificially created the same Dagger error in our project and a fresh one. In the one without the large amount of annotated classes being processed, the underlying exception is logged correctly.

Most of our errors are Dagger or databinding problems while generating code, which causes the compiler to log a large amount of exceptions (one for every layout or DI annotated class).

e: {path_redacted}/AppFeedbackActivity.java:17: error: cannot find symbol
e: 

e:     private ActivityAppFeedbackBinding mBinding;
e:             ^
e:   symbol:   class ActivityAppFeedbackBinding
e:   location: class AppFeedbackActivity
e:
e: public class MainActivity extends ExperticityActivity {
e:        ^
e: {path_redacted}/ProfileSettingsActivity.java:22: error: dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.

Here is the stack trace that is logged each time no matter the actual problem in our codebase.

e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
	at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:386)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:889)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:96)
	at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
	at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:90)
	at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:42)
	at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:205)
	at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:166)
	at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:82)
	at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:96)
	at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:106)
	at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:83)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:377)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:68)
	at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:368)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:133)
	... 30 more

We have the following in our build.gradle, increasing the limit above 500 does not resolve the issue.

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "500"
    }
}

kapt {
    javacOptions {
        option("-Xmaxerrs", 500)
    }
}

Here is all of our version info:

Kotlin v1.1.51
Android Gradle Plugin v3.0.0
Android Studio v3.0.0

kapt "com.android.databinding:compiler:v3.0.0"
kapt "com.google.dagger:dagger-compiler:v2.7"

Does this still occur when you manually invoke the gradle wrapper? Did you pass the needed parameters to increase gradle verbosity?

Yes, the same issue occurs in the console when using ./gradlew build --debug or --stacktrace. Is there anything else you would recommend?

HIļ¼ŒI have this mistake, too. I use android-studio3.0 and android-gradle-3.0.0. When I compile my project , I get this message ā€œjava.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processingā€ in my gradle console. How can I solve this question? THanks!

I spoke with @orangy at KotlinConf today and he suggested I ping @yanex about this issue.

I also spoke with @yeet and he confirmed he has seen a similar issue before, but the fix he had did not work in this case.

Looks like a few other people are encountering the same thing:

Iā€™m experiencing the same problem, not sure the problem is caused by a mistake with Dagger like things, as my project builds fine until upgrading Android Gradle Plugin to v3.0.0.

Well, there are two kinds of KaptError exceptions, actually. The one you experience, ā€œError while annotation processingā€, means that the Java compiler and/or some annotation processor logged an error diagnostic message. It does not mean some AP threw an exception.

This message normally can be found above the exception trace you shared. If you have not any error messages there, itā€™s a kapt bug. (Please share the build log with the --debug option specified, and also with kapt.verbose=true in the gradle.properties file if you canā€™t share the whole project).
Note that not all annotation processors pass the proper file location so the error message can be a plain text (without a file/line number).

The other kind of KaptError is ā€œException while annotation processingā€. It typically means some exception is occurred in one of annotation processors. In that case the underlying AP exception should be printed as a ā€˜causeā€™.

The deprecated original kapt shows the error properly.

Workaround:
Downgrade to the deprecated version of kapt by removing apply plugin: 'kotlin-kapt' and then add

kapt {
    generateStubs = true
    javacOptions {
        option("-Xmaxerrs", 500)
    }
}

@yanex Can you give me an email address to share the build log with? Do you want logs from a run of deprecated kapt also?

The workaround Austynm suggested isnā€™t working for me. Iā€™m uploading my log, I hope it could help too.debug.txt.zip (268.8 KB)

The original kapt will be removed in 1.2.20, so it not really a solution.

yan.zhulanow@jetbrains.com.
Well, it would be nice to see logs both from the old kapt and kapt3.

Do you mean there is no proper stack trace below the

class file for com.f2prateek.rx.preferences2.RxSharedPreferences not found
Consult the following stack trace for details.

error message?

No, thereā€™s a proper stack trace below the mentioned error, which starts with:

19:22:32.070 [ERROR] [org.gradle.api.Task] e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing

According to what you said above it seems to be a problem with kapt.

@austynm

Well, looks like there should be an another one from javac.
By the way, did you send the build logs? Iā€™m afraid I didnā€™t get anything recently.

If you send the logs, please add the --debug option, and please also enable verbose output by adding the kapt.verbose=true to gradle.properties.

@yanex Iā€™ve sent the logs to your email, please let me know when you receive them. If you need anything more let me know via Slack or email.

@austynm I donā€™t know if this is the only issue that causes this behavior, but here it is: https://youtrack.jetbrains.com/issue/KT-21264.

1 Like

Glad you found that problem! Let me know when you have a release we can test and I can let you know if it is fixed.

Iā€™m having a similar error trying to use DataBinding with a RecyclerView. My data class is written in Kotlin, everything else in my project is Java. The Gradle Console shows the following error message when I try to build:

e: C:\Users\...\app\db\LensListAdapter.java:60: error: cannot find symbol
e: 
e:         private LensListBinding binding;
e:                 ^
e:   symbol:   class LensListBinding
e:   location: class LensListAdapter.LensListViewHolder

Android Studio seems to build the Binding classes properly from my layout file (lens_list.xml). Let me know if thereā€™s anything I can do to help debug this issue.

any updates on this?