Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Jackson JSON parser returns null value in 'release' builds

I'm using the Jackson JSON library within my Android app to parse JSON from a REST API. Everything works fine other than a couple of instances where I'm using ObjectMapper.readValue and ObjectMapper.treeToValue to deserialise the JSON to objects. It works fine every time when running the 'debug' build variant, but when running the 'release' build it fails to create the object and just returns null instead.

I've read a lot about ensuring that the Proguard settings are configured properly, but I'm not using Proguard and have removed all references to Proguard in my app. Is there anything else that might be causing the issue with the 'release' build?

Jonathan

like image 750
Jonathan Wareham Avatar asked Feb 10 '17 14:02

Jonathan Wareham


2 Answers

Turns out the problem was due to the class I was using to deserialise my JSON was declared within my activity class, therefore an inner class. I read that inner classes can be used with the Jackson ObjectMapper as long as they were declared static which this was, so not exactly sure why it didn't work in 'release' mode. As soon as I moved it to become a regular public class it all worked fine.

like image 83
Jonathan Wareham Avatar answered Oct 04 '22 04:10

Jonathan Wareham


This was the only post I found that described a similar situation I experienced but in my case there was no inner class. It turns out that it was due to the fact that some fields were package private. Everything worked fine in a debug build but on the release build those fields were null. Changed them to public and it worked fine. Setting getters/setters for all would probably also work but I didn't try it.

like image 23
lbarbosa Avatar answered Oct 04 '22 04:10

lbarbosa