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

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

我正在开发一个 Android 应用程序,其中 Gradle 版本 7.1.1 和 Android Gradle 插件版本 7.0.0.当我升级到 Gradle 版本 7.2 和 Android Gradle 插件版本 7.1.1 时,出现以下错误。

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)

错误来自此代码:

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

build.gradle 文件设置为以 JVM 1.8 为目标并进行脱糖。

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

kotlinOptions {
    jvmTarget = '1.8'
}

desugar_jdk_libs版本设置为1.0.9

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

为什么升级会导致这个错误?

脱糖影响“java.time 的一个子集”,因此升级到最新版本的 desugar_jdk_libs 应该可以解决这个问题。发帖时最新版本是1.1.5.

参考资料