How to debug a Gradle build.gradle file (in a debugger, with breakpoints)?

There is the easier way:

just add in your command line -Dorg.gradle.debug=true --no-daemon

For example: gradle nameOfTask -Dorg.gradle.debug=true --no-daemon

Then you should start your IDE and run remote debugging with localhost port 5005, that all.

Gradle is waiting to you, because standard option server=y

org.gradle.debug

When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.

Link to docs


export GRADLE_OPTS does not work for me.

Try this:

1 Add remote debug config

enter image description here

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

2 add breakpoint in gradle

enter image description here

3 ./gradlew your task --stacktrace -Dorg.gradle.daemon=false -Dorg.gradle.debug=true

4 Attach debug in studio click icon enter image description here

enter image description here

Then the breakpoint stops !!

enter image description here

If console is not goiong, click this in debug pannel in studio. enter image description here


IntelliJ 2018.2 added the ability to debug Gradle scripts in a similar fashion to how you might run/debug other projects. You can see the announcement in the release notes here.

Here is a screenshot of some of the documentation from 2018.2:

IntelliJ documentation for debugging a Gradle script task

It does not yet support the kotlin-dsl (see gradle/kotlin-dsl/issues/39).


Personnaly I do this when I need to debug build scripts:

Inside you terminal do

export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

Then run your build

gradle clean install

Finally put some breakpoints and launch the remote debug configuration inside your IDE on the port 5005 and you’re good to go!