javakotlingradleandroid-gradle-pluginjdk-desugaring

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/Math8 when upgrading Gradle and Android Gradle Plugin


I'm working on an Android app with a Gradle version of 7.1.1 and an Android Gradle Plugin version of 7.0.0. When I upgrade to Gradle version 7.2 and Android Gradle Plugin version 7.1.1, I get the following error.

2022-03-02 17:15:47.072 25300-25300/... E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ..., PID: 25300
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/Math8;
        at j$.time.Instant.ofEpochSecond(Instant.java:328)
        at j$.time.Instant.<clinit>(Instant.java:232)
        at j$.time.Instant.ofEpochMilli(Instant.java:344)
        ...
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.Math8" on path: DexPathList[[zip file "/data/app/...-NbMXeOj8LumN03n4IMK5Cw==/base.apk"],nativeLibraryDirectories=[/data/app/...-NbMXeOj8LumN03n4IMK5Cw==/lib/x86, /data/app/...-NbMXeOj8LumN03n4IMK5Cw==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at j$.time.Instant.ofEpochSecond(Instant.java:328) 
        at j$.time.Instant.<clinit>(Instant.java:232) 
        at j$.time.Instant.ofEpochMilli(Instant.java:344) 
        ...
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

The error is coming from this code:

fun toLocalStartOfDay(date: Long): Instant = Instant.ofEpochMilli(date)
    .atZone(ZoneId.systemDefault())
    .withHour(0)
    .withMinute(0)
    .withSecond(0)
    .withNano(0)
    .toInstant()

The build.gradle file is set to target JVM 1.8 with desugaring.

compileOptions {
    coreLibraryDesugaringEnabled = true
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = '1.8'
}

The desugar_jdk_libs version is set to 1.0.9.

versions.androidDesugaringVersion = '1.0.9'
support.android_desugaring = "com.android.tools:desugar_jdk_libs:$versions.androidDesugaringVersion"

Why would upgrading cause this error?


Solution

  • Desugaring effects "a subset of java.time" so upgrading to the latest version of desugar_jdk_libs should fix the issue. At the time of posting, the latest version is 1.1.5.

    References