简体   繁体   English

如何修复 Android 中的“呼叫需要 API 级别 26(当前最小值为 25)”错误

[英]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;

Update:更新:

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
}

Supporting docs 支持文档

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.

相关问题 调用需要API级别26(当前最小值为23):java.time.Instant#now - Call requires API level 26 (current min is 23): java.time.Instant#now 错误:调用需要API级别23(当前最小值为15): - Error : Call requires API level 23(current min is 15): 调用要求API级别21,当前最小值为8 - Call requires API level 21, current min is 8 调用需要API级别11(当前最小值为8)android.app.Activity#onCreateView - Call requires API level 11(current min is 8) android.app.Activity#onCreateView 调用需要API级别11(当前最小值为8):android.app.Activity#onCreateView FragmentActivity - Call requires API level 11 (current min is 8): android.app.Activity#onCreateView FragmentActivity 调用要求API级别16(当前最小值为14),并带有EndAction - Call requires API level 16 (current min is 14) withEndAction 错误:调用需要 API 级别 24(当前最小值为 21):java.util.Map#forEach [NewApi] - Error: Call requires API level 24 (current min is 21): java.util.Map#forEach [NewApi] 需要API级别21(当前最小值为16) - Requires API level 21 (current min is 16) 我有一个错误“调用要求API级别11(当前最小值为8)”。 这是我在项目(ECLISPE)中可以找到的 - I have an error “Call requires API level 11 (current min is 8)”. This is what I can find in my project (ECLISPE) 调用需要 API 级别 23(当前最小值为 22)同时从应用程序发出调用请求 - Call requires API level 23 (current min is 22) While making a call request from App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM