How to debug a third-party Gradle plugin?

According to a thread in the gradle forums there is a somewhat secret org.gradle.debug-flag that allows you to attach a debugger.

gradle someTask --no-daemon -Dorg.gradle.debug=true

For the hotfixing/custom-plugin it should be enough to put your copied & modified plugin in rootProjectDir/buildSrc/src/main/groovy. You can read more about writing a custom plugin on the Gradle site.


This is how I did it using IntelliJ - Android Studio is based on IntelliJ so it should be the same, and other IDEs should be similar:

Download the correct version of the source code of the buggy Gradle plugin, or clone/access its version control repository and check out the relevant branch or tag corresponding to the version of the buggy Gradle plugin you are using. Import the code into IntelliJ by using File -> New project from existing sources. Then run, at the command line:

./gradlew someTask -Dorg.gradle.debug=true --no-daemon

and create a new remote debugging connection using port 5005, set your initial breakpoint, and start it.

Alternatively, you can use the Gradle daemon by instead doing ./gradlew -Dorg.gradle.jvmargs="standard JVM debugging arguments", and this way you can use any debugging port you like, but I have not tested this. For example:

./gradlew \
  -Dorg.gradle.jvmargs="-agentlib:jdwp=transport=dt_socket,server=y,address=5005,suspend=y" \
  someTask