RxJava2学习笔记

Posted by ijays on June 20, 2017

RxJava2 学习笔记

今天刚好捣鼓了下Rxjava2,期间遇到了一些问题,这里给自己做个记录。

一开始为了图省事,使用了如下的依赖:

    compile 'io.reactivex.rxjava2:rxjava:2.1.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

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

结果曝了如下错误:

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/ijaysdev/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.3.0/af000bec2036a2a9d07197c4b03b8966bfc60b03/rxjava-1.3.0.jar
  	File2: /Users/ijaysdev/.gradle/caches/modules-2/files-2.1/io.reactivex.rxjava2/rxjava/2.1.0/2fdf84dedcaaeabb9d70cde9dbb8aad4eccb80a1/rxjava-2.1.0.jar

Google 才之后发现是rxjava-adapter 的锅,如果继续在Rxjava2 中使用这个adapter 就会曝这个错误,替代方式是使用Rxjava2 适用的adapter,于是依赖就变成了:


    compile 'io.reactivex.rxjava2:rxjava:2.1.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'