Android Use Java Time With Java8 Library Desugaring

Apr 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

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.