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

I got "anonymous new runnable () can be replaced with lambda" using 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 rewriting 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 start the application, Android Studio stops with an 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 cannot understand why Android Studio allows me to use a lambda expression, even if it cannot compile. This is mistake?

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

Since I cannot compile my code, I am not sure if the above lambda expresssion is correct or not.

In my opinion, the IDE should not complain that the code could not be compiled. Therefore, I think the warning to use the lambda expression should be suppressed. But I do not know how this can be ...

Any help is appreciated.

+6
source share
3 answers

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

Although we can encode JDK8 using Retrolambda , (for Android Studio this is gradle-retrolambda ), or RxJava , they are just FLAVOR ...

My problem was caused by installing JDK8, not installing JDK7.
I thought it was better to install JDK8, because oracle officially supports JDK8 now and stopped updating JDK7, but this is a wrong idea.

After I uninstall JDK8 and install JDK7, the IDE does not warn you about using a lambda expression or does not occur. lambda expressions are not supported in the -source 1.7 error when compiling.

+1
source

First of all, “anonymous new runnable () can be replaced with lambda” is a warning , as you stated. Although such warnings are not as serious as compiler errors, you should still understand the reasons for the warning in order to make an informed decision on how to deal with this. In this case, the warning comes from the IDE, not the compiler, and can be safely ignored. Android Studio should have a setting in which you can turn off this warning, but I could not determine exactly how to do this. I would start by clicking on the text new Runnable() in the source code and pressing Alt-Enter to see quick fix options.

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

 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 closing compileOptions . The rest is to provide a context where this refers to the file.

Please note that I have not compiled or tested this. Also, I'm not sure if you need both sourceCompatibility and targetCompatibility . I suggest you do some research and experiment to determine if both are needed to compile and run your application on the devices you want to target.

Sources:

How to install -source 1.7 in Android Studio and Gradle

Java 8 features in Android development

+6
source

First, you need to know the difference between compile-time errors and runtime errors .

Further reading at runtime during compile-time errors:
Runtime and compilation time
Warnings and Errors

The reason you get the error is that it is not supported in Java <Version 1.8.

The solution to this problem is that you should change the Java version of your project to 1.8

Here is a procedure:
Using JDK 7 or higher with Android Studio and Eclipse on Mac OSX

Hope this helps.

+1
source

Source: https://habr.com/ru/post/988966/


All Articles