Alexei
Alexei

Reputation: 15716

Android Studio 3.1.4: java.time.LocalDate" is not supported

Android Studio 3.1.4, Java 8, Gradle 4.4.

in my build.gradle:

        compileSdkVersion 26
        minSdkVersion 18
        targetSdkVersion 26

      compileOptions {
            targetCompatibility JavaVersion.VERSION_1_8
            sourceCompatibility JavaVersion.VERSION_1_8
        }

        implementation 'com.android.support:appcompat-v7:26.1.0'

In my POJO

import java.time.LocalDate;
import java.util.Date;
public class Product {
    private Date redeemed_date;
    private LocalDate myLocalDate;
}

I must use minSdkVersion 18 because it's client requirement.

But when build I get error:

error: Field "myLocalDate" of type "java.time.LocalDate" is not supported.

Upvotes: 1

Views: 1934

Answers (2)

mfekim
mfekim

Reputation: 505

Since april 2020 with Android Gradle Plugin 4.0.0, you can enable support for using a number of Java 8 language APIs (see all supported APIs: https://developer.android.com/studio/write/java8-support-table)

To enable support for these language APIs on any version of the Android platform, update the Android plugin to 4.0.0 (or higher) and include the following in your module’s build.gradle file:

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

Source: https://developer.android.com/studio/write/java8-support

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1007296

The JavaDocs for LocalDate show that it was added in API Level 26. Your minSdkVersion is below that, and so LocalDate will not be supported on all the devices that you wish to run on.

Your choices are:

  1. Raise your minSdkVersion to 26, or

  2. Use something else, such as ThreeTenABP, that Christopher recommended in a comment

Upvotes: 2

Related Questions