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

How to exlude a jar file from an Android dependency?

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

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

我曾尝试添加一些符号来排除库,但不幸的是 none 其中的一些符号成功了。一旦我构建项目,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()
}

可排除的依赖:

我做错了什么或者我应该如何从依赖项中排除 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)

我认为这应该可行:

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

编辑

这个答案可能会对您有所帮助:

加入你的gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

在你的build.gradle中添加

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

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