10

I got "anonymous new runnable() can be replaced with lambda" warning with the following code.

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.post(new Runnable() {

    @Override
    public void run() {
        sv.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

I searched on Google very hard and seems to be re-write using lambda expression...

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
Runnable test = () -> sv.fullScroll(ScrollView.FOCUS_DOWN);
test.run();

But when I try to run application, Android Studio stops with error as follows:

Error:(78, 40) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)

I can't understand why Android Studio let me use lambda expression even though it can't compile. Is it a bug?

Also, I tried to use gradle-retrolambda, but it is hard to use for biginner.

As I can't compile my code, I'm not sure above lambda expresssion is correct or not.

In my opinion, IDE should not complain about the code can't be compiled. So I think the warning to use lambda expression should be suppressed. But I don't know how can it be...

Any help is appreciated.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
xanadu6291
  • 931
  • 10
  • 20

4 Answers4

8

First of all, "anonymous new runnable() can be replaced with lambda" is a warning as you stated. While warnings like this are not as serious as compiler errors, you should still understand the reasons for the warning in order to make an informed decision of how to deal with it. In this case, the warning comes from the IDE, not the compiler and can be safely ignored. Android Studio should have a setting where you can disable this warning, but I have been unable to find exactly how to do so. I would start by clicking on the new Runnable() text in your source code and pressing Alt-Enter to see the quick fix options.

Alternatively if you want to use lambda functions in your code, you need to enable support for Java 8, as the error message that you get states. Note that some Java 8 features are only available if your app targets Kit Kat or later. Lambda functions are supported on earlier versions of Android, so you don't have to worry in this case. To enable Java 8 for your project, modify your build.gradle file to look similar to the following:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

The important part is the compileOptions closure. The rest is to give context where this belongs within the file.

Note that I have not compiled and tested this. Also, I am unsure if you need both sourceCompatibility and targetCompatibility. I suggest you do some research and experimentation to determine if both are needed in order to compile and run your app on the devices that you wish to target.

Sources:

How to set -source 1.7 in Android Studio and Gradle

Java 8 features into Android Development

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • This is not a lint warning, more likely an IDE hint. – assylias Jun 12 '15 at 05:53
  • 3
    @assylias Thanks for the clarification. I wish that AS were more clear about the level of severity of any messages it gives...or maybe I haven't encountered issues like these often enough to know how AS signifies the difference from an actual compiler error. Of course, my main point stands. The message is not a serious issue and can be safely ignored. – Code-Apprentice Jun 12 '15 at 05:56
  • Thanks for your explanation. I know that I can ignore warning, but I'd like do not get waring in my hope. With your suggestion, I can find out the way to replace lambda expression. `sv.post(() -> sv.fullScroll(ScrollView.FOCUS_DOWN));` It seems that both sourceCompatibility and targetCompatibility are required. But when I press run button in Android Studio, I get _com.sun.tools.javac.code.Symbol$CompletionFailure: class file for java.lang.invoke.MethodType not found_ error. Should I quit to use lambda? – xanadu6291 Jun 12 '15 at 07:14
  • 2
    @xanadu62 I suggest that you Google that error message to figure out how to solve it. – Code-Apprentice Jun 12 '15 at 12:30
  • Googling it is a better option. Other than that, if the problem specified in your question is solved, then you should accept an answer :) Happy coding. – Salman Khakwani Jun 12 '15 at 13:46
  • @SalmanKhakwani Thanks for your comment. Well I was planed to accept the answer when I solved _java.lang.invoke.MethodType not found_ error. Fortunately, I can figure out the solution. I'm now happy!! – xanadu6291 Jun 12 '15 at 14:31
  • @Code-Apprentice Thanks for comment. The error I described above is may be my fault. I don't prepared retrolambda, and got that error. When I copied [Gradle Retrolambda Plugin](https://github.com/evant/gradle-retrolambda) 's Usage section #2 to top of build.gradle(Module: app), the error have gone. FYI: [This is Screenshot of my build.gradle(Module: app)](https://goo.gl/photos/GUC3vsFdCNpqNVNw9) As you see, `targetCompatibility` is not needed. – xanadu6291 Jun 12 '15 at 14:57
  • @xanadu62 Note that Retrolambda is a library which allows us to use lambdas in Java 7 code. `targetCompatibility` will allow you to use Java 8 features directly. – Code-Apprentice Jun 12 '15 at 22:36
  • @xanadu62 After you have done some research regarding the MethodType error, feel free to post a new question, if you still have been unable to solve it. – Code-Apprentice Jun 12 '15 at 22:37
  • @Code-Apprentice Thanks for your kind comment. But I can't find out the way to compile by adding `compileOptions` closure only. Now I realized that Android does not support Java8 yet. Yes I know that we can code _like Java8_ using plug-in like Retrolambda. But it is just a _FLAVOR_. So I posted my own answer and marked as accepted answer. – xanadu6291 Jun 14 '15 at 09:16
1

Firstly, one must know the difference between compile time errors and run-time errors.

Further reading on Run-time vs Compile-time Errors:
Runtime vs Compile time
Warnings and Errors

The reason behind the error you are getting is that it is not Supported in Java < 1.8 versions.

The Solution of this problem is that you should change the Java version of your Project to 1.8

Here is procedure of doing that:
Using JDK 7 Or Higher With Android Studio And Eclipse On Mac OSX

I hope this helps.

Community
  • 1
  • 1
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
1

After searching with Google, I have realized that Android does not support JDK8 yet officially. See this link

Though we can JDK8 flavored coding using Retrolambda, (For Android Studio, it is gradle-retrolambda), or RxJava, they are just a FLAVOR ...

My problem was caused by installing JDK8, instead of should be install JDK7.
I thought that it is preferred to install JDK8 because oracle officially supports JDK8 now and have terminated to update JDK7, but it is wrong thought.

After I uninstalled JDK8 and installed JDK7, IDE does not warn to use lambda expression, or does not occur lambda expressions are not supported in -source 1.7 error on compilation.

Community
  • 1
  • 1
xanadu6291
  • 931
  • 10
  • 20
-1

There seems to be a new problem here in that Android Studio Arctic Fox 2020.3.1 Patch 3 (the current version) doesn't provide a way to disable the Inspection which causes the warning "Anonymous [class] can be replaced with lambda".

So if I want to compile without warnings I have to replace all my anonymous classes (I have a lot of them) with lambdas and then I can't run my code with JDK 7.

There is also an Inspection which causes the warning "Lambda can be replaced with anonymous class", and this one can be disabled.

???!!!

Richard Parkins
  • 347
  • 2
  • 13