Can't Debug an Annotation Processor when using kapt and gradle
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.