How to fix "Call requires API level 26 (current min is 25) " error in Android
Asked Answered
V

6

41

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.

What can I do? Your help will be very appreciated.

Veto answered 21/6, 2019 at 2:6 Comment(2)
It is quite obvious that you can't use newer features on older devices. You have to either implement this feature by yourself, or find a library that provides similar functionalityLornalorne
Many years have past but I just wanted to point out that the comment by @VladyslavMatviienko is not valid anymore.Leighton
M
43

You need to use https://github.com/JakeWharton/ThreeTenABP to be able using LocalDateTime with Android API < 26.

Add the dependencies to your project (please follow the project README):

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'

Then change your LocalDateTime import from:

import java.time.LocalDateTime;

to:

import org.threeten.bp.LocalDateTime;

and please do not forget to call AndroidThreeTen.init(this) in your Application class onCreate method like;

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        AndroidThreeTen.init(this);
    }

}

Update:

The library mentioned above is no longer the best way as mentioned in 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.

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

Meissner answered 21/6, 2019 at 3:32 Comment(2)
It is recommended now in the ThreeTenABP repo that you use code desugaring(developer.android.com/studio/write/…)Abbacy
look also here #38282308 if using timezonesGlidebomb
C
55

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). Just add the below lines to your app module gradle file:

enter image description here

Finally, add the ff. dependency to your dependencies block:

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'

Charitycharivari answered 19/8, 2020 at 15:6 Comment(2)
This is recommended way by Google. Reference: developer.android.com/studio/write/…Cliffcliffes
Also, from the Android site: "With Android Gradle plugin 7.4.0 or higher, a number of Java 11 language APIs are also available with desugared library 2.0.0 or higher." These libraries come in three different flavors: minimal, default and nio (this one being the catch-all flavor since it also includes the packages from the other two).Leighton
M
43

You need to use https://github.com/JakeWharton/ThreeTenABP to be able using LocalDateTime with Android API < 26.

Add the dependencies to your project (please follow the project README):

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'

Then change your LocalDateTime import from:

import java.time.LocalDateTime;

to:

import org.threeten.bp.LocalDateTime;

and please do not forget to call AndroidThreeTen.init(this) in your Application class onCreate method like;

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        AndroidThreeTen.init(this);
    }

}

Update:

The library mentioned above is no longer the best way as mentioned in 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.

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

Meissner answered 21/6, 2019 at 3:32 Comment(2)
It is recommended now in the ThreeTenABP repo that you use code desugaring(developer.android.com/studio/write/…)Abbacy
look also here #38282308 if using timezonesGlidebomb
S
13

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 {
    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

Sabina answered 20/12, 2021 at 6:21 Comment(1)
this answer worked for me because with just adding the dependency coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5"), I had to put @RequiresApi(Build.VERSION_CODES.O) above the function that used the code that required API26. Once I added coreLibraryDesugaringEnabled = true to the compileOptions, I no longer had to do that.Rhombencephalon
L
4

You can use ThreeTenBP. But for android it is recommended to use Jake Wharton's ThreeTenABP.

Why not use 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.

Why not use 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.

These explanations came from Jake Wharton's ThreeTenABP.

Lactam answered 21/6, 2019 at 2:18 Comment(1)
I just followed the link you gave me but the error is always there :(Veto
U
1

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.

Uncrown answered 3/3, 2022 at 4:11 Comment(0)
D
0

Don't forget to click 'Sync Project With Gradle Files' for the configurations to take into effect.

This resolves the warning messages that remain.

Dispossess answered 1/12, 2022 at 2:21 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPhrensy

© 2022 - 2024 — McMap. All rights reserved.