Android Use Java Time With Java8 Library Desugaring

April 20, 2020

Previously if you need to use java.time.* packages in Android API 21/26 or below, you need to use ThreeTenABP.

Thanks to Java 8 library desugaring in D8 and R8, android can now support java.time.* packages in Android below API 21.

Edit app/module build.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

        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

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

NOTE: If you bump into error Could not find method coreLibraryDesugaringEnabled

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.