How to set -source 1.7 in Android Studio and Gradle

130,112

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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).

Solution 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+

Share:
130,112

Related videos on Youtube

simekadam
Author by

simekadam

Android developer at Avast Mobile.

Updated on December 01, 2020

Comments

  • simekadam
    simekadam over 3 years

    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?

    • fge
      fge almost 11 years
      Do you have sourceCompatibility set in your build.gradle?
    • Sergii Pechenizkyi
      Sergii Pechenizkyi over 10 years
      Java 7 support was added at build tools 19. Please, check my edited answer.
  • blalasaadri
    blalasaadri almost 11 years
    Here, a similar question with a link on how to overcome the problem: stackoverflow.com/questions/14487682/…
  • Natix
    Natix over 10 years
    Does this work in Android Studio only or is it possible to make this work also in Eclipse with ADT?
  • Sergii Pechenizkyi
    Sergii Pechenizkyi over 10 years
    It is possible with latest ADT for Eclipse. Check full description here: sites.google.com/a/android.com/tools/recent/…
  • coder
    coder about 10 years
    woohoo for string switches!
  • Alex Lockwood
    Alex Lockwood almost 10 years
    Note that try-with-resources can only be used with API 19 or higher.
  • pravin
    pravin over 9 years
    I guess this is correct answer for the latest android-studio. Wish I could make this also a correct answer..
  • Kerem
    Kerem almost 9 years
    This is essentially the same as the accepted answer.
  • user3259330
    user3259330 over 8 years
    It's just a convenience feature, it will insert the previously described "compileOptions" code into the gradle file.
  • Oliver Hausler
    Oliver Hausler over 7 years
    This setting is confusing, because it affects all projects, even though it is named "Project Structure".
  • Neon Warge
    Neon Warge about 7 years
    How can I set this to 8?
  • Srikar Reddy
    Srikar Reddy almost 7 years
    I can't see 1.8 and 1.9? Even though the About -> Help says Android studio is using 1.8 JRE.