Amir
Amir

Reputation: 16587

Diamond type are not supported at this language level

After importing a project into Android studio, if I want to compile or run the project it throws an error:

Error:(61, 65) java: diamond operator is not supported in -source 1.6
(use -source 7 or higher to enable diamond operator)

Does anyone know what it is and how to solve it ?

Upvotes: 8

Views: 13673

Answers (6)

Dincio
Dincio

Reputation: 1072

In Intellij, at leat for me, the problem was that the target version for each module, specified under "Settings->Build, Execution, Deployment->Java Compiler", was wrong. enter image description here

Hope this saves someone some time.

Upvotes: 2

erajuan
erajuan

Reputation: 2294

In Android Studio (File -> Project Structure..., Properties tab), set the following values:

Source Compatibility == 1.7
Target Compatibility == 1.7

enter image description here

After this your build.gradle will have these entries:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

enter image description here

Upvotes: 13

Ranga Reddy
Ranga Reddy

Reputation: 3066

In Intellij Idea, you need to set the project language level (default for all modules) and the module(s) language level.

File --> Project Structure --> Under Project Settings --> Select Project --> Project Language Level --> Select 7 - Diamons, ARM, multi-catch etc. or 8 - Lambdas,type annoationsetc. option and Click on Apply

Click here to see the pic

Upvotes: 7

Fahim
Fahim

Reputation: 12358

With Android KitKat (buildToolsVersion 19) you can use the diamond operator, multi-catch, strings in switches, try with resources, etc. To do this, add the following to your build file:

android {
        compileSdkVersion 19
        buildToolsVersion "19.0.0"

        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 19
        }

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

Note that you can use minSdkVersion with a value earlier than 19, for all language features except try with resources. If you want to use try with resources, you will need to also use a minSdkVersion of 19.

You also need to make sure that Gradle is using version 1.7 or later of the JDK. (And version 0.6.1 or later of the Android Gradle plugin.)

http://tools.android.com/tech-docs/new-build-system/user-guide

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

Few days ago , I suffered from this . Just update your buildToolsVersion Like below. And Upgrade Your SDK.

    android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

Upvotes: 2

Arkar Aung
Arkar Aung

Reputation: 3584

Diamond operator is one of the new feature of Jdk 7. Please make sure you jdk version is 7 or not. Here is an example of diamond operator.

Here is an assignment statement :

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

With diamond operator :

Map<String, List<String>> anagrams = new HashMap<>();

Edit

Add that to your build.gradle ..

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
    }

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

Hope it will be useful for you.

Upvotes: 2

Related Questions