34

Happy to know that java introduced Lambda expressions in java version 8.

I was working in android studio. And I noticed that DialogInterface.OnClickListener() was showing underline and became grey colored as below:

enter image description here

So, I did alter + enter on that line and found option to replace the code with Lambda Expressions, as below:

enter image description here

Which resulted in something below:

enter image description here

After running the code it showed me this error:

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

But I am already using java jdk1.8.0_31. Is there anything else, I need to install ?

Can anyone help me out please..

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • 2
    Have you read: http://stackoverflow.com/questions/23318109/is-it-possible-to-use-java-8-for-android-development – Morrison Chang May 18 '15 at 03:11
  • Java 8 is not supported but you can use retrolanbda for the same effect https://github.com/evant/gradle-retrolambda – Eoin May 18 '15 at 05:37

2 Answers2

11

You cannot compile Java 8 sources for Android, because Android SDK does not support Java 8 yet.

But there is one way to use Lambdas in your code - RetroLambda. This is a little hack that allows you to use some of the features of Java 8 in your project.

Stepango
  • 4,721
  • 3
  • 31
  • 44
  • Have u used it..did you got any side-effect about this lib ! – CoDe Sep 03 '15 at 06:00
  • @Shubh i tried to use it in production few months ago, but unfortunately i faced a lot of problems with it. Bamboo CI did not supported Java 8 means you cannot compile your project there. Also for some versions of gradle plugin project did not compiles, and espresso tests did not worked with retrolambdas. Probably this problems already solved but i did not checked – Stepango Sep 03 '15 at 06:16
  • So this is not recommended to use it for now at least, Right! – CoDe Sep 03 '15 at 06:35
6

I don't think Android development is ready yet for Java 8. Android Studio supports the compile level Java 7 whether you installed Java 7 or Java 8. So, if you want to use new features of Java 8, I think you have to wait Android Studio to support compile level Java 8.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • yes might be...you are right here!. – CoDe Sep 03 '15 at 05:58
  • Instant run not being supported is the main drawback and what caused me to revert back to Java 7 honestly, PLUS all the really cool language features in Java 8 (Streams for example) are only supported with API 24 and above..which is insane – Odaym Nov 15 '16 at 10:19