32

I need 1.7 for a library which uses diamond operators.

I followed these sO answers...but no joy.

From gradle build

    compileSdkVersion 19
buildToolsVersion '19.0.3'

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

defaultConfig {
    minSdkVersion 10
    targetSdkVersion 19
    versionCode 22
    versionName "1.3.1"
}

Error: Execution failed for task ':MyApp:compileDefaultFlavorDebugJava'.

invalid source release: 1.7

Java home is set : $ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

Community
  • 1
  • 1
serenskye
  • 3,467
  • 5
  • 35
  • 51

4 Answers4

28

check your JVM versions. It should not be 1.6.

./gradlew --version
javac -version

on OSX check your $JAVA_HOME

echo $JAVA_HOME

You can set your JAVA_HOME environment variable in ~/.bash_profile JDK:

/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
8

Go to Project Structure->SDK Location and make sure the JDK Location is set to the correct location of your JDK. Setting JAVA_HOME will fix it for command line builds, but in Android Studio it still failed for me until I fixed this setting.

Android Studio JDK location

Kekoa
  • 27,892
  • 14
  • 72
  • 91
0

The answers here require you to make global changes using environment variables or IDE settings which may not be ideal if you have to support multiple projects and JVMs or simply don't want to make global permanent changes.

The proper way to set the JVM version in Gradle is to add a property setting in your gradle.properties file. The property is org.gradle.java.home and the value should be the path to the root directory of your JDK or JRE installation.

PeterToTheThird
  • 353
  • 1
  • 2
  • 10
0

A more system independent way to resolve this, is to use Gradle's toolchains. When a toolchain is specified, Gradle will search common system locations and locations you configure for JRE/JDKs matching the specified Java version. For versions of Java after 1.7, Gradle will download the required a JDK, if it cannot be located locally (unless auto-provisioning has been manually disabled).

If Java 1.7 is installed, the following addition to build.gradle should resolve the issue:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(7)
    }
}

In cases where the JDK/JRE is installed in a location not searched by Gradle, an error No locally installed toolchains match will occur on any Gradle project tasks. To resolve this, the gradle.properties (in the GRADLE_USER_HOME) should be updated to include the location of the JDK/JRE.

org.gradle.java.installations.paths=/usr/lib64/openjdk-7
Eric G
  • 1