Quick Overview on Java 8 Features

android_development

After all we have some features of java 8 on android :D, lets look at them.

Default Methods

Default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface (For more information please check this link).

Important Note: This feature just work on API level 24.

Static Methods

A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

Important Note: This feature just work on API level 24

Repeating Annotations

There are some situations where you want to apply the same annotation to a declaration or type use. Repeating annotations enable you to do this.

Important Note: This feature just work on API level 24

Lambda Expressions

Lambda expression facilitates functional programming, and simplifies the development a lot.
Important characteristics of a lambda expression:

  • Optional type declaration
  • Optional parenthesis around parameter
  • Optional curly braces
  • Optional return keyword

This feature is available on API level 24 and lower.

Method References

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name.

This feature is available on API level 24 and lower.

 

lambda

Migrating to Jack compiler

jackjill

What is Jack?
Google says:

Jack is a new Android toolchain that compiles Java source into Android dex bytecode. It replaces the previous Android toolchain, which consists of multiple tools, such as javac, ProGuard, jarjar, and dx.

What about existing .jar libraries?
Jill tool translates the existing jar libraries into new library format 😀

Why we need migrating to Jack compiler?

  • Speeds compilation time
    Jack compiler can compile java code to .dex in one step

    • Old toolchain: javac (java -> .class) -> dx (.class -> .dex)
    • Jack toolchain: Jack (java -> .jack -> .dex)
  • Using Java8 features
  • Shrinking, obfuscation, repacking without using different tools

 

Note: Make sure your Android Studio is updated to the last available version.

To enabling Java 8 and Jack for your project your build.gradle file should be like this:

android {
   ...
   defaultConfig {
      ...
      targetSdkVersion 24
      jackOptions{
         enabled true
      }
   }
   compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
   }
}

Rebuild your project and it’s should work without any error, But if you see below error:

Error:Could not get unknown property ‘classpath’ for task ‘:app:transformJackWithJackForDebug’ of type com.android.build.gradle.internal.pipeline.TransformTask.

It’s means you should use annotationProcessor dependency scope instead of apt.

 

Important Note: Instant Run doesn’t work with Jack.