37

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.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Anna
  • 383
  • 1
  • 3
  • 4
  • 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 functionality – Vladyslav Matviienko Jun 21 '19 at 06:25
  • Many years have past but I just wanted to point out that the comment by @VladyslavMatviienko is not valid anymore. – joakie Aug 16 '23 at 20:35

6 Answers6

50

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'

Jim
  • 1,027
  • 1
  • 10
  • 19
  • 3
    This is recommended way by Google. Reference: https://developer.android.com/studio/write/java8-support#library-desugaring – Thanh Nguyen Aug 24 '21 at 02:33
  • 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). – joakie Aug 16 '23 at 20:56
41

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

oguzhan
  • 2,073
  • 1
  • 26
  • 23
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    It is recommended now in the ThreeTenABP repo that you use code desugaring(https://developer.android.com/studio/write/java8-support#library-desugaring) – Hiro Nov 25 '20 at 18:40
11

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

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
  • 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. – Lance Samaria May 02 '23 at 22:56
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.

Community
  • 1
  • 1
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
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.

Amir
  • 1,290
  • 1
  • 13
  • 20
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.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33310432) – Vojin Purić Dec 05 '22 at 16:02