使用 ConcurrentHashMap::newKeySet 和 Android coreLibraryDesugaring

Using ConcurrentHashMap::newKeySet with Android coreLibraryDesugaring

我正在尝试使用 AGP 中新的脱糖功能,但是在我的应用程序中尝试使用 ConcurrentHashMap.newKeySet() 时出现以下错误:

10-23 21:17:49.471 5023-5023/uk.org.mattford.scoutlink E/AndroidRuntime: FATAL EXCEPTION: main
    Process: uk.org.mattford.scoutlink, PID: 5023
    java.lang.NoSuchMethodError: No static method newKeySet()Lj$/util/concurrent/ConcurrentHashMap$KeySetView; in class Lj$/util/concurrent/ConcurrentHashMap; or its super classes (declaration of 'j$.util.concurrent.ConcurrentHashMap' appears in /data/app/uk.org.mattford.scoutlink-1/base.apk:classes3.dex)
        at org.pircbotx.hooks.managers.ThreadedListenerManager.<init>(ThreadedListenerManager.java:49)
        at org.pircbotx.Configuration$Builder.getListenerManager(Configuration.java:884)
        at org.pircbotx.Configuration$Builder.addListener(Configuration.java:726)
        at uk.org.mattford.scoutlink.irc.IRCService.connect(IRCService.java:143)
        at uk.org.mattford.scoutlink.irc.IRCService.onStartCommand(IRCService.java:67)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2864)
        at android.app.ActivityThread.access00(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的顶层有以下内容build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.google.gms:google-services:4.3.4'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
    }
}

在我的模块中 build-gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 29
    buildToolsVersion '29.0.3'
    defaultConfig {
        applicationId "uk.org.mattford.scoutlink"
        minSdkVersion 16
        targetSdkVersion 29
        multiDexEnabled true
    }
    buildTypes {
        release {}
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
    }
    buildFeatures {
        viewBinding = true
    }
}

dependencies {
    implementation 'org.slf4j:slf4j-android:1.7.30'
    implementation 'org.pircbotx:pircbotx:2.3-SNAPSHOT'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.firebase:firebase-analytics:17.6.0'
    implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
    implementation 'com.google.android.material:material:1.2.1'

    implementation "androidx.room:room-runtime:2.2.5"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
    annotationProcessor "androidx.room:room-compiler:2.2.5"

    implementation 'androidx.multidex:multidex:2.0.1'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
}

查看 APK 分析器。我可以看到 j$.util.concurrent.ConcurrentHashMap 是在 classes3.dex 中定义的,但不包括 newKeySet 方法。然而 j$.util.concurrent.ConcurrentHashMap 也在 classes4.dex 中定义并且确实有这个方法!

我试图解决的 ConcurrentHashMap.newKeySet 调用在 pircbotx 依赖项中。但是我已经在我的应用程序的 onCreate 方法中调用了这个方法,这也使应用程序崩溃。

在此先感谢您的帮助。我已经在这几个小时了,现在无济于事。

这被证明是 D8 中的一个“错误”,事实证明尽管以下 3 种方法在文档 (https://developer.android.com/studio/write/java8-support-table) 中被列为受支持,但实际上目前并不支持它们。

这是一项功能请求,但根据 D8 团队的说法,本季度不太可能关注它们。 (此处对该问题进行全面讨论:https://issuetracker.google.com/171666278

public ConcurrentHashMap.KeySetView keySet(Object mappedValue)
public static ConcurrentHashMap.KeySetView newKeySet()
public static ConcurrentHashMap.KeySetView newKeySet(int initialCapacity)