179

I'm getting following error when trying to compile my project in Android Studio:

Gradle: error: diamond operator is not supported in -source 1.6

I have 1.7 set as target in all project preferences I've found. Also the path displayed in project SDK's under 1.7 SDK is correct path to java 1.7 installation.

Even when I run java -version in terminal, it tells me I'm running on java 1.7.

I have tried to set JAVA_HOME env variable to this:

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

The error does not go away. How do I eliminate the error?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
simekadam
  • 7,334
  • 11
  • 56
  • 79

8 Answers8

291

Java 7 support was added at build tools 19. You can now use features like the diamond operator, multi-catch, try-with-resources, strings in switches, etc. Add the following to your build.gradle.

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

Gradle 1.7+, Android gradle plugin 0.6.+ are required.

Note, that only try with resources require minSdkVersion 19. Other features works on previous platforms.

Link to android gradle plugin user guide

Link to see how source vs target are different

Community
  • 1
  • 1
Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
73

Maybe these answers above are old but with the new Android Studios 1, you do the following to see the module to run on 1.7 (or 1.6 if you prefer). Click File --> Project Structure. Select the module you want to run and then under "Source Compatibility" and "Target Compatibility", select 1.7. Click "OK".

Project Structure screen of Android Studios 1

Simon
  • 19,658
  • 27
  • 149
  • 217
14

You can change it in new Android studio version(0.8.X)

FIle-> Other Settings -> Default Settings -> Compiler (Expand it by clicking left arrow) -> Java Compiler -> You can change the Project bytecode version here

enter image description here

San
  • 5,567
  • 2
  • 26
  • 28
8

Latest Android Studio 1.4.

Click File->Project Structure->SDK Location->JDK Location.

You could also set individual module JDK Version compatibility by going to the Module (below the SDK Location), and edit the Source Compatibility accordingly. (note, this only applies to Android Module).

Elye
  • 53,639
  • 54
  • 212
  • 474
5

Right click on your project > Open Module Setting > Select "Project" in "Project Setting" section

Change the Project SDK to latest(may be API 21) and Project language level to 7+

kunal
  • 51
  • 1
  • 1
4

At current, Android doesn't support Java 7, only Java 6. New features in Java 7 such as the diamond syntax are therefore not currently supported. Finding sources to support this isn't easy, but I could find that the Dalvic engine is built upon a subset of Apache Harmony which only ever supported Java up to version 6. And if you check the system requirements for developing Android apps it also states that at least JDK 6 is needed (though this of course isn't real proof, just an indication). And this says pretty much the same as I have. If I find anything more substancial, I'll add it.

Edit: It seems Java 7 support has been added since I originally wrote this answer; check the answer by Sergii Pechenizkyi.

Community
  • 1
  • 1
blalasaadri
  • 5,990
  • 5
  • 38
  • 58
  • 2
    Here, a similar question with a link on how to overcome the problem: http://stackoverflow.com/questions/14487682/diamond-operator-is-not-supported – blalasaadri Jul 14 '13 at 10:45
2

Always use latest SDK version to build:

compileSdkVersion 23

It does not affect runtime behavior, but give you latest programming features.

DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
2

Go into your Gradle and look for sourceCompatibility and change it from 1.6 to 7. That worked for me at least.

You can also go into your module settings and set the Source/Target Compatibility to 1.7.

Module settings window

That will produce the following code in your Gradle:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
Klez
  • 21
  • 1
  • 3