问题
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:
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
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.
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).
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.
Here is the build.gradle
of library:
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?
回答1:
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
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
Previously, you should use a temporary version: Here is the repository of Jake Wharton with Retrofit 2 to RxJava 2 adapter.
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.
Update: On February, 21 Square published the official version of adapter, as Darshan Mistry pointed out in a comment. Answer updated.
回答2:
Need to remove duplicate library from the project if added multiple times with different version.
or if two libraries which are dependent and have different version thn both will have different meta inf version which will cause this type of issue.
How to check Dependency/hierarchy Refere this answer
In your case there are multiple rxjava.properties
in jar meta inf
So to ignore that duplicate entry exclude it from the application's build.gradle
in android
tag
packagingOptions {
exclude 'META-INF/rxjava.properties'
}
来源:https://stackoverflow.com/questions/41140203/using-rxjava-2-and-retrofit-2-adapter-version-issue