27

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?

Volo
  • 28,673
  • 12
  • 97
  • 125
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • just waiting is an option: jack is now deprecated and java8 API will soon be supported directly: https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html – ccpizza Jun 10 '17 at 06:57
  • @ccpizza They do not support all features of Java 8 for Android. – IgorGanapolsky Jun 12 '17 at 13:01
  • 1
    Much of the java.time functionality is back-ported to Java 6 & 7 in the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [*ThreeTenABP*](https://stackoverflow.com/q/36000997/642706) project; see [*How to use…*](https://stackoverflow.com/q/38922754/642706). – Basil Bourque Sep 07 '17 at 22:01

4 Answers4

29

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'
}
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Volo
  • 28,673
  • 12
  • 97
  • 125
21

Android N is not supporting all the features of Java 8. Following features are only supported:

  • Default and static interface methods
  • Lambda expressions
  • Repeatable annotations

Reflection and language-related APIs:

  • java.lang.FunctionalInterface
  • java.lang.annotation.Repeatable
  • java.lang.reflect.Method.isDefault()

and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class)

Utility APIs:

  • java.util.function

For more info check the following link: http://developer.android.com/preview/j8-jack.html

Volo
  • 28,673
  • 12
  • 97
  • 125
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • 6
    Thanks for the outline. I guess our only option for Java 8 Time api is https://github.com/JakeWharton/ThreeTenABP – IgorGanapolsky Mar 16 '16 at 12:38
  • 1
    @Igor right untill stable version of android-n will release. hope in stable version they will support – Maheshwar Ligade Mar 16 '16 at 12:42
  • Well, if they didn't add the Time api yet, then they probably won't in Android N. Otherwise, I have no idea how Google makes decisions to include or omit certain apis... – IgorGanapolsky Mar 16 '16 at 12:59
  • Much of the java.time functionality is back-ported to Java 6 & 7 in the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [*ThreeTenABP*](https://stackoverflow.com/q/36000997/642706) project; see [*How to use…*](https://stackoverflow.com/q/38922754/642706). – Basil Bourque Sep 07 '17 at 22:03
19

There is backport library of java.time APIs for Android that can be used

https://github.com/JakeWharton/ThreeTenABP

kotucz
  • 1,190
  • 13
  • 13
  • 2
    This library is no longer maintained, deprecated, and the repo is now archived. You should now [use Gradle 4.00 + desugaring](https://developer.android.com/studio/preview/features#j8-desugar) – Martin Marconcini Aug 07 '20 at 14:22
6

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

Gaket
  • 6,533
  • 2
  • 37
  • 67
  • Why do we need desugaring if we're using Kotlin? – IgorGanapolsky Aug 08 '20 at 15:12
  • 1
    Kotlin still uses many Java classes underneath and, I guess, they couldn't just reimplement the java.time package in order to sustain the backwards compatibility? – Gaket Aug 08 '20 at 18:47