gradle 将ConcurrentHashMap::newKeySet与Android核心库反糖化结合使用

nue99wik  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(117)

我正在尝试使用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.access$2100(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.ConcurrentHashMapclasses3.dex中定义,但不包括newKeySet方法。然而j$.util.concurrent.ConcurrentHashMap也在classes4.dex中定义,并且确实有这个方法!
我尝试解决的ConcurrentHashMap.newKeySet调用位于pircbotx依赖项中。但是,我在应用程序的onCreate方法中调用了此方法,这也会导致应用程序崩溃。
谢谢你的帮助,我已经忙了好几个小时了,但还是没有结果。

eoigrqb6

eoigrqb61#

这被证明是D8中的一个“bug”,事实证明,尽管以下3种方法在文档(https://developer.android.com/studio/write/java8-support-table)中被列为受支持,但它们实际上当前并不受支持。
这是一项功能请求,但根据D8团队的说法,本季度不太可能考虑这些功能。(有关该问题的完整讨论,请访问:(x、x、e、f、x)

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

oxcyiej72#

此问题已通过将脱糖依赖关系升级到1.2.0解决,如下所示:

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.0'
    // or
    // coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.0'

...
}

请参阅以下信息:

  • 1.2.0的脱糖库更改日志
  • ConcurrentHashMap(包含适用于Android 5.0的错误修复)

相关问题