Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expressions not supported -source 1.7 (Android Studio) [duplicate]

Tags:

java

android

I was trying to run my android project in Android Studio, but I can't do so.

I am getting this error:

Error:(23, 47) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

I am using JDK 1.8.

Any idea why is this happening? Any resoultion.

PS: There are couple of similar questions in Stack but none resolved this problem. Please understand the problem before you tag Duplicate.

like image 359
kishoredbn Avatar asked Sep 29 '22 15:09

kishoredbn


1 Answers

Here is the solution:

Android cannot be build on JDK 1.8; And Lambda expression cannot be used in JDK below 1.8.

The solution is to go back to JDK 1.7 and avoid using Lambda sign. In place of using:

button.setOnClickListener((v) -> {


                    Intent newIntent = new Intent(MainActivity.this, NextActivity.class);
                    MainActivity.this.startActivity(newIntent);
                }
            });

We have to use this:

       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent newIntent = new Intent(MainActivity.this, NextActivity.class);
                MainActivity.this.startActivity(newIntent);
            }
        });
like image 55
kishoredbn Avatar answered Oct 03 '22 06:10

kishoredbn