retrofit convertor factory can not access GsonConverterFactory

AndroidGsonRetrofit

Android Problem Overview


I have included these dependencies to my project:

> compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
> compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'

I have a class where I am going to access my api's via retrofit:

 public static  <S> S createService(Class<S> serviceClass, String baseUrl) {


        Retrofit builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();    
           
            RestAdapter adapter = builder.build();*/

        return  builder.create(serviceClass);
    }

And now, it gives me this compile time error :

> Error:(24, 17) error: method addConverterFactory in class Builder > cannot be applied to given types; required: Factory found: > GsonConverterFactory reason: actual argument GsonConverterFactory > cannot be converted to Factory by method invocation conversion

How can I solve this? I followed the documentation. What is wrong?

Android Solutions


Solution 1 - Android

Try to use same version for retrofit and converter-gson - 2.0.0-beta2. You are using beta2 for retrofit and beta1 for converter.

implementation 'com.squareup.retrofit:retrofit:2.0.0-beta2'
implementation 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

Important note!

Retrofit change its package name since 2.0.0-beta3 version. Now you should use com.squareup.retrofit2. Here is example:

implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'

Solution 2 - Android

In build.gradle (app) instead of:

implementation 'com.google.code.gson:gson:2.8.2'

write:

implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

Solution 3 - Android

With latest Beta 2.0.3 release you need to add :

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

Make sure to match the retrofit library version with gson converter version.

Solution 4 - Android

This is the latest:

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

If you use beta version:

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

Solution 5 - Android

  error: method addConverterFactory in class Builder cannot be applied to given types;
    required: Factory
    found: GsonConverterFactory
    reason: actual argument GsonConverterFactory cannot be converted to Factory by method invocation conversion

If you are getting this error, The reason is wrong dependancy included.

Add/ change dependency in application build.gradle file as

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'  
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

make sure that converter version is 2.0.0-beta2 not 2.0.0-beta1.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAEMLovijiView Question on Stackoverflow
Solution 1 - AndroidIlya TretyakovView Answer on Stackoverflow
Solution 2 - AndroidCoolMindView Answer on Stackoverflow
Solution 3 - AndroidSurajView Answer on Stackoverflow
Solution 4 - AndroidjoeView Answer on Stackoverflow
Solution 5 - AndroidmanojView Answer on Stackoverflow