The “Gradle sync failed” error
Coding apps in Android can be a lot of fun. But it can also be immensely frustrating. I recently updated my JDK, after Android Studio gave me a warning about using an outdated JDK. Then I got an error “Gradle sync failed: Please use JDK 8 or newer” as shown below.
I spent half a day, trying to solve this problem, but I finally nailed it.
Every app has a variable stored in the local.properties file, that points to the Java compiler environment to use. Maybe it’s just my old computer or there is a problem with Android Studio, but there is a problem recognizing the new path. I tried browsing to the new path, in the file explorer dialog box, but it kept going back to the old, non-existent path.
You first need to make sure that the new JDK installed properly. All Android needs is the JDK, not the JRE. The JDK package already contains a JRE in it, so there’s no need to download and separately install the JRE package. If you’re sure that the JDK is properly installed, run a java version command as shown below.
It can be run from any directory, not in the jdk as shown in the picture above. If you’re running a Windows system, like I am, ensure that your path has no spaces in it. Some people suggest that you copy your SDK to a path that has no spaces in it. But this isn’t necessary. Instead you can pass old style DOS paths to the Android Studio dialog.
Finding the old style DOS paths for any directory is easy. Open a command prompt and type in
This will display the old style DOS path as well as the usual full name you expect. For example I switched to a directory on my Desktop called ‘twentyfifteen’ and run the above command. This is the output I got.
After pasting in the new path, you may have to restart Android Studio and confirm that it has “accepted” the new path. This works most of the time.