mtdavem
mtdavem

Reputation: 541

Program type already present: com.google.common.util.concurrent.ListenableFuture

I have just converted my project to androidx and am now getting the "Program type already present" error for com.google.common.util.concurrent.ListenableFuture.

I have looked through multiple stackoverflow solutions and some of the Gradle documentation and still can't get it to work.

The problem is that there are two versions of ListenableFuture brought in by Gradle in these modules:

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

I would guess that I want to exclude the second one, but can not figure out how to do it.

You can see what I have tried in my gradle file, but no joy so far.

    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'
}

If I am looking at the dependencies correctly, it looks like it comes from 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   

Suggestions would be appreciated. Thanks.

Upvotes: 8

Views: 10435

Answers (3)

Mehul Kanzariya
Mehul Kanzariya

Reputation: 1348

Updating the androidx.core:core-ktx to version 1.1.0-rc01 also solved the problem.

Upvotes: 2

mtdavem
mtdavem

Reputation: 541

Found the problem and the answer. Looking at the External Libraries in the Project view I found the following:

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

The only thing in the second entry was the duplicate ListenableFuture.

By making the following entry in the gradle build file, this problem went away:

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
    }

I could not figure out what was generating the com.google.guava:listenablefuture:1.0@jar, but the resulting problem was solved.

Thanks for all who reviewed the problem.

Upvotes: 16

Martin Zeitler
Martin Zeitler

Reputation: 76569

you cannot exclude listenablefuture from listenablefuture

... try something alike this instead:

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

Upvotes: 3

Related Questions