Gson: NoSuchFieldException when serializing objects with an enumeration field on Samsung devices running Android 5.0.x

Created on 6 Sep 2016  路  7Comments  路  Source: google/gson

Stack trace

java.lang.AssertionError: java.lang.NoSuchFieldException: ENUM_CONSTANT_NAME
at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter#<init>(808)
at com.google.gson.internal.bind.TypeAdapters$30#create(834)
at com.google.gson.Gson#getAdapter(423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory#createBoundField(115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory#getBoundFields(164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory#create(100)
at com.google.gson.Gson#getAdapter(423)
at com.google.gson.Gson#toJson(661)
at com.google.gson.Gson#toJson(648)
at com.google.gson.Gson#toJson(603)
at com.google.gson.Gson#toJson(583)
at com.myapp.mypackage.MyClass#myMethod()

Crash Descriprion

I can see this crash on Samsung devices running Android 5.0.x only.
I'm using gson-2.7 and I'm not usin ProGurad.

The crash occurs when I try to serialize an object of a specific type that contains a field of type enumeration.
However, it does not occur sistematically but rather randomly.

As fas as I understand, such an exception cannot be caused by my app's code.
Moreover, by inspecting gson code, I cannot understand why this exception is generated. It seems to me that this should never happen:

for (T constant : classOfT.getEnumConstants()) {
          String name = constant.name();
          SerializedName annotation = classOfT.getField(name).getAnnotation(SerializedName.class);
...
}

The NoSuchFieldException exception is thrown when the Class.getField(String name) method is called (line 797)

Any idea on why this exception is thrown?
What could be the reason that causes a similar crash?

Most helpful comment

this is not a bug in samsungs version of android. i get the same thing on a nexus5x running android 8.1.0. its more lily a pro guard issue. See: https://stackoverflow.com/questions/15543607/assertionerror-in-gson-enumtypeadapter-when-using-proguard-obfuscation

All 7 comments

This is a bug in Samsung's version of Android. I don't think there's anything you can do and there's certainly nothing we can do.

Thanks for your support @JakeWharton. That is exactly what I suspected.

this is not a bug in samsungs version of android. i get the same thing on a nexus5x running android 8.1.0. its more lily a pro guard issue. See: https://stackoverflow.com/questions/15543607/assertionerror-in-gson-enumtypeadapter-when-using-proguard-obfuscation

@bpappin as described in the issue pro guard was not used

I also got same exception but problem was i extending model with facebook native ad i just make that field transient and it worked fine.

Anyone resolved this ? I am getting this issue on all my devices

Anyone resolved this ? I am getting this issue on all my devices

Try method from this article:
https://medium.com/@hanru.yeh/gson-will-crash-at-enum-fields-in-custom-class-with-proguard-bbcf5ad1b623
It helped me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  路  32Comments

JakeWharton picture JakeWharton  路  39Comments

GoogleCodeExporter picture GoogleCodeExporter  路  19Comments

GoogleCodeExporter picture GoogleCodeExporter  路  16Comments

LucianWang picture LucianWang  路  42Comments