How to Fix 'Could not resolve all dependencies' Error in Gradle Builds
The "Could not resolve all dependencies" error is a common hurdle faced by developers working with Gradle. It's frustrating when your build fails, especially when you're just trying to get your project up and running or integrate a new library. But don't worry, this guide will walk you through understanding, diagnosing, and ultimately fixing this pesky problem.
Problem Explanation
When you encounter the "Could not resolve all dependencies" error, Gradle is essentially telling you that it tried to find one or more of the external libraries (dependencies) that your project needs to compile or run, but it couldn't locate them. The error message typically appears during the configuration or compilation phase of your build.
You'll usually see output similar to this in your terminal or IDE build window:
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
> Could not find com.example:some-library:1.0.0.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/com/example/some-library/1.0.0/some-library-1.0.0.pom
- https://repo.maven.apache.org/maven2/com/example/some-library/1.0.0/some-library-1.0.0.jar
Required by:
project :app
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
The key part here is "Could not resolve all dependencies for configuration..." followed by "Could not find groupId:artifactId:version". This clearly indicates which specific dependency Gradle failed to locate and often lists the repositories it checked.
Why It Happens
This error occurs when Gradle cannot successfully download or access one or more of the required external libraries that your project declares. There are several common reasons why this might happen:
- Network Connectivity Issues: Your machine might not have an active internet connection, or a firewall might be blocking Gradle's access to external dependency repositories. If you're behind a corporate proxy, Gradle might not be configured to use it.
- Incorrect Repository Configuration: Gradle needs to know where to look for dependencies. If the repository containing a specific library (e.g., Maven Central, Google Maven, a private Artifactory, or JCenter for older projects) is not declared or is misspelled in your
build.gradleorsettings.gradlefile, Gradle won't find it. - Typos or Non-Existent Dependencies: A simple typo in the
groupId,artifactId, orversionof a dependency declaration will lead Gradle to search for something that doesn't exist. Similarly, you might be requesting a version of a library that has not been published yet or has been removed from the repository. - Gradle Cache Corruption: Over time, Gradle's local cache (where it stores downloaded dependencies) can become corrupted, leading to issues even if the dependency exists in remote repositories.
- Offline Mode Enabled: If Gradle is explicitly running in "offline mode," it will not attempt to fetch any new dependencies from remote repositories, relying solely on its local cache. If a dependency isn't in the cache, it will fail.
Understanding these root causes is the first step toward effective troubleshooting. Now, let's dive into the solutions.
Step-by-Step Solution
Follow these steps to diagnose and resolve the "Could not resolve all dependencies" error in your Gradle build.
## Step 1: Understand the Error Message and Identify the Culprit
The most crucial piece of information is the specific dependency that Gradle failed to resolve. Look for lines in the error output like:
> Could not find com.example:some-library:1.0.0.
This tells you exactly which groupId, artifactId, and version Gradle couldn't locate. Make a note of this dependency, as it will be your primary focus for the subsequent steps. The error message also often indicates where Gradle looked (e.g., https://repo.maven.apache.org/maven2/...), which can be a clue if a repository is missing.
## Step 2: Check Network Connectivity and Proxy Settings
A surprising number of dependency resolution issues are due to basic network problems.
-
Verify Internet Connection: Open a web browser and try to access a public site like
https://repo.maven.apache.org/maven2/orhttps://google.com. If you can't access these, your internet connection is the problem. -
Ping a Repository Host: From your terminal, try pinging one of the repository hosts that Gradle mentioned in the error message (e.g.,
ping repo.maven.apache.org). If it fails, there's a network issue or firewall blocking access. -
Configure Proxy Settings (if applicable): If you're behind a corporate proxy, Gradle needs to be aware of it. You can configure proxy settings in your
gradle.propertiesfile (located in your Gradle user home directory, usually~/.gradle/on Linux/macOS or%USERPROFILE%\.gradle\on Windows, or within your project root for project-specific settings). Add the following lines, replacing the placeholders with your proxy details:systemProp.http.proxyHost=your.proxy.host systemProp.http.proxyPort=8080 systemProp.https.proxyHost=your.proxy.host systemProp.https.proxyPort=8080 systemProp.http.nonProxyHosts=localhost|127.0.0.1You might also need to specify proxy authentication if your proxy requires it.
systemProp.http.proxyUser=your_username systemProp.http.proxyPassword=your_password systemProp.https.proxyUser=your_username systemProp.https.proxyPassword=your_passwordAfter configuring, try running your build again.
## Step 3: Verify Repository Configuration
Gradle needs to know which repositories to search for dependencies. These are declared in the repositories {} block, typically in your build.gradle file (at the project level or module level) or sometimes in settings.gradle.
-
Check
build.gradleandsettings.gradle: Open your project'sbuild.gradle(orbuild.gradle.kts) andsettings.gradle(orsettings.gradle.kts) files. Ensure that all necessary repositories are declared. Common public repositories include:repositories { mavenCentral() // Essential for most Java/JVM projects google() // Essential for Android projects jcenter() // Deprecated, but still used by older projects // Add any custom or private repositories here maven { url 'https://repo.spring.io/milestone' } // Example for Spring milestones } -
Ensure All Required Repositories are Present: If the problematic dependency is from a specific vendor or framework (e.g., Spring milestones, Jetpack Compose, a private company library), ensure that its corresponding repository URL is correctly added. A good practice is to declare
repositories {}in the rootbuild.gradlefor multi-project builds and useallprojects { repositories { ... } }orsubprojects { repositories { ... } }to apply them to all modules. -
Check Repository Order: While usually not the cause of "could not resolve," repository order can sometimes affect which version is picked. Generally, place more specific or faster repositories first.
## Step 4: Scrutinize Dependency Declarations for Typos or Incorrect Versions
Now that you know which dependency is problematic (from Step 1), carefully examine its declaration in your build.gradle file.
- Exact Match: Ensure the
groupId,artifactId, andversionexactly match what Gradle reported as "Could not find." Even a single character typo will prevent resolution.- Example:
implementation 'com.example:some-library:1.0.0'
- Example:
- Verify Against Official Sources: Go to Maven Central (
https://search.maven.org/) or the official documentation/website for the problematic library. Search for the library using itsgroupIdandartifactIdto confirm that the specificversionyou're requesting actually exists and is spelled correctly. Sometimes, a developer might release a library under a slightly differentartifactIdorgroupId. - Avoid Dynamic Versions (for troubleshooting): If you're using dynamic versions like
1.0.+orlatest.release, temporarily replace them with a fixed, known-good version (e.g.,1.0.0) to rule out issues with Gradle's dynamic version resolution.
## Step 5: Clear the Gradle Cache and Re-sync
Gradle maintains a local cache of downloaded dependencies to speed up builds. Sometimes, this cache can become corrupted or outdated, leading to resolution failures.
-
Use
--refresh-dependencies: The most common and safest way to clear the cache for specific dependencies is to run your build command with the--refresh-dependenciesflag. This forces Gradle to ignore locally cached versions and fetch them anew from remote repositories../gradlew clean build --refresh-dependencies # or just: ./gradlew --refresh-dependenciesThe
cleantask ensures that any compiled artifacts are removed before a fresh build, which is a good practice. -
Manually Delete Cache (if persistent): If
--refresh-dependenciesdoesn't work, you can manually delete Gradle's caches. This is a more aggressive step and will force Gradle to re-download everything for all projects.- Location:
- Linux/macOS:
~/.gradle/caches - Windows:
%USERPROFILE%\.gradle\caches
- Linux/macOS:
- Delete the entire
cachesdirectory. Then, try running your build again:./gradlew clean build.
- Location:
-
IDE Cache Invalidation: If you're using an IDE like IntelliJ IDEA or Android Studio, sometimes their internal caches can also cause issues.
- Go to
File > Invalidate Caches / Restart... - Select "Invalidate and Restart" (and optionally "Clear file system cache and local history").
- Go to
## Step 6: Check for Gradle Offline Mode
Gradle has an "offline mode" feature that prevents it from accessing the internet to resolve dependencies. While useful for working without network access, it's a common cause of this error if accidentally enabled.
- Command Line: Ensure you are not passing the
--offlineflag when running your build.- Correct:
./gradlew build - Incorrect (if you need to resolve dependencies):
./gradlew build --offline
- Correct:
- IDE Settings:
- Android Studio/IntelliJ IDEA: Go to
File > Settings(orAndroid Studio > Preferenceson macOS)> Build, Execution, Deployment > Gradle. Ensure the "Offline work" checkbox is unchecked. - Eclipse (with Buildship): Check your workspace or project settings for "offline mode" under Gradle.
- Android Studio/IntelliJ IDEA: Go to
## Step 7: Investigate Specific Version Conflicts or Build Script Issues
While "Could not resolve" usually means "could not find," sometimes deep version conflicts can manifest in similar ways or block resolution.
-
Inspect Dependency Tree: If the above steps don't work, there might be a more subtle issue. Use Gradle's
dependenciestask to inspect the full dependency tree for your project:./gradlew dependencies --configuration <configurationName>Replace
<configurationName>with the specific configuration mentioned in the error (e.g.,debugRuntimeClasspath,implementation,testImplementation). This can reveal conflicts or unexpected dependency versions. -
Gradle Sync Issues: If using an IDE, try forcing a Gradle sync (e.g., the "Sync Project with Gradle Files" button in Android Studio or IntelliJ). This can sometimes resolve transient issues.
Common Mistakes
When troubleshooting this error, developers often make a few common mistakes that can prolong the resolution process:
- Ignoring the Specific Error Message: Many users immediately jump to clearing caches without first identifying the exact dependency that failed. The error message is your best clue!
- Forgetting
--refresh-dependencies: Simply runninggradlew clean buildoften isn't enough. If the issue is with cached dependency metadata,cleanwon't force a fresh download. Always add--refresh-dependencieswhen dealing with resolution errors after making changes to repositories or dependencies. - Missing a Required Repository: Assuming all dependencies are in Maven Central or Google Maven is a common pitfall. Many libraries (especially older ones, snapshots, or company-internal ones) reside in other repositories that must be explicitly added.
- Typo in Dependency Coordinates: It's surprisingly easy to misspell a
groupId,artifactId, orversion. Double-check these against official documentation or Maven Central. - Unintentionally Running in Offline Mode: Forgetting that offline mode was enabled in an IDE or via a command-line flag can lead to confusion when dependencies aren't resolved.
Prevention Tips
Preventing this error from occurring in the first place saves a lot of time and frustration. Implement these best practices in your Gradle projects:
- Centralize Repository Declarations: For multi-project builds, declare your
repositories {}block in the rootbuild.gradlefile and apply it to all subprojects usingallprojects { repositories { ... } }. This ensures consistency across your entire project. - Use Dependency Management Tools: Leverage Gradle's Bill of Materials (BOMs) or Spring Boot's dependency management plugin to manage versions centrally. This helps prevent version conflicts and ensures that compatible versions of related libraries are used.
- Pin Known-Good Versions: Avoid using dynamic versions (e.g.,
1.0.+orlatest.release) in production or shared development environments. While convenient, they can lead to unreproducible builds if a new version is released that breaks compatibility. Stick to explicit version numbers. - Regularly Review Dependency Declarations: Periodically audit your
build.gradlefiles to ensure all declared dependencies are still necessary and have correct coordinates and versions. Remove unused dependencies. - Utilize
gradle.propertiesfor Environment-Specific Config: For sensitive information like proxy credentials or repository URLs that change per environment, usegradle.properties. Consider placing this file outside of version control for security. - Stay Updated with Gradle and Plugin Versions: Keep your Gradle wrapper and essential plugin versions reasonably current. Newer versions often include bug fixes and improvements to dependency resolution logic.
- Version Control Your
gradlewandgradle-wrapper.properties: Always commit yourgradlew(orgradlew.bat) script and thegradle/wrapper/gradle-wrapper.propertiesfile to version control. This ensures everyone on your team uses the same Gradle version and wrapper settings, enhancing build reproducibility.
By following these steps and incorporating prevention tips, you'll be well-equipped to tackle the "Could not resolve all dependencies" error and maintain a smooth, reliable Gradle build process.