Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "lambda expressions not supported at this language level"

People also ask

Can Java 11 use lambda expressions?

From Java 11 var can also be used for lambda parameter types. Here is an example of using the Java var keyword as parameter types in a lambda expression: Function<String, String> toLowerCase = (var input) -> input. toLowerCase();

Which of the following is are not valid lambda expressions?

Based on the syntax rules just shown, which of the following are not valid lambda expressions? Answer: Only 4 and 5 are invalid lambdas.

Can we use lambda expression in Java 7?

From the Retrolambda documentation: Retrolambda lets you run Java 8 code with lambda expressions and method references on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .

Is lambda expressions introduced in Java 8?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.


In IntelliJ IDEA:

In File MenuProject StructureProject, change Project Language Level to 8.0 - Lambdas, type annotations etc.

For Android 3.0+ Go FileProject StructureModuleapp and In Properties Tab set Source Compatibility and Target Compatibility to 1.8 (Java 8)

Screenshot:

enter image description here


You should change source code Language Level also on the Source tab (Modules part).

Change Language Level


This solution works in Android Studio 3.0 or later.

  1. File > Project Structure > Modules > app > Properties tab

enter image description here

Change both of Source Compatibility and Target Compatibility to 1.8

  1. Edit config file

You can also configure it directly in the corresponding build.gradle file

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

For intellij 13, Simply change the Project language level itself to 8.0 with following navigation.

File
|
|
 ---------Project Structure -> Project tab
          |
          |________Project language level

java8

Module lang level

I also had to update Modules lang level when there was no maven plugin for java compiler.

File
|
|
 ---------Project Structure -> Modules tab
          |
          |________ language level

Modules lang level

But this Module lang level would automatically be fixed if there's already a maven plugin for it,

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

After changes everything looks good


In Android studio canary build(3.+), you can do the following:

File -> Project Structure -> Look in Modules Section, there are names of modules of your class. Click on the module in which you want to upgrade java to 1.8 version. -> Change "Source Compatibility" and "Target Compatibility" to 1.8 from 1.7 or lower. Do not change anything else. -> Click Apply

now gradle will re-sync and your error will be removed.