5

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?

Community
  • 1
  • 1
Gaket
  • 6,533
  • 2
  • 37
  • 67
  • Not it is not a dublicate. I showed that I looked at similar questions and they contain workarounds. Look at the answers below. – Gaket Dec 14 '16 at 10:55
  • Yeah, I see ... it's rather duplicate of how to list dependencies in gradle http://stackoverflow.com/questions/21645071/using-gradle-to-find-dependency-tree – Selvin Dec 14 '16 at 10:56
  • Probably, it is closer to this one. However, I have a concrete solution for a concrete problem: using Retrofit 2 together with RxJava 2. It is not enough just to see that existing adapter uses RxJava inside. – Gaket Dec 14 '16 at 11:01
  • 1
    The title is missleading ... So if you don't read whole question you don't know that the main problem is not a duplicate of .properties file but *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)* – Selvin Dec 14 '16 at 11:03
  • Agree. Tried to update the question title. You can suggest one if you wish. – Gaket Dec 14 '16 at 11:04
  • The problem is that nor Retrofit nor RxJava don't say that something is wrong and this Gradle message is the single thing that shows the problem. – Gaket Dec 14 '16 at 11:05

2 Answers2

20

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.

Gaket
  • 6,533
  • 2
  • 37
  • 67
  • 2
    RxJava 2 adapter is merge with Retrofit, now you must remove compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' dependanay and add this one : compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' – Darshan Mistry Feb 22 '17 at 10:46
3

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'
    }
Community
  • 1
  • 1
user1140237
  • 5,015
  • 1
  • 28
  • 56
  • 1
    Thank you, but as for me, it is a workaround, I saw it in other topics. I think I've found a solution - stable version of adapter is not published yet. – Gaket Dec 14 '16 at 10:38