Android N Java8 java.time

java.time package was added only in API 26 (Android O):
https://developer.android.com/reference/java/time/package-summary.html

UPDATE

Starting with version 4.0 of the Android Gradle Plugin, you can use a subset of java.time APIs (along with a number of other Java 8 language APIs) without requiring a minimum API level for your app: https://developer.android.com/studio/write/java8-support#library-desugaring

The following set of APIs are supported when building your app using Android Gradle plugin 4.0.0 or higher:

  • Sequential streams (java.util.stream)
  • A subset of java.time
  • java.util.function
  • Recent additions to java.util.{Map,Collection,Comparator}
  • Optionals (java.util.Optional, java.util.OptionalInt and java.util.OptionalDouble) and some other new classes useful with the
    above APIs
  • Some additions to java.util.concurrent.atomic (new methods on AtomicInteger, AtomicLong and AtomicReference)
  • ConcurrentHashMap (with bug fixes for Android 5.0)

To enable support for these language APIs, one needs to include the following lines build.gradle 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.4'
}

Leave a Comment