If you are new to any tool or technology, knowing how to debug when things go wrong is a really important skill. This post gives some beginner tips on how to debug Gradle builds. Note: In the commands below, I’m assuming you are invoking gradle via the gradle wrapper, so all commands start “gradlew”. If you aren’t using the wrapper, this would just be “gradle”.
Logging
By default, Gradle builds run in “quiet” log mode. Use the -i flag to get info level logs, or -d for debug.
Knowing what tasks are available
If you are a beginner, sometimes you don’t even know what tasks are available to you in the current build. Simply run the “tasks” command and it will list every available task.
Running a task in a single module
gradles :module:sub-module:task
Seeing dependencies
gradlew dependencies
This is for the top level module. For a sub module, run the task in that sub module:
gradlew :module:sub-module:task
If you want to add a task to your build that will print the dependencies for all modules, you can do this an an allprojects closure in your top level build.gradle Groovy file:
allprojects { task printAllDependencies(type: DependencyReportTask) {} }
Debugging your own Gradle build scripts
In IntelliJ, you can right click on a Gradle task in the right nav and select the “Debug” option.
Alternatively, you can start Gradle in remote debug by adding:
-Dorg.gradle.debug=true
to the startup properties.
Debugging gradle core
This can be done provided you have the full distribution in use, not just the binary. So in the gradle/gradle-wrapper.properties file, set the distro:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
Debugging a third party plugin
There is currently a bug in IntelliJ whereby it will not find the source for a third party plugin. You can work around this by temporarily adding the plugin as a regular dependency to your app / module.
For Gradle docs on debugging, see:
https://docs.gradle.org/current/userguide/logging.html
https://docs.gradle.org/current/userguide/troubleshooting.html
https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html
Some of my other posts on Gradle:
Dependencies and configurations in Gradle
Gradle incremental tasks and builds
Gradle Release Plugin
Code coverage with Gradle and Jacoco