Describe a scenario where you had to troubleshoot a complex build script in Gradle.

Troubleshooting a complex Gradle build failure is a process of narrowing down the root cause using Gradle's diagnostic flags and dependency insight tooling, rather than guessing at fixes. The --stacktrace and dependencyInsight tools are usually the fastest path to the real cause of resolution errors.

Key Points: • Run the build with --stacktrace (or --full-stacktrace) to get the actual underlying exception instead of a truncated summary. • Use gradle dependencyInsight --dependency <name> to see every path that pulls in a given dependency and which version "won." • --info or --debug can reveal exactly which task or plugin is misbehaving when the failure isn't a clear dependency issue. • Once the conflicting or misconfigured dependency is identified, resolutionStrategy.force or an explicit version constraint fixes it. • Reproducing the issue with a minimal build.gradle can help isolate whether the problem is in your logic or a plugin's.

Example: A build failed with an opaque "could not resolve" error; running with --stacktrace surfaced a version conflict, and dependencyInsight then showed two different modules requesting incompatible versions of the same library, which was fixed by forcing a single version in resolutionStrategy.

Code Example:

configurations.all {
    resolutionStrategy {
        force 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
    }
}

Interview Tip: A concise interview answer is:

"When I hit an obscure Gradle failure, I run with --stacktrace to get the real exception, then use dependencyInsight to trace exactly which modules are pulling in a conflicting version. Once I've identified the conflict, I fix it with a resolutionStrategy.force or an explicit version constraint rather than guessing at the build script."