26

I installed a fresh copy of Android Studio using terminal but when i create a new project it gives the error "Could not determine Java Version". JDK and SDK both are installed. Android Studio is updated to the latest version. I am using Ubuntu 14.04 LTS

Zoe
  • 27,060
  • 21
  • 118
  • 148
Zimad
  • 343
  • 1
  • 5
  • 13

11 Answers11

83

Change this:

targetCompatibility 'JavaVersion.VERSION_1_8'
sourceCompatibility 'JavaVersion.VERSION_1_8'

to this:

targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
Zoe
  • 27,060
  • 21
  • 118
  • 148
no_cola
  • 1,150
  • 8
  • 7
5

I faced the same issue, but on windows 7 OS. It was because multiple versions of java was present in the same location C:\Program Files\Java Java 5, 7, 8, and 9. Although the Environment variables JAVA_HOME and PATH were set to Java 8, Android studio was referring to Java 9.

My Solution, in the menu, click on File > Project Structure here, point JDK to appropriate location C:\Program Files\Java\jdk

Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
  • same [problem](https://stackoverflow.com/questions/55756186/gradle-wrapper-get-wrong-java-version-in-android-project-on-windows) for me, but after assigning proper jdk location, gradle still find a wrong java version. – Summer Sun Apr 19 '19 at 05:45
3

One potential problem is that older versions of Gradle can't parse the Java version from the java -version output produced by the OpenJDK. Newer versions can (I think it was updated around Gradle 2.2; the latest, 2.9, works).

Nick Spacek
  • 4,717
  • 4
  • 39
  • 42
  • see this on how to change the gradle version when using the default wrapper: http://stackoverflow.com/questions/25205113/how-to-change-the-version-of-the-default-gradle-wrapper-in-intellij-idea – Stefan Paul Noack Aug 02 '16 at 19:42
3

change distributionUrl in gradle/wrapper/gradle-wrapper.properties to new version of gradle

ked
  • 2,426
  • 21
  • 24
2

I faced the same issue, in ubuntu 16. It was because multiple versions of java was present in the same location 7, 8 and 9. Although the Environment variables JAVA_HOME and PATH were set to Java 8, Android studio was referring to Java 9.

My Solution, in the menu, click on File > Project Structure here, select 8 version

1

Did you set the JAVA_HOME environment? run echo $JAVA_HOME in your terminal and see if you get a valid path to your Java binaries.

if not, set the the JAVA_HOME:

nano .bashrc

Add the lines:

export JAVA_HOME=<path to jdk>
export PATH=$JAVA_HOME/bin:$PATH

Save the file and then reload the .bashrc

source ~/.bashrc
Filipe Batista
  • 1,862
  • 2
  • 24
  • 39
0

I simply changed default java JDK from Oracle JDK to OpenJDK. Works like a charm.

Zimad
  • 343
  • 1
  • 5
  • 13
0

Similar to Keds's answer. In android Studio, in Project Tab/File Explorer go to Gradle Scripts -> gradle.properties

You will see something like this:

#Sat Sep 21 13:08:26 CEST 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-2.4-all.zip

The distributionURL is old and, I'm assuming based on other answers, could not parse the Java version appropriately.

So picked a newer version by going to this site: http://services.gradle.org/distributions/

I picked

distributionUrl=http\://services.gradle.org/distributions/gradle-4.8.1-all.zip

just because. There are newer versions, such as 4.10.2, but I'm not really using gradle, it's just required for the project.

Also, although it didn't really affect me running my project, my project wasn't pointing to the new JDK. It was pointing to Android Studio's jre file automatically as recommended.

If you want to change this, get the file path location of the newest Java JDK. Usually in

C:\Program Files\Java\your_jdk_file

for windows. In Android Studio, go to File -> Project Structure -> SDK Location.

Uncheck 'Embedded JDK (recommended)', then paste the file path to the newest JDK file path for the entire file.

Such as

C:\Program Files\Java\jdk-10.0.2 

And not just the bin file. Good luck!

0

Nothing of this worked for me so in case it can help someone else : Right click on "app" in your project, go to "Open Module Settings">"Properties" and change the source compatibility and target compatibility to the Java version you need.

Vick Tim
  • 97
  • 7
0

well ,what solved the problem for me is that i didn't use embeded jdk and then set java version to java 7 and build it gave me error because of lambda expression then i set java version to 1.8 and sync then build and it worked

Islam Assem
  • 1,376
  • 13
  • 21
0

While opening an old android project with latest version of the android studio many compilation errors occur. However, following steps worked for me to update the project settings and build scripts.

Open File->Project Structure and make sure that

  1. you have selected the latest versions of the Android Gradle Plugin and Gradle version
  2. CompileSdkVersion and targetSdkVersion are same
  3. compileSdkVerrion and buildToolsVersion are compatible (if not exactly same)

In the project level build.gradle file make sure google() is added to the repositories for buildscript{} and allprojects{}.

In the module level build.gradle file, replace the word compile with implementation and testCompile with testImplementation. In the compileOptions{} block remove the single quotes from around the JavaVersion… string.

Synchronize again and make the project.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
asim ali
  • 1
  • 2