简体   繁体   中英

Can't Debug an Annotation Processor when using kapt and gradle

I'm building an annotation processor, and I recently switched from using the default annotationProcessor type to kapt, using the kotlin-kapt plugin.

I was debugging my processor by using the command

./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac

(full instructions here: https://stackoverflow.com/a/42488641/502463 )

And then running a Remote debugging configuration. When I used annotationProcessor, I could hit breakpoints, and debug fine. with kapt, my processor runs, but I can't debug it. No breakpoints are triggered.

My kotlin version is 1.1.2-3

You actually want to debug the Kotlin compiler daemon, not the Gradle daemon. Here is how you can pass the required JVM arguments:

./gradlew <tasks> -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"

The other answer is generally correct, but I found https://medium.com/@daptronic/annotation-processing-with-kapt-and-gradle-237793f2be57 helpful for going into more detail.

You can run something like this

./gradlew --no-daemon clean compileDebugKotlin -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"

or if you want to run a specific module

./gradlew --no-daemon :modulename:clean :modulename:compileDebugKotlin -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n"

The tricky part

We actually need to wait for the Kotlin compilation task to begin before we attach the debugger, it doesn't pause and wait for you to attach the debugger like with java.

So you want to monitor your build and look for the task: :app:kaptDebugKotlin And when you see it, head immediately over to your IDE and hit debug on your Remote configuration. If you don't attach in time, the task will just move on. You have a few seconds to figure it out, but it's a bit of a race to get it all working.

This took me a frustratingly long time to figure out and get working. Now as soon as I run the command I just go to the IDE and mash the debugger button and I have had pretty good luck getting it to attach that way.

I just tried to debug a Kotlin annotation processor and found this post. You can tell the JVM to wait for the debugger by passing suspend=y

What I do now is starting the build from command line:

./gradlew --no-daemon clean build -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=y"

and then attaching with Intellij via remote configuration.

Since Kotlin 1.2.60, kapt supports using the Gradle Worker API by including kapt.use.worker.api=true in your gradle.properties file.

The bonus side effect is that the kapt task is debuggable with the normal gradle debug arguments ( ./gradlew <task> -Dorg.gradle.debug=true --no-daemon ) , and no specific kotlin args are needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM