Android N Java8 java.time

Viewed 10187

I updated to the latest Android N sdk. The only thing I don't understand is why I cannot import java.time into my code? I thought Java8 is available through Android N. Then why didn't Google add java.time package?

4 Answers

Starting from Android Gradle Plugin 4.0.0, we can finally use proper java.time package classes without worries (almost): https://developer.android.com/studio/write/java8-support

Optional, java.time, streams, and more are desugared into Java 7 by the Android Gradle Plugin.

To add those classes support, you just need to add a few lines to your build file:

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

Here is the full list: https://developer.android.com/studio/write/java8-support-table

Related