Lỗi the java type java.util.date is not a supported type

The Android Gradle plugin 3.0.0 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version. When building your app using the Android Gradle plugin 4.0.0 and higher, you can use some Java 8 language APIs without requiring a minimum API level for your app.

This page describes the Java 8 language features you can use, how to properly configure your project to use them, and any known issues you may encounter. See the following video for an overview of Java 8 language features.

The Android Gradle plugin provides built-in support for using certain Java 8 language features and third-party libraries that use them. The default toolchain implements the new language features by performing bytecode transformations, called desugar, as part of the D8/R8 compilation of class files into DEX code, as shown in figure 1.

![Java 8 language feature support using desugar bytecode

transformations][//developer.android.com/static/studio/images/write/desugar_diagram.png] Figure 1\. Java 8 language feature support using `desugar` bytecode transformations.

Java 8 language feature support [Android Gradle Plugin 3.0.0+]

To start using supported Java 8 language features:

  1. to 3.0.0 or higher.
  2. For each module that uses Java 8 language features [either in its source code or through dependencies], update the module's build.gradle or build.gradle.kts file as shown below:

Kotlin

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

Groovy

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

When building your app using the Android Gradle plugin 3.0.0 and higher, the plugin doesn't support all Java 8 language features. The following language features are available on any API level:

Java 8 language feature Notes Lambda expressions Android doesn't support the serialization of lambda expressions. Method references Type annotations Type annotation information is only available at compile time, not at runtime. The platform supports in API level 24 and below, but not

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

0 or

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

1. Default and static interface methods Repeating annotations

In addition to these Java 8 language features, Android Gradle plugin versions 3.0.0 and higher extend support for

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

2-with-resources to all Android API levels.

Desugar doesn't support or . If your source code or one of your module dependencies uses one of these methods, you need to specify

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

5 or higher. Otherwise, you receive the following error:

Dex: Error converting bytecode to dex:
Cause: signature-polymorphic method called without --min-sdk-version >= 26

In some cases, your module might not be using the

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

6 or

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

7 methods even when they're included in a library dependency. To keep using that library with

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

8 or lower, to remove unused methods. If that doesn't work, consider using an alternative library that doesn't use the unsupported methods.

Java 8+ language features desugaring on the Android Gradle plugin 3.0.0 and higher and doesn't make any additional classes and APIs [such as

android {

...
// Configure only for each module that uses Java 8
// language features [either in its source code or
// through dependencies].
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
    jvmTarget = "1.8"
}
}

  1. available for use on older Android releases. Support for partial Java API desugaring is available from the Android Gradle plugin 4.0.0 or higher, as described in the following section.

Java 8+ API desugaring support [Android Gradle Plugin 4.0.0+]

If you're building your app using the Android Gradle plugin 4.0.0 or higher, the plugin extends support for using a number of Java 8 language APIs without requiring a minimum API level for your app. 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.

This additional support for older platform versions is possible because plugin 4.0.0 and higher extends the desugaring engine to also desugar Java language APIs. You can include standard language APIs that were available only in recent Android releases [such as

Dex: Error converting bytecode to dex:
Cause: signature-polymorphic method called without --min-sdk-version >= 26

  1. in apps that support older versions of Android.

The following set of APIs are supported when building your app using Android Gradle plugin 4.0.0 or higher:

  • Sequential streams [

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    1]
  • A subset of

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    2
  • Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    3
  • Recent additions to

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    4
  • Optionals [

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    5,

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    6, and

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

  • and some new classes
  • Some additions to

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    8 [new methods on

    Dex: Error converting bytecode to dex: Cause: signature-polymorphic method called without --min-sdk-version >= 26

    9, android {
    defaultConfig {  
        // Required when setting minSdkVersion to 20 or lower  
        multiDexEnabled = true  
    }  
    compileOptions {  
        // Flag to enable support for the new language APIs  
        // For AGP 4.1+  
        isCoreLibraryDesugaringEnabled = true  
        // For AGP 4.0  
        // coreLibraryDesugaringEnabled = true  
        // Sets Java compatibility to Java 8  
        sourceCompatibility = JavaVersion.VERSION_1_8  
        targetCompatibility = JavaVersion.VERSION_1_8  
    }  
    
    } dependencies {
    // For AGP 7.4+  
    coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]  
    // For AGP 7.3  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]  
    // For AGP 4.0 to 7.2  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]  
    
    } 0, and android {
    defaultConfig {  
        // Required when setting minSdkVersion to 20 or lower  
        multiDexEnabled = true  
    }  
    compileOptions {  
        // Flag to enable support for the new language APIs  
        // For AGP 4.1+  
        isCoreLibraryDesugaringEnabled = true  
        // For AGP 4.0  
        // coreLibraryDesugaringEnabled = true  
        // Sets Java compatibility to Java 8  
        sourceCompatibility = JavaVersion.VERSION_1_8  
        targetCompatibility = JavaVersion.VERSION_1_8  
    }  
    
    } dependencies {
    // For AGP 7.4+  
    coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]  
    // For AGP 7.3  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]  
    // For AGP 4.0 to 7.2  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]  
    
    } 1]
  • android {
    defaultConfig {  
        // Required when setting minSdkVersion to 20 or lower  
        multiDexEnabled = true  
    }  
    compileOptions {  
        // Flag to enable support for the new language APIs  
        // For AGP 4.1+  
        isCoreLibraryDesugaringEnabled = true  
        // For AGP 4.0  
        // coreLibraryDesugaringEnabled = true  
        // Sets Java compatibility to Java 8  
        sourceCompatibility = JavaVersion.VERSION_1_8  
        targetCompatibility = JavaVersion.VERSION_1_8  
    }  
    
    } dependencies {
    // For AGP 7.4+  
    coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]  
    // For AGP 7.3  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]  
    // For AGP 4.0 to 7.2  
    // coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]  
    
    } 2 [with bug fixes for Android 5.0]

With Android Gradle plugin 7.4.0 or higher, additional Java 11 APIs are supported such as a subset of the

android {

defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled = true
}
compileOptions {
    // Flag to enable support for the new language APIs
    // For AGP 4.1+
    isCoreLibraryDesugaringEnabled = true
    // For AGP 4.0
    // coreLibraryDesugaringEnabled = true
    // Sets Java compatibility to Java 8
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
} dependencies {
// For AGP 7.4+
coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]
// For AGP 7.3
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]
// For AGP 4.0 to 7.2
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]
}

3 package.

For a complete list of supported APIs, visit Java 8+ APIs available through desugaring and Java 11+ APIs available through desugaring.

To support these language APIs, the plugin compiles a separate DEX file that contains an implementation of the missing APIs and includes it in your app. The desugaring process rewrites your app’s code to instead use this library at runtime.

To enable support for these language APIs on any version of the Android platform:

  1. to 4.0.0 [or higher].
  2. Include the following in your app module’s`build.gradle` or build.gradle.kts file:

Kotlin

android {

defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled = true
}
compileOptions {
    // Flag to enable support for the new language APIs
    // For AGP 4.1+
    isCoreLibraryDesugaringEnabled = true
    // For AGP 4.0
    // coreLibraryDesugaringEnabled = true
    // Sets Java compatibility to Java 8
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
} dependencies {
// For AGP 7.4+
coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]
// For AGP 7.3
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]
// For AGP 4.0 to 7.2
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]
}

Groovy

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 {
// For AGP 7.4+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
// For AGP 7.3
// coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.3'
// For AGP 4.0 to 7.2
// coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.9'
}

Note that you may also need to include the previous code snippet in a library module's build.gradle or build.gradle.kts file if:

  • The library module's instrumented tests use these language APIs [either directly or through the library module or its dependencies]. This is so that the missing APIs are provided for your instrumented test APK.
  • You want to run lint on the library module in isolation. This is to help lint recognize valid usages of the language APIs and avoid reporting false warnings.

Also note that API desugaring can be combined with shrinking, but only when using the R8 shrinker.

Versions

The following table shows the versions of the Java 8+ API library and the minimum Android Gradle plugin version that supports each version:

Version Minimum Android Gradle plugin version 1.1.9 4.0.0 1.2.3 7.3.0 2.0.3 7.4.0-alpha10

For details on the versions of the Java 8+ API library, see the CHANGELOG.md file in the

android {

defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled = true
}
compileOptions {
    // Flag to enable support for the new language APIs
    // For AGP 4.1+
    isCoreLibraryDesugaringEnabled = true
    // For AGP 4.0
    // coreLibraryDesugaringEnabled = true
    // Sets Java compatibility to Java 8
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}
} dependencies {
// For AGP 7.4+
coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:2.0.3"]
// For AGP 7.3
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.2.3"]
// For AGP 4.0 to 7.2
// coreLibraryDesugaring["com.android.tools:desugar_jdk_libs:1.1.9"]
}

8 GitHub repository.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2023-09-14 UTC.

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]

Chủ Đề