question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
bpappincommented, 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

0reactions
Papashkincommented, Jul 30, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - NoSuchFieldException when serializing objects with ...
NoSuchFieldException when serializing objects with an enumeration field on Samsung devices running Android 5.0.x.
Read more >
java - NoSuchFieldException when field exists - Stack Overflow
I am getting NoSuchFieldException even when using getDeclaredField() , and the "has private access" error even after using setAccessible(true) . Example 1: ...
Read more >
Build.VERSION_CODES - Android Developers
VERSION_CODES extends Object ... They are assumed to support different screen densities and sizes. ... Released publicly as Android 5.0 in November 2014....
Read more >
Diff - platform/frameworks/base - Google Git
Note: This is set at first run and cannot be changed without * breaking apps that access the provider. diff --git a/core/java/android/provider/CallLog ...
Read more >
Gson proguard SerializedName
NoSuchFieldException when serializing objects with an enumeration field on Samsung devices running Android 5.0.x #924.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found