使用 RxJava 2 和 Retrofit 2,适配器版本问题
Using RxJava 2 and Retrofit 2, adapter version issue
我在 Android Studio 中向现有应用程序模块添加了一个新的库模块。主要区别在于添加了 RxJava 2 和 Retrofit 2。
更新新模块 build.gradle 后,我开始遇到下一个错误:
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.0.2\cfccdd18cdfbe7b4773d42c9f3512eeafbe5cbf9\rxjava-2.0.2.jar
File2:
C:\Users\Gaket.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava.1.5\ece7b5d0870e66d8226dab6dcf47a2b12afff061\rxjava-1.1.5.jar
我看到 RxJava 有一些问题(我想尝试 RxJava 2,在这里我看到了 RxJava 1 和 RxJava 2)。添加所有依赖项后问题就出现了,我们在主应用程序中没有使用 RxJava
。
更新:
正如我在下面提到的,我检查了三个主题(你可以在下面看到)。本期: is similar to the one that I already mention below: 。他们都有解决方法,但没有考虑到我在回答中强调的真正问题。 答案是 "There is no production-ready adapter from Retrofit 2 to RxJava 2 at the moment"(完整细节在下面的答案中描述)。
我检查了几个主题: is some concrete solution for a concrete problem, talks about rxbindings
, but I don't use them, some others also have not resolved my problem or look like total workarounds 。我尝试 google "rxjava 2 and retrofit 2" 但没有找到解决方案。
这里是图书馆的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'
}
问题是:
为什么会出现这个问题,正确的处理方法是什么?
您应该使用 second 版本的 RxJava 的官方适配器:
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0 // works with RxJava 2
适配器的版本为 2.*.*
并不意味着它是为 RxJava 2 使用而设计的,正如我所想的那样。
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' // won't work with RxJava 2
以前,您应该使用一个临时版本:这里是 Jake Wharton 的存储库 Retrofit 2 to RxJava 2 adapter。
我在 Android Studio 终端中使用命令 gradlew :app dependencies
发现了这个问题,其中 app
是我的库模块的名称。我在那里寻找 "rxjava" 关键字并找到了它的不同版本。
更新:
正如Darshan Mistry在评论中指出的那样,21 Square 在 2 月发布了适配器的正式版本。答案已更新。
如果多次添加不同版本,需要从项目中删除重复的库。
或者如果两个相互依赖且版本不同的库将具有不同的 meta inf 版本,这将导致此类问题。
如何检查Dependency/hierarchy
参考 回答
在你的情况下,jar meta inf 中有多个 rxjava.properties
因此,要忽略该重复条目,请将其从 android
标签
中应用程序的 build.gradle
中排除
packagingOptions {
exclude 'META-INF/rxjava.properties'
}
我在 Android Studio 中向现有应用程序模块添加了一个新的库模块。主要区别在于添加了 RxJava 2 和 Retrofit 2。 更新新模块 build.gradle 后,我开始遇到下一个错误:
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.0.2\cfccdd18cdfbe7b4773d42c9f3512eeafbe5cbf9\rxjava-2.0.2.jar File2: C:\Users\Gaket.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava.1.5\ece7b5d0870e66d8226dab6dcf47a2b12afff061\rxjava-1.1.5.jar
我看到 RxJava 有一些问题(我想尝试 RxJava 2,在这里我看到了 RxJava 1 和 RxJava 2)。添加所有依赖项后问题就出现了,我们在主应用程序中没有使用 RxJava
。
更新:
正如我在下面提到的,我检查了三个主题(你可以在下面看到)。本期:
我检查了几个主题:rxbindings
, but I don't use them, some others also have not resolved my problem or look like total workarounds
这里是图书馆的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'
}
问题是:
为什么会出现这个问题,正确的处理方法是什么?
您应该使用 second 版本的 RxJava 的官方适配器:
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0 // works with RxJava 2
适配器的版本为 2.*.*
并不意味着它是为 RxJava 2 使用而设计的,正如我所想的那样。
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' // won't work with RxJava 2
以前,您应该使用一个临时版本:这里是 Jake Wharton 的存储库 Retrofit 2 to RxJava 2 adapter。
我在 Android Studio 终端中使用命令 gradlew :app dependencies
发现了这个问题,其中 app
是我的库模块的名称。我在那里寻找 "rxjava" 关键字并找到了它的不同版本。
更新: 正如Darshan Mistry在评论中指出的那样,21 Square 在 2 月发布了适配器的正式版本。答案已更新。
如果多次添加不同版本,需要从项目中删除重复的库。
或者如果两个相互依赖且版本不同的库将具有不同的 meta inf 版本,这将导致此类问题。
如何检查Dependency/hierarchy
参考
在你的情况下,jar meta inf 中有多个 rxjava.properties
因此,要忽略该重复条目,请将其从 android
标签
build.gradle
中排除
packagingOptions {
exclude 'META-INF/rxjava.properties'
}