Can't debug annotation processor when using kapt and gradle

I am creating an annotation processor and I recently switched from using the default annotation type to kapt using the kotlin-kapt plugin.

I debugged my processor with the command

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

      

(full instructions are here: stackoverflow )

And then run the remote debug configuration. When I used annotationProcessor I could hit breakpoints and debug fine. with kapt, my cpu is working but i cant debug it. No breakpoints are triggered.

My kotlin version is 1.1.2-3

+7


source to share


3 answers


You really want to debug the Kotlin compiler daemon, not the Gradle daemon. This 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"

      

+10


source


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

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

In fact, we need to wait for the Kotlin compilation task to start before we connect the debugger, it does not stop and waits for you to connect the debugger, like in java.

So you want to track your build and look for a task :: :app:kaptDebugKotlin

And when you see it, go straight to your IDE and hit debug on your remote config. If you don't apply in time, the task will just move on. You have a few seconds to figure this out, but it's just a race to get it all to work.

It took me a very long time to figure it out and start working. Now, as soon as I run the command, I just go to the IDE and hit the debugger button, and I'm very lucky to have it attached this way.

+2


source


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

I am currently running the build from the command line:

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

      

and then connecting with Intellij via remote setup.

0


source







All Articles