[英]How to fix "Call requires API level 26 (current min is 25) " error in Android
I know this question may be duplicated but I didn't find any answer for my problem, I'm using LocalDateTime
in my Android app that requires API 26 and my device's API is 25.我知道这个问题可能会重复,但我没有找到我的问题的任何答案,我在我的 Android 应用程序中使用
LocalDateTime
,它需要 API 26,而我的设备的 API 是 25。
What can I do?我能做什么? Your help will be very appreciated.
非常感谢您的帮助。
You need to use https://github.com/JakeWharton/ThreeTenABP to be able using LocalDateTime with Android API < 26.您需要使用https://github.com/JakeWharton/ThreeTenABP才能将 LocalDateTime 与 Android API < 26 一起使用。
Add the dependencies to your project (please follow the project README):将依赖项添加到您的项目中(请遵循项目 README):
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
Then change your LocalDateTime
import from:然后更改您的
LocalDateTime
导入:
import java.time.LocalDateTime;
to:到:
import org.threeten.bp.LocalDateTime;
The library mentioned above is no longer the best way as mentioned in JakeWharton/ThreeTenABP README:上面提到的库不再是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.
请考虑在未来几个月内改用 Android Gradle 插件 4.0、java.time.* 及其核心库脱糖功能。
To use LocalDateTime
in older API levels, use the desugaring feature from Gradle plugin 4.0: https://developer.android.com/studio/write/java8-support#library-desugaring要在较旧的 API 级别使用
LocalDateTime
,请使用 Gradle 插件 4.0 中的脱糖功能: https : //developer.android.com/studio/write/java8-support#library-desugaring
The best way to use LocalDateTime
on a lower versions of Android is by desugaring (you must have Android Gradle plugin version 4.0 or higher).在较低版本的 Android 上使用
LocalDateTime
的最佳方法是脱糖(您必须拥有 Android Gradle 插件版本 4.0 或更高版本)。 Just add the below lines to your app
module gradle file:只需将以下几行添加到您的
app
模块 gradle 文件中:
Finally, add the ff.最后,添加ff。 dependency to your dependencies block:
对您的依赖项块的依赖:
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
You can use ThreeTenBP .您可以使用ThreeTenBP 。 But for android it is recommended to use Jake Wharton's ThreeTenABP .
但是对于 android,建议使用 Jake Wharton 的ThreeTenABP 。
Why not use ThreeTenBP?为什么不使用 ThreeTenBP?
Similar to the problems with using Joda-Time on Android, the threetenbp uses a JAR resource for loading timezone information.
与在 Android 上使用 Joda-Time 的问题类似,threetenbp 使用 JAR 资源来加载时区信息。 This is an extremely inefficient mechanism on Android.
这是 Android 上极其低效的机制。
This library places the timezone information as a standard Android asset and provides a custom loader for parsing it efficiently.
该库将时区信息作为标准 Android 资产放置,并提供自定义加载器以高效解析它。
Why not use Joda-Time?为什么不使用 Joda-Time?
Joda-Time has a very large API which brings with it a very large binary size and large method count.
Joda-Time 有一个非常大的 API,它带来了非常大的二进制大小和大量的方法数。 The creator of both JSR-310 and Joda-Time has also said that while Joda-Time isn't broken, it does have design flaws.
JSR-310 和 Joda-Time 的创建者也表示,虽然 Joda-Time 没有损坏,但它确实存在设计缺陷。
If you are using Joda-Time already, there's little reason to switch unless its size or method count is relevant to you.
如果您已经在使用 Joda-Time,除非它的大小或方法计数与您相关,否则几乎没有理由切换。 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.
然而,对于新项目,这个库提供了 Java 8 中的标准 API 作为一个小得多的包,不仅在二进制大小和方法计数方面,而且在 API 大小方面。
These explanations came from Jake Wharton's ThreeTenABP.这些解释来自杰克沃顿的 ThreeTenABP。
To use LocalDateTime
on lower versions of Android is by desugaring (you must have Android Gradle plugin version 4.0 or higher) enable java8 and use the code below in your app.gradle
要在较低版本的 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
}
If you have coreLibraryDesugaringEnabled = true
as mentioned above but still see the issue is not resolved, check that you have the latest coreLibraryDesugaring version as in my case it was 1.1.1
which upgrading to 1.1.5
fixed the issue.如果您如上所述有
coreLibraryDesugaringEnabled = true
但问题仍未解决,请检查您是否有最新的 coreLibraryDesugaring 版本,因为在我的情况下它是1.1.1
,升级到1.1.5
解决了这个问题。
Don't forget to click 'Sync Project With Gradle Files' for the configurations to take into effect.不要忘记单击“Sync Project With Gradle Files”以使配置生效。
This resolves the warning messages that remain.这解决了剩余的警告消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.