RxJava2与Retrofit2冲突

Posted by phantomVK on September 25, 2017

如果同时引入以下库

implementation 'io.reactivex.rxjava2:rxjava:2.1.3'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation '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

即使通过以下方式忽略META-INF/rxjava.properties,重新编译后还会出现Unable to merge dex错误

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

报错的原因是com.squareup.retrofit2:adapter-rxjava只能支持Retrofit1,不支持Retrofit2,所以只要引入以下的库替代即可:

implementation 'com.squareup.retrofit2:adapter-rxjava2:1.0.0'

参考链接: