1

I'm using Gson to create a JSON String from a list of objects. This string then gets sent to our database. This works perfectly fine on my test device and emulator and it's stored correctly on the database when I run it on the devices from my laptop. Problems arise when I then upload our app to the Play Store: we're getting the strings in the following format stored on our database:

[{"a":true,"b":"Schedule 1","c":[true,true,true,true,true,true,true]}]

Whereas it's supposed to be something like this:

[{"active":true,"scheduleName":"Schedule 1","days":[true,true,true,true,true,true,true]}]

This happens for all schedules that get sent to our server. Can anyone help with this? Why is this happening?

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
  • 1
    Why weird? Is ProGuard obfuscation enabled for your build? – terrorrussia-keeps-killing Mar 08 '22 at 17:59
  • 2
    To expand on the above- if you use proguard, you need an exception for any class you use GSON on. This needs to be recursive- if class Foo includes a class Bar instance and GSON is used on Foo, Bar needs an exception as well. – Gabe Sechan Mar 08 '22 at 18:21
  • 2
    In addition to configure exceptions in the ProGuard configuration, you can also pin JSON field names to their respective fields in the mappings by using `@SerializedName` (it will be more verbose, but ProGuard never affects annotation values (I mean strings, numeric constants and booleans) as far I as I know, but I do believe it's a sign of good code design especially for dedicated DTOs containing all necessary configurations for a particular library like Gson), so even if the class field gets obfuscated from `active` to `a`, its name provided in `@SerializedName` would retain as declared. – terrorrussia-keeps-killing Mar 08 '22 at 18:30
  • Thanks everyone, this helped a lot! I cannot mark any of your answers as solution since they're comments, but ProGuard was indeed the cause – Jorn Rigter Mar 10 '22 at 14:08

0 Answers0