54

Did something to trigger a gradle sync (e.g. added a module that has its own compileSdkVersion different from my project's, as seen in its own build.gradle file), but after seeing Gradle sync started in the Event Log, I quickly run into:

Gradle sync failed: Sync failed: reason unknown

Tried solutions

  1. Invalidate cache and restart
  2. reinstalled android studio and recloned project
  3. changed the gradle version
  4. removed .gradle and .idea folder
  5. run gradle build from cli
  6. toggling offline mode
  7. Manually pointed gradle files

[using Android studio 4.2.1 and gradle 6.5, but can encounter this problem with other IDE/gradle combinations, as the problem most likely stems from the dependencies I've changed.]

qix
  • 7,228
  • 1
  • 55
  • 65
Karthik M
  • 563
  • 1
  • 4
  • 7

11 Answers11

70

In your screenshot, there is a infomation bar on top of the editor area "Gradle project sync failed. Basic funtionality will not work." Click the "Show log in finder", you can check the real reason in the log file.

In my case, I found “ Module: 'xxxxx' platform 'android-26' not found” which mean the platform 26 is missing. After download the platform api level 26, problem solved.

situee
  • 2,710
  • 2
  • 22
  • 27
  • 17
    Thanks a lot for the help. For anyone else reading this comment, all you need to do is open any file in your project that can be edited in the center editor area and the header with the "Show Log in Finder" should be shown. – Fearnbuster Sep 13 '21 at 21:36
  • I had the same problem, I looked at the log and the API level 30 was missing, I downloaded it and it worked. Thank you – Diego Farias Jan 06 '22 at 01:14
  • Yeah, read that `idea.log` file. It provieds all the information you need to fix the issue. – OneWorld Jan 17 '22 at 09:57
  • 2
    Well there's 2 hours down the pisser thanks AS team ;) – Daniel Wilson Jan 17 '22 at 15:30
  • In my case closing AS, deleting this log file solve the error, its surprising but its true. Don't know why this log file was creating problem of "Gradle sync failed: Sync failed: reason unknown (32 s 930 ms)" – Smeet Mar 09 '22 at 08:39
23

There is great post here: https://www.tehrir.com/gradle-sync-failed in this matter.

Make sure your Android Gradle Plugin version matches Gradle version according to this page: https://developer.android.com/studio/releases/gradle-plugin#updating-gradle

enter image description here

If it matches and still gives same error try upgrading to a higher "tier":

4.2.1 and 6.7.1

It worked for me.

Variag
  • 1,056
  • 10
  • 25
14

I got a similar issue in the new Arctic Fox version. After looking at the idea.log (it should be at /Users/username/Library/Logs/Google/AndroidStudioXX.XX/idea.log), it looked like I was missing some Android SDKs that are needed for the external libraries (I'm working on react native).

  1. Just look for the lines that say Module: xx platform 'android-yy' not found.
  2. Use the SDK Manager and install the SDK version yy
  3. Invalidate cache and restart

In my case, I had multiple missing SDKs, so it didn't work after restarting; I had to repeat steps 1 to 3 another time.

ouflak
  • 2,458
  • 10
  • 44
  • 49
alloystory
  • 145
  • 1
  • 5
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29787419) – Kishan Solanki Sep 10 '21 at 07:59
  • This answer saved my life as a newbie Android developper – BoCyrill Oct 12 '21 at 15:53
  • Had this issue with ExoPlayer as a lib. idea.log showed: `ERROR - intellij.openapi.progress.Task - Module: 'exoplayer-extension-cast' platform 'android-28' not found.` - after adding SDK API Level 28 the project was fixed. – Ben Nov 01 '21 at 10:53
  • You saved my day.. thanks! – Wijay Sharma Nov 17 '21 at 16:49
13

When installing new environment, go to SDK Manager and install necessary API (ex: from 28 to 33)

Finally go to Gradle of App level and Sync Now again.

dotrinh PM
  • 903
  • 1
  • 7
  • 19
  • 1
    absolutely correct, thanks. just open your current project gradle (app level) and check target and compile sdk then open sdk manger and download the necessary api – Amr Nov 28 '21 at 10:33
  • 1
    THIS is what working for me. Thankfully this is the first solution I tried, instead of all the other answers here. I was a bit sceptical at first. But after trying, the gradle really does sync well without issue. – Chen Li Yong Dec 31 '21 at 17:21
4

Did you install all the SDK packages required on Android Studio?

'Cause I solved installing some SDK packages I didn't install previously. Check if you have missed them.

For understand when you have done, the Event Log tab reports these messages (look at the img) https://i.stack.imgur.com/ids61.png

BatAmbra
  • 41
  • 3
  • can you add some text or without the need of redirection (like image in different website etc.) stuff ? – Samet Sökel Aug 27 '21 at 15:19
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 27 '21 at 15:19
  • The image (screenshot) I've posted after I took it on Android Studio is useful for understanding what the Log tab reports when the Gradle issue is solved. As when that problem appears there aren't furter details about the cause of it, the only thing allows you understanding you have done, is the Log tab. – BatAmbra Aug 30 '21 at 08:54
2

I had this same issue and i fixed it in the following steps after trying lots of stuff all in vain.

  1. Check the compileSdkVersion 30 and targetSdkVersion 30 values in your app gradle file.

  2. If the values are say 30, you must make sure that when you click the SDK Manager at the top left corner of your Android studio, you will have this SDK level installed.

  3. Otherwise you can install it. from the dialog box that will have popped up. In my case, my app gradle file from a pull request of my company's code was 30 but since i had just updated android studio i had level 31 only installed.

  4. So instead of going through the hassle of installing SDK level 30 of which i was in a rush to do other stuff, I just switched my values from 30 to 31 in the gradle app files.

And everything worked after gradle sync. Then when i had the time, i would later go back to install level 30 and revert the values.

Hope this helps someone as I had spent some good minutes trying to figure out the error

NMukama
  • 1,114
  • 9
  • 10
1

If you have a fresh installation of android studio , you should

  1. Install java if not already installed (If you re-installed windows) skip this step otherwise
  2. Menu -> Invalidate caches/restart and try to sync Gradle afterwards (If issue still persists go to step 3)
  3. go to tools->SDK Manager-> and install any missing SDK version (Sync Gradle afterwards) if all the android versions are installed prior to your targeted version and subsequent to your compiled SDK version go search another thread on Stack Overflow! else go to step 4
  4. Accept debugging dialog/keys at your android device if you keep the device connected and are waiting to show up in run configuration (Wait few seconds to let the dialog show up. Go to step 5 if dialog not presented in step 4)
  5. Disconnect the USB cable and reconnect again.

make sure to accept the SDK license in step 4.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
puneet yadav
  • 107
  • 4
  • 1
    Step #3 could be a bit clearer; you shouldn't need to install all SDKs, but at the very least you should have the `compileSdkVersion` version for your own app module installed, plus those of any modules you depend on, all specified in their respective `build.gradle` files. In the case of `flutter`, building with their tool should automatically download the necessary SDKs to build all your extra dependencies. But when viewing within Android Studio, I've encountered this error before when told to sync with gradle files, and saw the missing SDK clue in the Build tool window's errors. – qix May 24 '22 at 07:54
1

Tried all the options mentioned above and finally deleted the .gradle folder within the project structure and invalidate caches to restart the intellij, this resolved the issue

Sunil Kumar
  • 152
  • 1
  • 1
  • 12
0
  1. Just go to "Run/edit configurations" and scroll down to bottom of the window and here you can see option "Before launch"
  2. Remove whatever already inside the little window.
  3. Then click on (+) icon and select "Gradle-aware Make" and then type "assembleDebug" and select the first option or that you need. This will solve your problem.
Muhammad Zahab
  • 1,049
  • 10
  • 21
0

I also have exact errors and tried all solutions, but I solved this by adding Android Virtual Device.

In my case, I added two virtual devices with minimum API level and Maximum API level of my project. Then Restarting the Android Studio Sync Gradle and solved the problem.

Try this with Stable Network Connection.

0

I had a fresh installation of Android Studio and started facing this problem. After trying all the various solutions, I found that the SDK Platform was not installed for API Level 30, but was installed for API Level 31, and my project was using API Level 30.

So I installed API level 30 SDK Platform and the issue got resolved.

olleh
  • 1,915
  • 18
  • 21