我刚刚将项目转换为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

建议将不胜感激。谢谢。

最佳答案

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

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,但是解决了所产生的问题。

感谢所有审查此问题的人。

关于android - 程序类型已经存在: com. google.common.util.concurrent.ListenableFuture,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54174685/

10-09 00:48