Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
angelocoiro opened this issue Sep 6, 2016 · 7 comments

Comments

@angelocoiro
Copy link

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?

@JakeWharton
Copy link
Contributor

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.

@angelocoiro
Copy link
Author

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

@bpappin
Copy link

bpappin commented Apr 13, 2018

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

@angelocoiro
Copy link
Author

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

@itboy87
Copy link

itboy87 commented Jan 6, 2019

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.

@tasneembohra
Copy link

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

@Papashkin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants