如何修复 Android 中的 "Call requires API level 26 (current min is 25) " 错误
How to fix "Call requires API level 26 (current min is 25) " error in Android
我知道这个问题可能会被重复,但我没有找到我的问题的任何答案,我在我的 Android 应用程序中使用 LocalDateTime
,它需要 API 26 和我设备的 API 是 25。
我能做什么?非常感谢您的帮助。
您可以使用 ThreeTenBP. But for android it is recommended to use Jake Wharton's ThreeTenABP.
为什么不用 ThreeTenBP?
Similar to the problems with using Joda-Time on Android, the threetenbp uses a JAR resource for loading timezone information. This is an extremely inefficient mechanism on Android.
This library places the timezone information as a standard Android asset and provides a custom loader for parsing it efficiently.
为什么不用Joda-Time?
Joda-Time has a very large API which brings with it a very large binary size and large method count. The creator of both JSR-310 and Joda-Time has also said that while Joda-Time isn't broken, it does have design flaws.
If you are using Joda-Time already, there's little reason to switch unless its size or method count is relevant to you. For new projects, however, this library offers the standard APIs in Java 8 as a much smaller package in not only binary size and method count, but also in API size.
这些解释来自 Jake Wharton 的 ThreeTenABP。
您需要使用 https://github.com/JakeWharton/ThreeTenABP 才能在 Android API < 26 时使用 LocalDateTime。
将依赖项添加到您的项目(请遵循项目自述文件):
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
然后更改您的 LocalDateTime
导入来源:
import java.time.LocalDateTime;
至:
import org.threeten.bp.LocalDateTime;
更新:
上面提到的库不再是 JakeWharton/ThreeTenABP README 中提到的最佳方式:
Attention: Development on this library is winding down. Please consider switching to Android Gradle plugin 4.0, java.time.*, and its core library desugaring feature in the coming months.
要在较旧的 API 关卡中使用 LocalDateTime
,请使用 Gradle 插件 4.0 中的脱糖功能:
https://developer.android.com/studio/write/java8-support#library-desugaring
在较低版本的 Android 上使用 LocalDateTime
的最佳方法是脱糖(您必须具有 Android Gradle 插件版本 4.0 或更高版本)。只需将以下行添加到您的 app
模块 gradle 文件中:
最后,加上ff。依赖于你的依赖块:
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
要在较低版本的 Android 上使用 LocalDateTime
是通过脱糖(您必须有 Android Gradle 插件版本 4.0 或更高版本)启用 java8并在 app.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 // <- this flag is required
// Sets Java compatibility to Java 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") // <- this dependency is required
}
如果您有如上所述的 coreLibraryDesugaringEnabled = true
但问题仍未解决,请检查您是否有最新的 coreLibraryDesugaring 版本,因为我的情况是 1.1.1
升级到 1.1.5
解决了这个问题。
我知道这个问题可能会被重复,但我没有找到我的问题的任何答案,我在我的 Android 应用程序中使用 LocalDateTime
,它需要 API 26 和我设备的 API 是 25。
我能做什么?非常感谢您的帮助。
您可以使用 ThreeTenBP. But for android it is recommended to use Jake Wharton's ThreeTenABP.
为什么不用 ThreeTenBP?
Similar to the problems with using Joda-Time on Android, the threetenbp uses a JAR resource for loading timezone information. This is an extremely inefficient mechanism on Android.
This library places the timezone information as a standard Android asset and provides a custom loader for parsing it efficiently.
为什么不用Joda-Time?
Joda-Time has a very large API which brings with it a very large binary size and large method count. The creator of both JSR-310 and Joda-Time has also said that while Joda-Time isn't broken, it does have design flaws.
If you are using Joda-Time already, there's little reason to switch unless its size or method count is relevant to you. For new projects, however, this library offers the standard APIs in Java 8 as a much smaller package in not only binary size and method count, but also in API size.
这些解释来自 Jake Wharton 的 ThreeTenABP。
您需要使用 https://github.com/JakeWharton/ThreeTenABP 才能在 Android API < 26 时使用 LocalDateTime。
将依赖项添加到您的项目(请遵循项目自述文件):
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
然后更改您的 LocalDateTime
导入来源:
import java.time.LocalDateTime;
至:
import org.threeten.bp.LocalDateTime;
更新:
上面提到的库不再是 JakeWharton/ThreeTenABP README 中提到的最佳方式:
Attention: Development on this library is winding down. Please consider switching to Android Gradle plugin 4.0, java.time.*, and its core library desugaring feature in the coming months.
要在较旧的 API 关卡中使用 LocalDateTime
,请使用 Gradle 插件 4.0 中的脱糖功能:
https://developer.android.com/studio/write/java8-support#library-desugaring
在较低版本的 Android 上使用 LocalDateTime
的最佳方法是脱糖(您必须具有 Android Gradle 插件版本 4.0 或更高版本)。只需将以下行添加到您的 app
模块 gradle 文件中:
最后,加上ff。依赖于你的依赖块:
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
要在较低版本的 Android 上使用 LocalDateTime
是通过脱糖(您必须有 Android Gradle 插件版本 4.0 或更高版本)启用 java8并在 app.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 // <- this flag is required
// Sets Java compatibility to Java 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") // <- this dependency is required
}
如果您有如上所述的 coreLibraryDesugaringEnabled = true
但问题仍未解决,请检查您是否有最新的 coreLibraryDesugaring 版本,因为我的情况是 1.1.1
升级到 1.1.5
解决了这个问题。