64

I decided to update my JDK to Java 8, and installed to the default location of C:\Program Files\Java\jdk1.8.0 with a jre subdirectory.

I wasn't sure how Android Studio worked out the JDK location, so I decided to launch it and see. I got the following message:

Failed to complete Gradle execution.

Cause: Supplied javaHome is not a valid folder. You supplied: C:\Program Files\Java\jdk1.7.0_45

I updated my JAVA_HOME environment variable (both for system and user) to point to the new path, and tried adding it to my PATH variable as well, but every time I attempt to build in Android Studio I get the same message. The quoted path is nowhere to be found in my environment variables though, so where is it getting it from and how can I change it?

Thanks

Ellis
  • 3,048
  • 2
  • 17
  • 28

10 Answers10

149

The problem was my project's JDK location. From the Android Studio Project tool window, I right clicked on the root directory, selected 'module settings', and set the JDK location. Everything's fine now.

Ellis
  • 3,048
  • 2
  • 17
  • 28
  • 21
    This kind of thing is what I seem to spend 50% of my time with Android Studio on. Of course you'd expect to see this in Module Settings! Searching the Settings for javaHome returns nothing. And it completely ignores the JAVA_HOME environment variable that is already set. Every time you upgrade the JDK it breaks. – core24 Jun 24 '14 at 03:54
  • Judging by the fact that this answer continually seems to gain upvotes, I'd say you're not alone there! Of course, Android Studio is still in beta so we have to hope these things will be a bit smoother by the time 1.0 rolls out. Personally I still prefer to use it over Eclipse. – Ellis Jun 24 '14 at 07:33
  • 1
    I just got this running Android Studio 0.8.6 with the 64 bit JDK 8 update 20. Once set I could compile and run the app. I tried a text search in my project folder and couldn't find the old version 7 JDK text anywhere. The JAVA_HOME environment variable was around but it doesn't seem to use it. I was setting it up on a new PC. Still I agree with @Ellis that it's better than Eclipse. – Kioshiki Sep 13 '14 at 14:24
  • 1
    Ah yes, this keeps happening to me and I have to look it up here every time. Not an intuitive thing at all. Android Studio is still a billion times better than Eclipse though! :-) – Brian Knoblauch Oct 24 '14 at 17:10
  • 1
    If anyone needs to leave the project settings alone, you can add a shortcut for your JDK inside the module settings folder where it's looking and it works just fine. – Jacob Holloway May 19 '15 at 13:45
  • I hate everything related to Java. The tools just never work! What a waste of time. I wish I could do everything in C++. – user2061057 Feb 19 '16 at 10:02
8

I don't know why or how this problem occurred but this answer got me over the hump. On macOS rm -rf ~/.gradle to wipe out the cache then restart Android Studio. I tried everything else up to and including reinstalling the JDK and Android Studio. Wiping out the .gradle directory is the only thing that worked.

Victor Ude
  • 415
  • 4
  • 13
  • 3
    Only solution which worked for me, also do not upgrade gradle after this when it asks – Steffan Sep 24 '21 at 10:47
  • 1
    Man, I was going crazy for not being to set up an existing project that ran perfectly on a previous laptop, and wondering all the time where the old invalid path still lives. This is the solution. Can't believe how absolutely unintuitive `gradle` works... smh. Thanks so much for the answer. – xji Feb 04 '22 at 23:47
  • 2
    That solved the problem on Pop_OS! (an Ubuntu variant) immediately. – Lars Blumberg Jul 12 '22 at 16:28
5

Try to delete all .iml files and .idea, and re-import the project from Gradle. Worked for me (provided you have everything else set up).

This error happens when you switch from Intellij to Android Studio, or the other way around, and load the same project. Looks like their project model is not compatible.

Desik
  • 604
  • 2
  • 8
  • 21
3

For Android Studio 1.1.0, after right-clicking on the project:
Select Open Module Settings
Select SDK Location at top left
Type text into JDK Location: box

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
3

I ran into the same issue. My jdk was upgraded from a 1.8.0_172 to 1.8.0_202. And I kept getting the mentioned error although I changed to the right jdk, in the module configurations. What confused me was the fact that the API version was stamped with the old *_172 version in the dropdown of available project sdk's. I found the reason for this to be an old obsolete entry in the dropdown that referenced the old *172. After deleting this entry, the APi versions was magically stamped with the new version.

Java versions

Note to self: Remember to sanitize your projects when new versions come along

stuf99
  • 95
  • 6
  • This is the one that worked for me, just a note that I had to **restart IntelliJ** after deleting the obsolete SDKs in order for it to change to the new version. – c z Jun 22 '20 at 08:57
2

I was also seeing this same problem. For me the fix was in 'Run/Debug Configurations' (Run > Edit Configurations...)

The 'Gradle project' value was set to the build.gradle file location, and this should've been set to the Gradle project object in IDEA, click the folder looking icon not the '...'.

Sooth
  • 2,834
  • 23
  • 26
0

I'm in 1.0.2 and after I clicked OK, it immediately opened a dialog to allow me to specify the location for my project, no longer requiring navigation of the menus to edit the project JDK path.

Definitely strange that during install, it says the JAVA_HOME environment variable is set to a path that doesn't contain the JDK. After spending time fixing it (downloading the full 64 bit JDK), I create my first project and get the same thing. Because it's not using the environment variable that it hinges on during setup.

Odd behavior.

omJohn8372
  • 154
  • 1
  • 6
0

Try removing the .dart_tool, .idea, .vscode folders as well as the pubspec.lock file. Then run "flutter clean" and "flutter pub get".

-1

For some reason I failed using all the above answers, so what I did is to delete (actually moved, so that I could go back in case something goes wrong) the ".idea" folder from the project. Then I opened the IDE again, and it went fine.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
-1

Getting this in 2021? Try to to rm -rf ~/.gradle and restart Android studio.

Luca
  • 558
  • 4
  • 18