如何从Android依赖项中排除jar文件?

时间:2019-05-07 13:04:55

标签: java android gradle dependencies androidx

我已经阅读并尝试了很多解决此问题的方法,但是我做错了什么,因为它对我不起作用。

我刚刚将Android更新为具有依赖项androidx.preference:preference:1.1.0-alpha04的AndroidX。此依赖项包括名为com.google.guava:listenablefuture:1.0@jar的jar文件。问题是,com.google.common.util.concurrent.ListenableFuture在依赖项中被发现两次,这在构建项目时给我一个错误。我还发现,可以删除整个jar文件(androidx.preference:preference:1.1.0-alpha04),因为其中的文件已经在另一个依赖库中。

我试图添加一些表示法来排除该库,但没有一个能顺利完成。构建项目后,jar文件就会不断添加。

  

build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('org.ros.android_core:android_10:[0.3, 0.4)') {
        exclude group: 'junit'
        exclude group: 'xml-apis'
    }
    // e.g. official msgs
    implementation 'org.ros.rosjava_messages:std_msgs:0.5.11'
    implementation 'org.ros.rosjava_messages:sensor_msgs:1.12.7'
    implementation 'org.ros.rosjava_messages:geometry_msgs:1.12.7'
    implementation 'org.ros.rosjava_bootstrap:message_generation:0.3.3'
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha5'
    implementation ("androidx.preference:preference:1.1.0-alpha04", {
        exclude group: 'com.google.guava'
    })
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:2.27.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

repositories {
    mavenCentral()
}

可以排除的依赖项:

enter image description here

我在做什么错?我该如何从依赖项中排除jar文件?

编辑:我要执行此操作的原因是因为出现以下错误:

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-12.0.jar (com.google.guava:guava:12.0) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)

1 个答案:

答案 0 :(得分:1)

我认为这应该可行:

implementation ("androidx.preference:preference:1.1.0-alpha04", {
    exclude group: 'com.google.guava'
})

编辑

此答案可能会帮助您: https://stackoverflow.com/a/54717425/7947225

添加gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

在您的build.gradle中,添加

configurations {
   all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}

然后清理并最终构建项目。

相关问题