如何解决在 APK META-INF/rxjava.properties 中复制的重复文件

How to resolve Duplicate files copied in APK META-INF/rxjava.properties

我在 android 应用程序中使用 rxjava 和 rxvolley。当我尝试 运行 时,我得到了这个错误

Execution failed for task ':testapp:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties
            File1: C:\Users\Daniel\.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava.1.08f0546d5c3c27f1aef07270ffea0c45f0c42a4\rxjava-1.1.0.jar
            File2: C:\Users\Daniel\.gradle\caches\modules-2\files-2.1\io.reactivex.rxjava2\rxjava.0.3\d2f725668bd22e21170381b23f8fbdf72c69d886\rxjava-2.0.3.jar

我有一个 exclude.gradle 这样的文件

android {
packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/rxjava.properties'
    exclude 'META-INF/rxjava.PROPERTIES'
    exclude 'META-INF/RXJAVA.properties'
    exclude 'META-INF/RXJAVA.PROPERTIES'
    exclude 'META-INF/rxjava'
    exclude 'META-INF/RXJAVA'
}

lintOptions {
    abortOnError false
}
}

我该如何解决这个问题?

我遇到了同样的问题,并通过将以下代码放入 app/build.gradle 文件中修复了它。请注意,您必须在路径末尾放置“*”以排除文件夹内的所有文件。您将必须根据错误描述更改以下代码中要排除的文件的路径。

compileSdkVersion 25
    buildToolsVersion "24.0.3"

    packagingOptions {
        exclude 'com/google/appengine/repackaged/org/apache/commons/codec/language/bm/*'
        exclude 'com/google/appengine/repackaged/org/codehaus/jackson/impl/*'
        exclude 'com/google/appengine/repackaged/org/apache/commons/codec/language/*'
    }

我遇到了同样的问题。
就我而言,我使用的是 Retrofit2 但我认为问题出在 rx libraries

这是我正在使用的 build.gradle (module:app) 并且在我的案例中有效。

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

compile 'io.reactivex:rxandroid:1.1.0' //<-use this
compile 'io.reactivex:rxjava:1.1.3'    //<-use this

compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

无论如何,如您所见,有一个更好的解决方案

我今天遇到了这个问题并解决了这个问题

compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

//RxJava dependencies
compile 'io.reactivex.rxjava2:rxandroid:2.0.0'
compile 'io.reactivex.rxjava2:rxjava:2.0.2'
compile 'org.reactivestreams:reactive-streams:1.0.0'

我遇到了同样的问题。我修复它的方法是在应用 gradle 中添加 packagingOptions,如 Duplicated file rxjava.properties

中所述
android {

    defaultConfig {
    }
    buildTypes {
    }
    packagingOptions{
        exclude 'META-INF/rxjava.properties'
    }
}

我也有这个问题,我也用了和你一样的方法,但是因为我有两个模块,所以我只在依赖Rxjava的模块中更改它,最后我修复它是在app中添加packaginOptions gradle

packagingOptions {
    exclude 'META-INF/rxjava.properties'
}

我也有同样的问题,但很容易删除重复的 "comment" 所有 Rxjava 相关的依赖项,它没有在我的代码中使用。

//RxJava removes or comment duplicated and not used dependencies.
      //  implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0'
       // implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
       // implementation 'io.reactivex.rxjava2:rxjava:2.1.13'

如果您在 2019 年及以上遇到此问题,这很可能是因为您使用的是 已弃用 RxJava 2 CallAdapter.Factory 即

com.jakewharton.retrofit:retrofit2-rxjava2-adapter

您将必须删除该依赖项并添加此

implementation 'com.squareup.retrofit2:adapter-rxjava2:latest.version'

请从here

获取最新版本

在build.gradle.app

中添加此代码
android {
 ...
 ...
 ...

 packagingOptions {
        exclude 'META-INF/rxjava.properties'
 }
}