Android N Java8 java.time
Asked Answered
M

4

28

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?

Magnificat answered 15/3, 2016 at 1:14 Comment(3)
just waiting is an option: jack is now deprecated and java8 API will soon be supported directly: android-developers.googleblog.com/2017/03/…Megaron
@Megaron They do not support all features of Java 8 for Android.Magnificat
Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted for Android in the ThreeTenABP project; see How to use….Luke
A
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

Attainture answered 16/3, 2016 at 6:44 Comment(4)
Thanks for the outline. I guess our only option for Java 8 Time api is github.com/JakeWharton/ThreeTenABPMagnificat
@Igor right untill stable version of android-n will release. hope in stable version they will supportAttainture
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...Magnificat
Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted for Android in the ThreeTenABP project; see How to use….Luke
B
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'
}
Bezoar answered 5/4, 2017 at 19:39 Comment(0)
A
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

Attainture answered 16/3, 2016 at 6:44 Comment(4)
Thanks for the outline. I guess our only option for Java 8 Time api is github.com/JakeWharton/ThreeTenABPMagnificat
@Igor right untill stable version of android-n will release. hope in stable version they will supportAttainture
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...Magnificat
Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted for Android in the ThreeTenABP project; see How to use….Luke
B
19

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

https://github.com/JakeWharton/ThreeTenABP

Bakelite answered 3/2, 2017 at 9:43 Comment(1)
This library is no longer maintained, deprecated, and the repo is now archived. You should now use Gradle 4.00 + desugaringRoulers
B
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

Babe answered 8/8, 2020 at 13:37 Comment(2)
Why do we need desugaring if we're using Kotlin?Magnificat
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?Babe

© 2022 - 2024 — McMap. All rights reserved.