Android Developers get "Gradle project sync failed" or "Gradle sync failed" error in Android Studio when project gradle is unable to sync properly. This maybe caused due to a number of reasons including incorrent versions in project settings, corrupt cache, network problems and wrong dependencies. Gradle sync fail error happens commonly due to following reasons;
- You updated the Android Studio version and Gradle plugin failed to update correctly.
- You tried updating Gradle version but used incorrect plugin version or distributionUrl.
- Android Gradle Plugin and Gradle don't match supported versions.
- Cache is corrupted due to netword connection problem.
- Gradle distribution file of new version is corrupted due to incomplete download.
Before changing anything to fix the gradle sync error, make sure that your internet connection is working properly. If you have no network problem then look at the project settings and Gradle Scripts to see if there are any errors there.
Once you update Android studio, you are asked to update Gradle version also. You can do this manually or automatically. You have to make sure that required Gradle version is installed for Android gradle version. You can see the list of required versions on Android Developer's page.
For example: If you want to update Android Gradle Plugin to version 4.0.1, for this the required Gradle version is 6.1.1. You always have to ensure that correct versions are used in project structure settings and Gradle scripts.
If you changed the gradle scripts incorrectly then you can follow the following steps to fix gradle settings.
You can see the Gradle Version to install in Gradle Scripte > gradle-wrapper.properties as distributionURL. This provides the source of gradle zip file that is downloaded for updating to required gradle version.
If distributionURL is correct then check the Plugin version in project build.gradle file. Classpath in project dependencies should correspond to the correct Android Gradle Plugin version.
Lastly open Project Structure from File menu and ensure that Android Gradle plugin version and Gradle version are correct.
Once the Plugin and Gradle versions match in project structure and gradle scripts, you can restart Android Studio to see if it solves the Gradle Sync errorr. If it doesn't then try to invalidate the cache using File / Invalidate Chache.
Android Studio > File > Invalidate Cached / Restart
This will reset the cache and should solve any issue caused by corrupt cache data. If the problem persists then you will need to manally delete the cache files in the .gradle folder on your system.
Gradle sync failed: Cause: error in opening zip file
If you get an error that gradle sync failed due to an error in opening zip file, then chances are that the Gradle file has been corrupted somehow during download. Gradle doesn't resolve this problem automatically so you will have to remove the corrupte zip file manually. To remove the download cache, find the .gradle folder in your system and delete the downloaded version in wrapper subfolder. This will allow the Android studio to downlaod the new Gradle version zip file again and install it correctly.
- Close Android studio.
- Find the .gradle folder in your system. Go to /user/.gradle on Windows and on Mac by writing ~/.gradle/ in the spotlight search box.
- Open the .gradle folder and then Wrapper directory and then delete the file folder with gradle version you are trying to install e.g. gradle-6.1.1-all.
- Resync gradle in the android studio.
It will download and install the gradle distribution and build the project. These steps should solve any gradle sync failed errors due to incorrect settings, version control or corrupt cache. Once all settings are used correctly and corrupt cache files are deleted, restart Android studio and sync gradle to build the projects. Update gradle version will be downloaded and installed from the zip file. It might take a few minutes so wait for the Gradle sync to complete.