Gson Will Crash at Enum Fields When Enabling Proguard

Bram Yeh
Bram Yeh
Feb 17, 2017 · 1 min read
  • Root Cause of Crash when Gson serialized Enum with Proguard

This issue might crash here: the name of Enum constant is different from Enum filed. This is because Proguard would modifies the enums’ constant names and Gson would throw NoSuchFieldException when deserializing enum constants from JSON data.

  • How to resolve this crash

To solve this problem, I have 2 solutions.

  1. Paring enums via @SerializedName annotation:

To add @SerializedName for each Enum type, as following,

public enum CarBrand{
@SerializedName("bmw")
BMW,
@SerializedName("mercedes")
Mercedes,
@SerializedName("lamborghini")
Lamborghini
}

, and Gson would use serialized name for JSON form.

2. Create a type adapter (e.g. LowercaseEnumTypeAdapterFactory) for Gson

Gson office document offers a LowercaseEnumTypeAdapterFactory; to import this adapter into project, and use GsonBuilder().registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory()) to create Gson.
https://gist.github.com/b44272a1a53866f129d0df60b1ab28c6.git


First solution looks easier, however, we have lots enums and I afraid someone will forget to add corresponding serialized names for each enum. So I prefer second solution to implement a factory that creates a type adapter that will write enums in lowercase and map it to the correct constant.

By the way, if we used EnumMap, only @SerializedName won’t work with enum types. We have to use GsonBuilder.enableComplexMapKeySerialization() to avoid this EnumMap problem.

Bram Yeh

Written by

Bram Yeh

Lead Android & iOS Mobile Engineer at Yahoo (Verizon Media) Taiwan https://www.linkedin.com/in/hanruyeh/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade