[Tutorial] Accessing to the Java 8 language features with Android N

Search This thread

sylsau

Recognized Developer
Nov 8, 2011
976
672
Hello,

I create that thread to share with you a tutorial showing you how to access the Java 8 language features with Android N. You can find the tutorial here on my blog : http://www.ssaurel.com/blog/accessing-to-the-java-8-language-features-with-android-n/ . There are also others great tutorials for Android development.

If you prefer to read the tutorial here, this is the complete content :

Accessing to the Java 8 language features with Android N

Google has unveiled developer preview of Android N last month with a new exciting developer program. Official release is planned for Q3 2016. Amongst the new features of Android N, the support of several Java 8 language features is a great addition for developers.

Supported features include :
  • Lambda expressions
  • Default and static interface methods
  • Repeatable annotations
  • Method References

Additionally, some new APIs introduced with Java 8 are available like reflection and language-related APIs and Utility APIs (java.util.function and java.util.stream).
Set up your development environment

To support Android N new features, you need to set up correctly your development environment. You need to get last version of Android N developer preview and to download Android Studio 2.1. You can make these installations via the Android SDK Manager on your computer. To get more details about the installation process, don’t hesitate to read the official documentation from Android Team : http://developer.android.com/preview/setup-sdk.html

Enable support for Java 8

In order to use the new Java 8 language features, you need to use the new Jack toolchain. Jack, for Java Android Compiler Kit, replaces javac compiler. It compiles Java language source code into Android-readable dex bytecode with its own .jack library format. Furthermore, it provides other great toolchain features inside a single tool like repackaging, shrinking, obfuscation and multidex. Before Jack, you needed to use ProGuard to achieve these tasks for example.

To enable support for Java 8 in your Android project, you need to configure your build.gradle file like that :

Code:
android {
  ...
  defaultConfig {
  ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Enjoy your first Lambda Expression on Android

Now, you can enjoy your first Lambda expression on Android. Basically, a Lambda expression is a block of code that can be passed around to be executed later. In fact, it is very similar to anonymous classes. However, Lambda expressions are succinct with less verbose code. Let’s use a Lambda to set a click listener on a Button :

Code:
button.setOnClickListener(view -> Log.d(TAG, "Button Clicked."));

You can make the same with an item click listener on a ListView :

Code:
listView.setOnItemClickListener((parent, view, position, id) -> {
  // …
}

It works great with a Runnable interface too :

Code:
Runnable runnable = () -> Log.d(TAG, "Log from a Runnable with a Lambda");
newThread(runnable).start();

Clearly, you can see the code is shorter and more readable with Lambdas expressions. It will be great for the productivity of Android developers.

Create your first Functional Interface on Android

To create our first Functional on Android, let’s take classic example of a Calculator with an addition feature :

Code:
@FunctionalInterface
public interface Calculator {
  int calculate(int a, int b);
}

Now, you can add a default method to the Calculator interface that will be used to display a result for example :

Code:
@FunctionalInterface
public interface Calculator {
  int calculate(int a, int b);
  default void print(String result){
    // … print result …
  }
}

Finally, we can create a Lambda expression with that interface :

Code:
Calculator c = (a, b) -> a + b;

Like you can see with those simple examples, Java 8 language features will offer a new way to Android developers to improve their productivity. Thanks to the developer preview of Android N, you can try these features right now.

Don't hesitate to share your feedbacks with me.

Thanks.

Sylvain
 

AndroidDeveloperLB

Senior Member
Mar 30, 2014
2,020
472
Is it worth going to Jack right now, though?
I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.

Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.
 
  • Like
Reactions: agriculture

sylsau

Recognized Developer
Nov 8, 2011
976
672
Is it worth going to Jack right now, though?
I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.

Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.

For the moment, I just tried on small projects. So, it was not so slow during build time. May be on big projects ?

These new features are not a revolution for sure, but it's interesting to have the choice to use it in the future when you develop Android apps. Be able to have some functional programming features is a good thing.

For Lambdas, it's not really the same thing on the bytecode. On Java 8 desktop apps, some benchmarks showed that using Lambdas is very often faster.
 
  • Like
Reactions: agriculture

AndroidDeveloperLB

Senior Member
Mar 30, 2014
2,020
472
For the moment, I just tried on small projects. So, it was not so slow during build time. May be on big projects ?

These new features are not a revolution for sure, but it's interesting to have the choice to use it in the future when you develop Android apps. Be able to have some functional programming features is a good thing.

For Lambdas, it's not really the same thing on the bytecode. On Java 8 desktop apps, some benchmarks showed that using Lambdas is very often faster.

I see.

For Lambdas, I don't know. It's just my guess that whatever you write is converted to normal code that you probably should have written without lambdas.
Perhaps the JDK has some optimizations for it.

EDIT: it seems there is a difference:
http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf
http://blog.takipi.com/benchmark-how-java-8-lambdas-and-streams-can-make-your-code-5-times-slower/
Nice. Wonder if it's this way on Android too.
 
Last edited:

sylsau

Recognized Developer
Nov 8, 2011
976
672
I see.

For Lambdas, I don't know. It's just my guess that whatever you write is converted to normal code that you probably should have written without lambdas.
Perhaps the JDK has some optimizations for it.

EDIT: it seems there is a difference:
http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf
http://blog.takipi.com/benchmark-how-java-8-lambdas-and-streams-can-make-your-code-5-times-slower/
Nice. Wonder if it's this way on Android too.

Thanks for the links. Really interesting.

It seems you were right. Lambdas and Stream can make execution slower.
 

AndroidDeveloperLB

Senior Member
Mar 30, 2014
2,020
472
It depends the way you use it like said on takipi blog. If they are well used, it can make your code run faster however. It's always the same thing, it depends from your implementation and the way you use it.

So there is no definite conclusion? So my guess is that for Android it's too early to see difference between normal code and Lambda.
 
May 28, 2016
14
3
Pretty Information

such a nice post, i am studying the java but it is totally different why can you please explain what the java in book is different the java in applications?? :confused:
Regards Junaid Shahid

---------- Post added at 02:47 PM ---------- Previous post was at 02:42 PM ----------

So there is no definite conclusion? So my guess is that for Android it's too early to see difference between normal code and Lambda.
:good:
 

AndroidDeveloperLB

Senior Member
Mar 30, 2014
2,020
472
such a nice post, i am studying the java but it is totally different why can you please explain what the java in book is different the java in applications?? :confused:
Regards Junaid Shahid

---------- Post added at 02:47 PM ---------- Previous post was at 02:42 PM ----------


:good:

They are almost the same. The pure Java is still there, but some classes do not exist or have less functions, because Android is a mobile OS.
There is just another layer of the framework itself, that you need to learn, when developing for Android.
 

desalesouche

Senior Member
Feb 6, 2012
3,651
4,297

Top Liked Posts

  • There are no posts matching your filters.
  • 12
    Hello,

    I create that thread to share with you a tutorial showing you how to access the Java 8 language features with Android N. You can find the tutorial here on my blog : http://www.ssaurel.com/blog/accessing-to-the-java-8-language-features-with-android-n/ . There are also others great tutorials for Android development.

    If you prefer to read the tutorial here, this is the complete content :

    Accessing to the Java 8 language features with Android N

    Google has unveiled developer preview of Android N last month with a new exciting developer program. Official release is planned for Q3 2016. Amongst the new features of Android N, the support of several Java 8 language features is a great addition for developers.

    Supported features include :
    • Lambda expressions
    • Default and static interface methods
    • Repeatable annotations
    • Method References

    Additionally, some new APIs introduced with Java 8 are available like reflection and language-related APIs and Utility APIs (java.util.function and java.util.stream).
    Set up your development environment

    To support Android N new features, you need to set up correctly your development environment. You need to get last version of Android N developer preview and to download Android Studio 2.1. You can make these installations via the Android SDK Manager on your computer. To get more details about the installation process, don’t hesitate to read the official documentation from Android Team : http://developer.android.com/preview/setup-sdk.html

    Enable support for Java 8

    In order to use the new Java 8 language features, you need to use the new Jack toolchain. Jack, for Java Android Compiler Kit, replaces javac compiler. It compiles Java language source code into Android-readable dex bytecode with its own .jack library format. Furthermore, it provides other great toolchain features inside a single tool like repackaging, shrinking, obfuscation and multidex. Before Jack, you needed to use ProGuard to achieve these tasks for example.

    To enable support for Java 8 in your Android project, you need to configure your build.gradle file like that :

    Code:
    android {
      ...
      defaultConfig {
      ...
        jackOptions {
          enabled true
        }
      }
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    }

    Enjoy your first Lambda Expression on Android

    Now, you can enjoy your first Lambda expression on Android. Basically, a Lambda expression is a block of code that can be passed around to be executed later. In fact, it is very similar to anonymous classes. However, Lambda expressions are succinct with less verbose code. Let’s use a Lambda to set a click listener on a Button :

    Code:
    button.setOnClickListener(view -> Log.d(TAG, "Button Clicked."));

    You can make the same with an item click listener on a ListView :

    Code:
    listView.setOnItemClickListener((parent, view, position, id) -> {
      // …
    }

    It works great with a Runnable interface too :

    Code:
    Runnable runnable = () -> Log.d(TAG, "Log from a Runnable with a Lambda");
    newThread(runnable).start();

    Clearly, you can see the code is shorter and more readable with Lambdas expressions. It will be great for the productivity of Android developers.

    Create your first Functional Interface on Android

    To create our first Functional on Android, let’s take classic example of a Calculator with an addition feature :

    Code:
    @FunctionalInterface
    public interface Calculator {
      int calculate(int a, int b);
    }

    Now, you can add a default method to the Calculator interface that will be used to display a result for example :

    Code:
    @FunctionalInterface
    public interface Calculator {
      int calculate(int a, int b);
      default void print(String result){
        // … print result …
      }
    }

    Finally, we can create a Lambda expression with that interface :

    Code:
    Calculator c = (a, b) -> a + b;

    Like you can see with those simple examples, Java 8 language features will offer a new way to Android developers to improve their productivity. Thanks to the developer preview of Android N, you can try these features right now.

    Don't hesitate to share your feedbacks with me.

    Thanks.

    Sylvain
    1
    Is it worth going to Jack right now, though?
    I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.

    Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.
    1
    Is it worth going to Jack right now, though?
    I've read that it's slower in build time than the normal configuration, and as some apps already take a huge amount of time to compile, I'm not sure if it's worth it.

    Also, lambdas are only syntactic sugar, right? Behind the scenes, everything is still the same.

    For the moment, I just tried on small projects. So, it was not so slow during build time. May be on big projects ?

    These new features are not a revolution for sure, but it's interesting to have the choice to use it in the future when you develop Android apps. Be able to have some functional programming features is a good thing.

    For Lambdas, it's not really the same thing on the bytecode. On Java 8 desktop apps, some benchmarks showed that using Lambdas is very often faster.