Diamond type are not supported at this language level

你离开我真会死。 提交于 2019-12-05 20:54:08

问题


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 ?


回答1:


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

Source Compatibility == 1.7
Target Compatibility == 1.7

After this your build.gradle will have these entries:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}




回答2:


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




回答3:


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.




回答4:


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
    }



回答5:


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.

Hope this saves someone some time.




回答6:


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



来源:https://stackoverflow.com/questions/29205072/diamond-type-are-not-supported-at-this-language-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!