程序类型已经存在:com.google.common.util.concurrent.ListenableFuture

8

我刚把我的项目转换成了androidx,并且现在遇到了“程序类型已经存在”错误,错误出现在com.google.common.util.concurrent.ListenableFuture上。

我查看了多个stackoverflow解决方案以及Gradle文档,但仍然无法解决问题。

问题是Gradle在这些模块中引入了两个版本的ListenableFuture。

Gradle: com.google.quava:quava:23.5jre@jar 
Gradle: com.google.guava:listenablefuture:1.0@jar

我猜想我想要排除第二个,但是不知道如何做。
你可以在我的gradle文件中看到我尝试过的内容,但是迄今为止没有成功。
    apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.drme.weathertest"
        minSdkVersion 26
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

//    added 1/2/19 to try and prevent java.lang.NoClassDefFoundError: Failed resolution of:
//    Landroid/view/View$OnUnhandledKeyEventListener;
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == "com.android.support") {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion "26.+"
                }
            }
        }
//        resolutionStrategy.force 'com.google.quava.listenablefuture:1.0',
//                'com.google.common.util.concurrent.listenablefuture'
    }

//    compile('com.google.guava:listenablefuture:1.0@jar') {
//        // causing "Program type already present" compile errors
//        //exclude("com.google.guava:listenablefuture:1.0")
//        exclude('com.google.guava:listenablefuture')
//        exclude('listenablefuture-1.0.jar')
//        //exclude("maven.com.google.quava.listenablefuture")
//    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src/main/java/com.drme.weatherNoaa/Data', 'src/main/java/2']
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    // causing "Program type already present" compile errors // did not fix problem
    implementation('com.google.guava:listenablefuture:1.0') {
        exclude module: 'com.google.guava:listenablefuture'
    }
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'androidx.arch.core:core-testing:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha01'
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha01"
    annotationProcessor "androidx.room:room-compiler:2.1.0-alpha03"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.code.gson:gson:2.8.2'
    testImplementation 'junit:junit:4.12'
    implementation 'androidx.test:runner:1.1.1'
    implementation 'androidx.preference:preference:1.1.0-alpha02'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'androidx.media:media:1.1.0-alpha01'
    implementation 'androidx.room:room-runtime:2.1.0-alpha03'
    implementation 'androidx.room:room-testing:2.1.0-alpha03'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'androidx.core:core:1.1.0-alpha03'
    implementation 'androidx.room:room-compiler:2.1.0-alpha03'
}

如果我正确理解依赖关系,它似乎来自于androidx.core:core:1.1.0-alpha03
+--- androidx.core:core:1.1.0-alpha03@aar
+--- androidx.concurrent:concurrent-futures:1.0.0-alpha02@jar
+--- com.google.guava:listenablefuture:1.0@jar   

欢迎提出建议,非常感谢。

3个回答

16

找到了问题和答案。在项目视图中查看外部库时,我发现以下内容:

Gradle: com.google.guava:guava:23.5-jre@jar 
 |_ guava-23.5-jre.jar 
    |_com.google
      |_common 
        |_util.concurrent
          |_ListenableFuture

Gradle: com.google.guava:listenablefuture:1.0@jar
 |_listenablefuture-1.0.jar
   |_com.google.common.util.concurrent
     |_ListenableFuture
第二个条目中唯一的事情就是重复的ListenableFuture。通过在gradle构建文件中添加以下条目,这个问题消失了:
configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == "com.android.support") {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion "26.+"
                }
            }
        }

        all*.exclude group: 'com.google.guava', module: 'listenablefuture' <===This fixed the problem
    }

我不知道是什么产生了com.google.guava:listenablefuture:1.0@jar,但由此导致的问题已经解决。

感谢所有审核该问题的人。


“Program type already present” 意味着有重复的类存在...排除所有出现的情况很可能会产生不良副作用;最好保留其中一个。./gradlew app:dependencies 可以列出它们。如果我的方法不起作用,可能存在两个以上的情况。 - Martin Zeitler
Martin:感谢您的建议。我只知道我的Android Studio项目中有两个build.gradle文件,但无法弄清如何运行您的建议以列出依赖项。我查看了Gradle文档,并几乎通过添加构建扫描来破坏了我的项目。 :) 您能否提供一个链接以更详细地说明如何实现您的建议?我不是命令行大师。谢谢。 - mtdavem
它在依赖项解析后被包含,配置':app:annotationProcessor'时出现了错误。 - Amin Pinjari
这是我必须在我的应用程序 gradle 文件配置中包含的内容。all { all * .exclude group:'com.google.guava',module:'listenablefuture' } - Neri
它能工作,但出现了一些问题:Caused by: java.lang.ClassNotFoundException: androidx.work.impl.utils.futures.AbstractFuture - Darmawan Z.

3

你不能从 ListenableFuture 中排除 ListenableFuture

... 相反,尝试类似这样的方式:

implementation ("androidx.core:core:1.1.0-alpha03") {
    exclude group: "com.google.guava", module: "listenablefuture"
}

Martin:这对我没有起作用,但给了我一些其他的想法去尝试。请看我的问题答案。感谢您的帮助。 - mtdavem

2

androidx.core:core-ktx更新到版本1.1.0-rc01也解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接