gpt4 book ai didi

Android IN Java 8 java.time

转载 作者:IT老高 更新时间:2023-10-28 23:18:26 25 4
gpt4 key购买 nike

我更新到最新的 Android N sdk。我唯一不明白的是为什么我不能将 java.time 导入到我的代码中?我以为Java8可以通过Android N获得。那为什么谷歌没有添加java.time包?

最佳答案

java.time 包仅在 API 26 (Android O) 中添加: https://developer.android.com/reference/java/time/package-summary.html

更新

从 Android Gradle 插件 4.0 版开始,您可以使用 java.time API 的子集(以及许多其他 Java 8 语言 API)无需您的应用需要最低 API 级别: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 theabove APIs
  • Some additions to java.util.concurrent.atomic (new methods on AtomicInteger, AtomicLong and AtomicReference)
  • ConcurrentHashMap (with bug fixes for Android 5.0)

要启用对这些语言 API 的支持,需要在 build.gradle 文件中包含以下几行:

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'
}

关于Android IN Java 8 java.time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36000997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com