使用RxJava 2和Retrofit 2,适配器版本问题 [英] Using RxJava 2 and Retrofit 2, adapter version issue

查看:128
本文介绍了使用RxJava 2和Retrofit 2,适配器版本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向Android Studio中的现有应用程序模块添加了一个新的库模块.主要区别在于添加了RxJava 2和Retrofit 2.更新新模块的build.gradle后,我开始出现下一个错误:

I've added a new library module to existing application module in Android Studio. The main difference was adding RxJava 2 and Retrofit 2. After updating build.gradle of new module I started to get next error:

错误:任务':app:transformResourcesWithMergeJavaResForBetaNewApiDebug'的执行失败.com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException:文件重复复制到APK META-INF/rxjava.properties文件1中:C:\ Users \ Gaket.gradle \ caches \ modules-2 \ files-2.1 \ io.reactivex.rxjava2 \ rxjava \ 2.0.2 \ cfccdd18cdfbe7b4773d42c9f3512eeafbe5cbf9 \ rxjava-2.0.2.jar文件2:C:\ Users \ Gaket.gradle \ caches \ modules-2 \ files-2.1 \ io.reactivex \ rxjava \ 1.1.5 \ ece7b5d0870e66d8226dab6dcf47a2b12afff061 \ rxjava-1.1.5.jar

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForBetaNewApiDebug'. com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties File1: C:\Users\Gaket.gradle\caches\modules-2\files-2.1\io.reactivex.rxjava2\rxjava\2.0.2\cfccdd18cdfbe7b4773d42c9f3512eeafbe5cbf9\rxjava-2.0.2.jar File2: C:\Users\Gaket.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava\1.1.5\ece7b5d0870e66d8226dab6dcf47a2b12afff061\rxjava-1.1.5.jar

我看到RxJava存在一些问题(我想尝试RxJava 2,在这里我同时看到RxJava 1和RxJava 2).添加所有依赖项后,问题立即出现,我们在主应用程序中不使用 RxJava .

I see that there is some problem with RxJava (I want to try RxJava 2 and here I see both RxJava 1 and RxJava 2). The problem arises right after adding all dependencies, we do not use RxJava in our main application.

更新:正如我在下面提到的,我检查了三个主题(您可以在下面看到).此问题:可能的重复是类似于我在下面已经提到的内容:这一个.两者都具有未考虑到我在回答中强调的实际问题的变通办法.而答案是目前尚无从Retrofit 2到RxJava 2的可用于生产的适配器"(下面的答案中描述了完整的细节).

Update: As I mentioned below, I checked three topics (you can see it below). This issue: possible duplicate is similar to the one that I already mention below: this one. Both of them have workarounds that do not take into account the real problem that I underlined in my answer. And the answer is "There is no production-ready adapter from Retrofit 2 to RxJava 2 at the moment" (full details described in answer below).

我检查了几个主题:第二讨论了 rxbindings ,但是我没有使用它们,其他一些也没有解决我的问题,或者总体解决方法这一个.我尝试用Google搜索"rxjava 2和翻新2",但没有找到解决方法.

I checked several topics: first is some concrete solution for a concrete problem, second talks about rxbindings, but I don't use them, some others also have not resolved my problem or look like total workarounds this one. I tried to google "rxjava 2 and retrofit 2" but haven't found a solution.

这是库的 build.gradle :

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}



dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',      {
    exclude group: 'com.android.support', module: 'support-annotations'
})

compile "com.android.support:appcompat-v7:$supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"

compile 'io.reactivex.rxjava2:rxjava:2.0.2'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
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.makeramen:roundedimageview:1.3.0"

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
provided 'javax.annotation:jsr250-api:1.0'
compile 'com.jakewharton:butterknife:8.4.0'

testCompile 'junit:junit:4.12'

}

问题是:
为什么会出现此问题,正确的解决方法是什么?

The question is:
Why does this problem happen and what is the proper way to handle it?

推荐答案

RxJava的 second 版本应使用官方适配器:

You should use the official adapter for the second version of RxJava:

compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0 // works with RxJava 2

适配器具有版本 2.*.* 的事实并不意味着它打算与RxJava 2一起使用.

The fact that adapter has version 2.*.* does not mean that it is intended for use with RxJava 2, as I thought.

compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' // won't work with RxJava 2

以前,您应该使用一个临时版本:这是Jake Wharton的存储库,其中将Retrofit 2修改为RxJava 2适配器.

Previously, you should use a temporary version: Here is the repository of Jake Wharton with Retrofit 2 to RxJava 2 adapter.

我在Android Studio终端中使用命令 gradlew:app依赖项发现了这个问题,其中 app 是我的库模块的名称.在那里,我寻找了"rxjava"关键字,并发现了它的不同版本.

I found this problem using command gradlew :app dependencies in Android Studio terminal, where the app is name of my library module. There I looked for "rxjava" keyword and found different versions of it.

更新:2月21日,正如 Darshan Mistry 在评论中指出的,21 Square发布了适配器的正式版本.答案已更新.

Update: On February, 21 Square published the official version of adapter, as Darshan Mistry pointed out in a comment. Answer updated.

这篇关于使用RxJava 2和Retrofit 2,适配器版本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆