// Copyright (C) 2024 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[troubleshooting]]
= Troubleshooting builds

The following is a collection of common issues and suggestions for addressing them.
You can get other tips and search the link:https://discuss.gradle.org/c/help-discuss[Gradle forums] and link:https://stackoverflow.com/questions/tagged/gradle[StackOverflow #gradle] answers.

[[sec:troubleshooting_installation]]
== Troubleshooting the installation

If you followed the <<installation.adoc#installation,installation instructions>>, and aren’t able to execute your Gradle build, here are some tips that may help.

If you installed Gradle outside of just invoking the <<gradle_wrapper.adoc#gradle_wrapper,Gradle Wrapper>>, you can check your Gradle installation by running `gradle --version` in a terminal.

You should see something like this:

----
❯ gradle --version

------------------------------------------------------------
Gradle {gradleVersion}
------------------------------------------------------------

Build time:    2025-05-13 06:56:13 UTC
Revision:      3c890746756262d3778e12eaa5155d661d7cbdf2

Kotlin:        2.1.21
Groovy:        4.0.28
Ant:           Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM:  22.0.1 (Oracle Corporation 22.0.1+8-16)
Daemon JVM:    Compatible with Java 17, any vendor, nativeImageCapable=false (from gradle/gradle-daemon-jvm.properties)
OS:            Mac OS X 15.5 aarch64
----

If not, here are some things you might see instead.

=== Command not found: gradle

If you get "command not found: gradle", you must ensure that Gradle is correctly added to your `PATH`.

=== JAVA_HOME is set to an invalid directory

If you get an error like:

----
ERROR: JAVA_HOME is set to an invalid directory
----

Set the JAVA_HOME variable in your environment to match the location of your Java installation:

----
$ export JAVA_HOME=/Users/user/Library/Java/JavaVirtualMachines/corretto-22.0.1/Contents/Home
$ echo $JAVA_HOME
/Users/user/Library/Java/JavaVirtualMachines/corretto-22.0.1/Contents/Home
----

You must ensure that a link:{jdkDownloadUrl}[Java Development Kit] version {minJdkVersion} or higher is link:https://www.java.com/en/download/help/index_installing.html[properly installed], the `JAVA_HOME` environment variable is set, and Java is added to your `PATH`.

=== Permission denied

If you get "permission denied", that means that Gradle likely exists in the correct place, but it is not executable.
You can fix this using `chmod +x path/to/executable` on *nix-based systems.

=== Other installation failures

If `gradle —-version` works, but all of your builds fail with the same error, it is possible that one of your Gradle build configuration scripts is broken.

You can verify the problem with Gradle scripts by running `gradle help`, which executes configuration scripts but no Gradle tasks.
If the error persists, it means the build configuration is problematic.
If not, the problem exists when executing one or more requested tasks (Gradle executes configuration scripts first, followed by build steps).

[[sec:troubleshooting_dependency_resolution]]
== Debugging dependency resolution

You can see a dependency tree and see which resolved dependency versions differed from what was requested by clicking the _Dependencies_ view and using the search functionality, specifying the resolution reason.

image::troubleshooting-dependency-management-build-scan.png[]

The actual link:https://scans.gradle.com/s/sample/troubleshooting-userguide/dependencies?expandAll&filters=WzFd&toggled=W1swXSxbMF0sWzAsMF0sWzAsMV1d[Build Scan] with filtering criteria is available for exploration.

[[sec:troubleshooting_performance]]
== Troubleshooting slow builds

For build performance issues (including "slow sync time"), see <<performance.adoc#performance_gradle,improving the Performance of Gradle Builds>>.

Android developers should watch a presentation by the Android SDK Tools team about link:https://youtu.be/7ll-rkLCtyk[Speeding Up Your Android Gradle Builds].
Many tips are also covered in the Android Studio user guide link:https://developer.android.com/studio/build/optimize-your-build.html[on optimizing build speed].

[[sec:troubleshooting_build_logic]]
== Debugging build logic

=== Attaching a debugger to your build

You can set breakpoints and debug <<custom_plugins.adoc#custom_plugins,`buildSrc` and standalone plugins>> in your Gradle build itself by setting the `org.gradle.debug` property to "true" and then attaching a remote debugger to port 5005.
You can change the port number by setting the `org.gradle.debug.port` property to the desired port number.

To attach the debugger remotely via the network, you must set the `org.gradle.debug.host` property to the machine's IP address or `*` (listen on all interfaces).

----
❯ gradle help -Dorg.gradle.debug=true
----

Using the Kotlin DSL, you can debug the build scripts themselves.

The following video demonstrates how to debug an example build using IntelliJ IDEA.

image::remote-debug-gradle.gif[]

=== Adding and changing logging

In addition to <<command_line_interface.adoc#sec:command_line_logging,controlling logging verbosity>>, you can also control display of task outcomes (e.g. "UP-TO-DATE") in lifecycle logging using the <<command_line_interface.adoc#sec:command_line_customizing_log_format,`--console=verbose` flag>>.

You can also replace much of Gradle’s logging with your own by registering various event listeners.
One example of a <<logging.adoc#sec:changing_what_gradle_logs,custom event logger is explained in the logging documentation>>.
You can also <<logging.adoc#sec:external_tools,control logging from external tools>>, making them more verbose to debug their execution.

NOTE: Additional logs from the <<gradle_daemon.adoc#gradle_daemon,Gradle Daemon>> can be found under `$<<directory_layout.adoc#dir:gradle_user_home,GRADLE_USER_HOME>>/daemon/{gradleVersion}/`.

=== Task executed when it should have been UP-TO-DATE

`--info` logs explain why a task was executed, though a Build Scan does this in a searchable, visual way by going to the _Timeline_ view and clicking on the task you want to inspect.

.Debugging incremental build with Build Scan
image::troubleshooting-task-execution-build-scan.png[]

You can learn what the task outcomes mean from <<more_about_tasks.adoc#sec:task_outcomes,this listing>>.

[[sec:troubleshooting_ide_integration]]
== Debugging IDE integration

Many infrequent errors within IDEs can be solved by "refreshing" Gradle.
See also more documentation on working with Gradle link:https://www.jetbrains.com/help/idea/gradle.html[in IntelliJ IDEA] and link:http://www.vogella.com/tutorials/EclipseGradle/article.html[in Eclipse].

=== Refreshing IntelliJ IDEA

NOTE: This only works for Gradle projects link:https://www.jetbrains.com/help/idea/gradle.html#link_gradle_project[linked to IntelliJ].

From the main menu, go to `View` > `Tool Windows` > `Gradle`.
Then click on the _Refresh_ icon.

.Refreshing a Gradle project in IntelliJ IDEA
image::troubleshooting-refresh-intellij.png[]

=== Refreshing Eclipse (using Buildship)

If you're using link:https://projects.eclipse.org/projects/tools.buildship[Buildship] for the Eclipse IDE, you can re-synchronize your Gradle build by opening the "Gradle Tasks" view and clicking the "Refresh" icon, or by executing the `Gradle` > `Refresh Gradle Project` command from the context menu while editing a Gradle script.

.Refreshing a Gradle project in Eclipse Buildship
image::troubleshooting-refresh-eclipse.png[]

[[network_connection]]
=== Troubleshooting daemon connection issues

If your Gradle build fails before running any tasks, you may be encountering network configuration problems.
When Gradle is unable to communicate with the Gradle daemon process, the build will immediately fail with a message similar to this:

```
$ gradle help

Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
A new daemon was started but could not be connected to: pid=DaemonInfo{pid=55913, address=[7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[/127.0.0.1]], state=Busy, ...
```

This can occur when network address translation (NAT) masquerade is used.
When NAT masquerade is enabled, connections that should be considered local to the machine are masked to appear from external IP addresses.
Gradle refuses to connect to any external IP address as a security precaution.

The solution to this problem is to adjust your network configuration such that local connections are not modified to appear as from external addresses.

You can monitor the detected network setup and the connection requests in the daemon log file (`$<<directory_layout.adoc#dir:gradle_user_home,GRADLE_USER_HOME>>/daemon/<Gradle version>/daemon-<PID>.out.log`).

```
2021-08-12T12:01:50.755+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface enp0s3
2021-08-12T12:01:50.759+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback interface? false
2021-08-12T12:01:50.769+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding remote address /fe80:0:0:0:85ba:3f3e:1b88:c0e1%enp0s3
2021-08-12T12:01:50.770+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding remote address /10.0.2.15
2021-08-12T12:01:50.770+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface lo
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback interface? true
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /0:0:0:0:0:0:0:1%lo
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /127.0.0.1
2021-08-12T12:01:50.775+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Listening on [7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[localhost/127.0.0.1]].
...
2021-08-12T12:01:50.797+0200 [INFO] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertising the daemon address to the clients: [7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[localhost/127.0.0.1]]
...
2021-08-12T12:01:50.923+0200 [ERROR] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Cannot accept connection from remote address /10.0.2.15.
```

== Getting additional help

If you didn't find a fix for your issue here, please reach out to the Gradle community on the link:https://discuss.gradle.org/c/help-discuss[help forum] or search relevant developer resources using link:https://help.gradle.org/[help.gradle.org].

If you believe you've found a bug in Gradle, please link:https://github.com/gradle/gradle/issues[file an issue] on GitHub.
