How to use AutoValue with Retrofit 2?
Asked Answered
N

2

12

I've got AutoValue (and the android-apt plugin) working in a project, and I'm aware of Ryan Harter's gson extension for AutoValue, but how do I hook Retrofit 2 up to use the extension and factory method on the abstract class?

String grantType = "password";
Call<SignIn> signInCall = retrofitApi.signIn(email, password, grantType);
signInCall.enqueue(callback);

eg here I would like to use AutoValue with the SignIn JSON model object to enforce immutability but how do I hook up Retrofit (or perhaps more specifically Gson) to the immutable, AutoValue model class?

Nonlinearity answered 10/4, 2016 at 11:40 Comment(0)
S
26

[update] The library have changed a bit, check more here: https://github.com/rharter/auto-value-gson

I was able to make it work like this. I hope it will help you.

  • Import in your gradle app file

    apt 'com.ryanharter.auto.value:auto-value-gson:0.3.1'

  • Create object with autovalue:

    @AutoValue public abstract class SignIn {    
        @SerializedName("signin_token") public abstract String signinToken();
        @SerializedName("user") public abstract Profile profile();
    
        public static TypeAdapter<SignIn> typeAdapter(Gson gson) {
            return new AutoValue_SignIn.GsonTypeAdapter(gson);
        }
    }
    
  • Create your Type Adapter Factory (Skip if using version > 0.3.0)

    public class AutoValueGsonTypeAdapterFactory implements TypeAdapterFactory {
    
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            Class<? super T> rawType = type.getRawType();
    
            if (rawType.equals(SignIn.class)) {
                return (TypeAdapter<T>) SignIn.typeAdapter(gson);
            } 
    
            return null;
        }
    }
    
  • Create your Gson converter with your GsonBuilder

    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(
            new GsonBuilder()
                    .registerTypeAdapterFactory(new AutoValueGsonTypeAdapterFactory())
                    .create());
    
  • Add it to your retrofit builder

    Retrofit retrofit = new Retrofit
            .Builder()
            .addConverterFactory(gsonConverterFactory)
            .baseUrl("http://url.com/")
            .build()
    
  • Do your request

  • Enjoy

Bonus live template:
In your autovalue class, type avtypeadapter then autocomplete to generate the type adapter code. To work you need to add this as a live template in Android Studio.

public static TypeAdapter<$class$> typeAdapter(Gson gson) {
    return new AutoValue_$class$.GsonTypeAdapter(gson);
}

Live template configuration

How to create and use the live template.

Live template gif

Snailpaced answered 12/4, 2016 at 20:41 Comment(2)
great explanation, appreciate it. I have most of the part working but I am not able to find AutoValueGsonTypeAdapterFactory. Keep getting this error "Error:(40, 57) error: cannot find symbol class AutoValueGsonTypeAdapterFactory"Lisette
You have to use @GsonTypeAdapterFactory on an abstract class that implements TypeAdapterFactory. Check it there github.com/rharter/auto-value-gson#factorySnailpaced
S
2

Here's a Gist by Jake Wharton for a Gson TypeAdapterFactory that just requires you add an annotation to all of your AutoValue classes that require working with Gson https://gist.github.com/JakeWharton/0d67d01badcee0ae7bc9

Works great for me.

Here's some proguard help too..

-keep class **.AutoValue_*
-keepnames @yourpackage.AutoGson class *
Spense answered 4/9, 2016 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.