Home » Android » java – How can I suppress anonymous new runnable() can be replaced with lambda

java – How can I suppress anonymous new runnable() can be replaced with lambda

Posted by: admin June 15, 2020 Leave a comment


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() {

    public void run() {

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);

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.

How to&Answers:

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.


How to set -source 1.7 in Android Studio and Gradle

Java 8 features into Android Development


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.


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.