Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJavaCallAdapterFactory cannot be converted to Factory

I'm trying to use Retrofit 2 and RxJava following the guide in this https://inthecheesefactory.com/blog/retrofit-2.0/en

In section "RxJava Integration with CallAdapter " explains how use RxJava with retrofit

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://api.nuuneoi.com/base/")
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();

However during compilation there is the following error:

Error:(63, 71) error: incompatible types: RxJavaCallAdapterFactory cannot be converted to Factory

How can I fix it? Thanks

like image 947
Luisa Repele Avatar asked Jun 09 '16 13:06

Luisa Repele


4 Answers

Make sure you are using the same groupId and version for the main Retrofit dependency, the Gson converter dependency, and the RxJava adapter dependency.

I'm guessing yours look something like this:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'

(Note that they use different groupIds and version numbers)

They should all look the same like this:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
like image 129
Jake Wharton Avatar answered Nov 03 '22 18:11

Jake Wharton


Currently adding "com.squareup.retrofit:adapter-rxjava:2.0.0" gives following error

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

But, instead, adding "compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0",makes packaging successful

like image 20
Amit Maskery Avatar answered Nov 03 '22 17:11

Amit Maskery


change your retrofit rxjava adapter into

  implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"

And change your CallAdapterFactory into

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

it will look something like this

.baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
like image 30
Eldhopj Avatar answered Nov 03 '22 19:11

Eldhopj


Use updated and latest libs.

// retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'

    // rxjava3
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.0'

NOTE :: RxJava3CallAdapterFactory.create() instead of RxJava2CallAdapterFactory.create()

Retrofit.Builder()
            .baseUrl("https://datanapps.com")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
            .build()
like image 1
Yogendra Avatar answered Nov 03 '22 19:11

Yogendra