Android Studio 3.0 (by 25th July 2017) only has a preview (canary) version available. Like the previous versions (2.3.x) it supports all Java 7 features and some of the Java 8 features. These subset of Java 8 features supported, vary by platform version. During your Android application development, it is not mandatory that you use Java 8 features, but compiling using Java 8 is mandatory. And in the ongoing tutorial, we shall learn how to use Java 8 features in Android Studio that are builtin.

The Jack tool chain that has been introduced in Android Studio 2.1 and higher is depreciated. Now Android Studio 3.0 has a built in support for a subset of Java 8 features. So, if you have been using Jack toolchain, it is advised to disable Jack toolchain and use the default toolchain provided by Android Studio 3.0 + for an improved Java 8 builtin support.

Use Java 8 features in Android Studio - www.tutorialkart.com
Use default Java 8 features in Android Studio

To disable Jack toolchain from your Android Application, remove the jackOptions snippet from defaultConfig block in build.gradle file.

To start using Java 8 features in your Android Application, update Android plugin to 3.0.0-alpha1 or higher and add the following compileOptions to build.gradle file in your app folder.

</>
Copy
android {
    . . .
    defaultConfig {
        . . . 
    }
    buildTypes {
        . . .
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Following are the Java 8 features supported by all sdk versions (you may keep any minSdkVersion) :

  • Lambda expressions (Exception : All values captured by the lambda should be serializable.)
  • Method References
  • Type Annotations (Exception : ElementType.TYPE_USE and ElementType.TYPE_PARAMETER are supported for sdk version 24+)
  • Default and Static interface methods
  • Repeating annotations

And following are the Java 8 APIs that are supported with a minSdkVersion of 24 (or higher) :

  • java.lang.annotation.Repeatable
  • AnnotatedElement.getAnnotationsByType(Class)
  • java.util.stream
  • java.lang.FunctionalInterface
  • java.lang.reflect.Method.isDefault()
  • java.util.function

If you experience any issues with the latest Java 8 features, you may disable it by disabling Desugar in your gradle.properties file as shown in the following.

android.enableDesugar=false

Conclusion

In this Android Tutorial, we have learnt a way for how to use Java 8 features in Android Application development.