|Android Studio Moves Toward Java 8|
|Written by Mike James|
|Tuesday, 18 April 2017|
Android Studio is making progress to supporting Java 8 after the ambitious, perhaps too ambitious Jack & Jill project was shelved. What does all this mean?
Version 2.4 of Android Studio is heading towards a release candidate. at which point we will all get the opportunity to try out some Java 8 features - one in particular that new Android programmers have been asking about for some time, lambdas.
The latest version, preview 6, supports a surprising number of Java 8 features considering the short time that has elapsed since the Jack & Jill compiler project was deprecated. The problem is that while Android uses supposedly standard Java it compiles it to a different byte code. The standard Java compiler is used to convert to standard Java byte code and then dx is used to convert this to Dalvik byte code or Dex.
The problem is that dx only translates a subset of the byte code and specifically not enough to implement Java 8 features. The Jack & Jill compiler project was an attempt to create an entirely new compiler that went directly from Java to Dalvik byte code. This would have been an even bigger split from Java and this is ostensibly the reason the project has been shelved.
Now the plan is to extend the dx compiler to one called desugar to compile the extra features in the byte code needed to implement Java 8.
The latest Android Studio preview includes a good range of Java 8 features and most important of all you will almost certainly be able to use lambdas in the next stable release.
What does this mean to the average Android programmer?
The biggest impact, as mentioned, is likely to be lambdas. The reason is that Java has long had a problem with its lack of first class functions. If you want to pass a function in Java you have to pass an object that has the function as a method. This makes event handling in Java very wordy and almost incomprehensible to a programmer coming from almost any other langauge. Adding lambdas makes event handling much easier.
For example, suppose you have a Button with the id button, then to attach a simple click event handler you need to use:
This uses an anonymous class to create an instance of OnClickListener with an overridden onClick method.
Notice that's a class and instance of a class and a method just to pass a function to setOnClickListener.
It isn't as bad as it sounds because Android Studio will do a lot of the work for you by generating the boilerplate code. There is an easier way to create an event handler for the click event, but this only works for the click event.
Using lambdas is much more direct:
This just passes a function to the setOnClickListener. Of course, the compiler wraps the function in an instance of a class. but this is all behind the scenes and the programmer can ignore it. Before you think that there is an error in that b is used and not v in the lambda, it is worth also pointing out that this is a closure and the lambda has access to the variables that were in scope when it was created. However, it isn't good to cache references to UI objects so a better version is:
It will be interesting to find out if any Android programmers in the future know what an anonymous class is.
You can also use method references which allow you to reference an existing method as the body of a lambda thus allowing you to pass an existing method without passing the entire class.
You can also use type annotations but only at compile time not run time and if you are prepared to give up instant run you can use default and static interface methods. Shame because default methods are very useful.
Also if you are prepared to use API 24 or higher you can use:
To make use of these features you have to disable Jack and any other library you are using to make up for a lack of Java 8 support. You also have to add:
to the module's build.gradle file. After this you can use Java 8 in a new project in older projects there may be more to do.
Let's hope that in the final version we just have a dropdown for the language level.
Android Studio is still changing too fast in some respects and not fast enough in others. Why for example am I still having to use findById and not data binding as default?
And why is it all still so slow?
or email your comment to: firstname.lastname@example.org
|Last Updated ( Tuesday, 18 April 2017 )|