-->

Migrate from RxAndroid 1.x to 2.x (RxJava included

2019-05-26 15:16发布

问题:

I have a project running RxAndroid 1.x (everythings works). I am trying to migrate to 2.x version.

My gradle file:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    //compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
        transitive = true;
    }

    //compile 'io.reactivex:rxandroid:1.2.1'
    //compile 'io.reactivex:rxjava:1.2.1'
    compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'

    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    compile 'io.reactivex.rxjava2:rxjava:2.0.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.0-RC1'
}

I have this error:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties
    File1: /Users/agustin/.gradle/caches/modules-2/files-2.1/io.reactivex.rxjava2/rxjava/2.0.0/5151c737c01616c372c3d00ab145868ede10e826/rxjava-2.0.0.jar
    File2: /Users/agustin/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.1.0/748f0546d5c3c27f1aef07270ffea0c45f0c42a4/rxjava-1.1.0.jar

Why RxJava 1.1.0 is added?

I can exclude the rxjava.properties but I would like to understand the error. I have cleaned the project and invalidated the cache but the error persists.

Look the picture:

Thanks

回答1:

To fix this, now you can update the RxBinding and Retrofit Adapter to version to 2.

compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
compile 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'


回答2:

Just wanted to follow-up with a solution to this problem. RxBinding 1.0.0 has been released and from this point on it should be compatible with RxJava2, however it still imports an older version of RxJava which results in the same issue this question asks about. In order to fix the problem simply add the following to your gradle build scrip under android.

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


回答3:

You can find out what libraries may be including RxJava 1.1.0 by running the command ./gradlew :app:dependencies in the root of your project where app is the name of the module you are including these dependencies from.

In your case I'd suggest the offending item is: com.jakewharton.rxbinding:rxbinding:0.4.0