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.

2
  • Which version of Android Studio do you use? Jun 12, 2015 at 5:17
  • I'm using Ver.1.2.2 The latest version.
    – xanadu6291
    Jun 12, 2015 at 6:48

4 Answers 4

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

10
  • This is not a lint warning, more likely an IDE hint.
    – assylias
    Jun 12, 2015 at 5: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. Jun 12, 2015 at 5: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, 2015 at 7:14
  • 2
    @xanadu62 I suggest that you Google that error message to figure out how to solve it. Jun 12, 2015 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. Jun 12, 2015 at 13:46
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.

4
  • 2
    More importantly, the OP needs to learn about the difference between warnings and compiler errors. Jun 12, 2015 at 5:15
  • Thanks for your suggestion, i have updated my answer. Jun 12, 2015 at 5:37
  • Also, the link you provide seems to give instructions for setting the JDK installation which is used by AS. It doesn't address setting the options which control which Java version and it's accompanying features are supported in the source code. Jun 12, 2015 at 6:03
  • OK. Thanks for telling me about errors and waring, Runtime and compile-time.
    – xanadu6291
    Jun 12, 2015 at 6:51
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.

-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.

???!!!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.