'LibGDX Android Production Failing to Load from JSON
I have developed and released a game using LibGDX platform for Android. I'm using Json to save and load information:
To save an object (a rocket class):
Json json = new Json();
String rocketStr = json.toJson(rocket);
To load this object:
Rocket rocket = json.fromJson(Rocket.class, rocketStr);
After the release, I have added a new advertising provider (adMob) and released the update to the game. I have not made any changes to the classes which are being saved. And I have not changed any library versions. The only change was some new classes added and used in the project.
With this new update the game could no longer load the saved objects with Json - it would come up with an exception when trying to run json.fromJson(Rocket.class, rocketStr); Exceptions would differ but mainly there were two: 1) property not found or 2) unable to cast value to some property.
After some experimenting, I found out that in production Android (or LibGDX?) obscures all the class names and property names and when Json is writing a class the resulting string looks like this:
{A:97.50652,A2:{E:RIGHTTHRUSTER,F:true,a:Liberator Thrusters,b:2,d:false,f:true,g:{}},H:24.934832,I:10,J:true,K:30,M2:1587,N2:1647,O2:1527,Y:-7.3379517,a:{seed0:28965116732361103,seed1:-4728429348234121846},a0:-19.308016,b:Liberator,b0:17.0175,c:1,d:1,e:false,j0:{a:500},m:true,o:497.43192,p:1587,s:100,t:100,u2:{E:TOP,F:true,a:Liberator Nose,b:1,c:1,d:false,f:true,f0:[],g:{}},z2:{E:LEFTTHRUSTER,F:true,a:Liberator Thrusters,b:2,c:1,d:false,f:true,f0:[],g:{}}}
And after the update, the same object saved has some different field names:
{B2:{E:LEFTTHRUSTER,F:true,a:Liberator Thrusters,b:43,c:1,f:true,f0:[],g:{}},C:100,D2:{E:RIGHTTHRUSTER,F:true,a:Liberator Thrusters,b:43,f:true,g:{}},J:0.128269,K:-7.5,L:true,N:true,O2:1587,P2:1647,Q2:1527,R:-5,a0:-2.9551423,c:{seed0:-3537680883966184457,seed1:6879650071827728698},d:Liberator,e:17,f:1,l0:{a:500},o:true,q:595.54803,r:1587,u:100,v:100,w2:{E:TOP,F:true,a:Liberator Nose,b:42,c:1,f:true,f0:[],g:{}}}
For example, in first example one of the properties is called 'A2' and in the second example the same property is called 'D2'
Is my understanding correct that Android (or LibGDX?) can change the obfuscated class names for the same classes when I make some changes to the project?
And most importantly, what can I do to make sure that the class / property names stay the same to be able to save and load the same object with Json after any updates? Are there any alternatives / better methods to save / load information?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
