Android Studio 2.3 and later lets you program with certain Java 8 syntax patterns, including lambda expressions and method references. I'll describe how to configure an Android project to use Java 8, and how to convert existing event handler code to use both of these patterns.
- [Narrator] Starting with Android Studio 2.3, you can program with some Java eight style syntax. Including lambda expressions and method references. If you're using Android Studio 2.3, this is enabled by an experimental compiler chain called Jack and Jill. Starting in Android Studio 2.4 though, the Jack compiler is deprecated and now Java eight syntax is supported in the core building process. I'm working in Android Studio 2.3 in this example so I'll show you that version using the Jack compiler, but then I'll also show you what you need to remove if you're using Android Studio 2.4 or later.
In this example, in my on create method, I have references to a couple of button objects that are declared in my layout. I'm using traditional find view by ID code and I'm getting the references here to M Run button and M Clear button. Then, I'm creating implementations of the on click listener interface and then adding the code I want to execute to the on click method. And this all works fine. When I click the buttons in my application, I get the expected behavior.
If I click Run Code, I'm adding information to the console, and if I click the Clear button it clears the screen. If you use Java eight syntax though, you can significantly decrease the amount of boiler plate code that's needed. The first step is to configure your project to use Java eight. I'll go to my application module's gradle file. And then because I'm working in Android Studio 2.3, I need to enable the Jack compiler. This goes inside the default config element.
Type Jack Options and then in a code block add Enabled and then True. Don't add a semi colon, this isn't Java. This is just a configuration declaration. Now, also inside the Android element but outside any of its child elements, add a compile option section. Inside compile options, add source compatibility.
As you type you might get a pop up list, and if so, you should be able to select Source Compatibility from there. And then set that as Java version dot version one eight. Now duplicate that line of code and change source compatibility to target compatibility. So the source compatibility affects the compiler and the target compatibility affects the device on which the application is running. Re-sync your gradle file.
Now if you're working on Android Studio 2.4 or later, do exactly the same thing for the compile options directive, but just don't put in the Jack options directive. You'll still be enabling Java eight. After you sync gradle, go back to your main activity class. And you should now see that the on click listener implementation is disabled. That's a visual indication that you can simplify this code. Place the cursor into the first on click listener call.
And use an intention action. Hold down Alt or Option and press Enter or Return. And choose Replace with Lambda. And now all those lines of code are reduced down to a single statement. This argument V, represents the view object that initiated the click event. The call to run code receives that view object and if I control or command click on this method call, I'll jump to the reference.
Which is designed as an event handler method, receiving a view object as its only argument. I'll do the same thing for the clear button listener. Clicking in, pressing Alt Enter or Option and Return and once again replacing with a lambda. And now I'll run my application on my device. And the code still works exactly like it did before. But with a lot less code in my activity class.
Now notice that these calls, these lambda expressions, can be reduced even further with method references. Click into one of them, and once again use an intention action, and this time replace the lambda with a method reference. The method reference says I'm calling the run code method that's a member of the current object. This, the double colon is the method reference operator. I don't need to be specific about how the view object is being passed in as an argument.
This follows what's known as a functional interface. An interface that has a single method or function. The set on click listener method is expecting a particular interface which only defines one abstract method, and the run code method follows that method's signature. Now I'll do the same thing for the clear button. Converting the lambda to the method reference. And once again I'll run the application.
And once again the code still works exactly like it did before. So that's lambda expressions and method references. Two Java eight syntax features that are now available for Android developers. Again, if you're in Android Studio 2.3, use the Jack compiler. If you're in 2.4 or later, just eliminate the Jack options declaration and you'll be programming with Java eight.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.