Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java 8 in android studio [duplicate]

Im using android studio and I want to use lambda expression that are available only from java 8, and when I try to use it I get:

Lambda expression are not supported at this language level.

So, how can I change that on AndroidStudio?

like image 675
Golan Kiviti Avatar asked Dec 25 '15 16:12

Golan Kiviti


People also ask

Can I use Java 8 in Android Studio?

Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still it doesn't have invokedynamic, only the new syntax sugar. If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba.

How do I find duplicates in Java 8?

Get the stream of elements in which the duplicates are to be found. For each element in the stream, count the frequency of each element, using Collections. frequency() method. Then for each element in the collection list, if the frequency of any element is more than one, then this element is a duplicate element.

How do I remove duplicates from a list in Java 8?

Using Java 8 Stream. You can use the distinct() method from the Stream API. The distinct() method return a new Stream without duplicates elements based on the result returned by equals() method, which can be used for further processing.


2 Answers

So, how can I change that on AndroidStudio?

you can't. Android supports up to java 7. If you want to use AS only for Lambda, you can use gradle-retrolamba or retrolambda, which back-port retrolambda on java 6/7

like image 150
Blackbelt Avatar answered Sep 18 '22 20:09

Blackbelt


Android supports Java 6 up to KitKat (4.4) with some minor syntactic sugar (like diamond operator), and Java 7 from there on (Lollipop and Marshmallow atm). Lambdas and other Java 8 stuff are available only as plugins (e. g. gradle-retrolambda or the original retrolambda). They will probably be included in API at some point in time (like Java 7 and KitKat), but it doesn't seem likely it'll be very soon.

like image 22
Luke Avatar answered Sep 21 '22 20:09

Luke