Strange bug with long XML files in android.
Lately I encountered a strange bug while developing my android application, and I think it is strange enough to share it in this blog post.
Brestram is an android application that aims at delivering real time bus schedule for the people living in Brest. For each new version, I want to display a small message with the new features of the version. I need to deliver the message both in English and in French, and I wanted to keep history of all the messages I have written in the past for reference. On top of this, I wanted to be able to write that message in an understandable format (for me) to avoid formatting errors.
I decided to go for XML, for all the reasons above, and also because android supports it really well out of the box.
Here is how my update file would look like :
Basically, my update mechanism rely on two different things :
- A large group of XML arrays
Each array describes one update. Each of those updates is divided into 4 items : a title and a content, in English and then in French.
- Then, another array lists all the update messages.
Each item of the array contains a reference to an update, in ascending order.
To use this file, I wrote a small update functionality in Java (that may be the subject of a future blog post). The updater compares the value of the latest update with the current version number in database, and displays the update message if needed.
But basically, the java code of interest can be summarized in the few following lines :
But for some strange reason, things started acted weird last month. My 11th update wouldn’t show up. Instead, it was the 8th being displayed!
I checked the format of the xml times for a while, screening for a silly copy-pasting error or something alike. But I couldn’t find anything.
After some time, I started checking my Java code. During the debugging, I realized that my update array was detected as 8 rows long, instead of 11. At least I had found the reason why another update was showing up on screen instead of the one I was waiting for. But why?!
I kept searching for a while, realizing that everything was for example working fine if I was replacing the text of my 11th update by a copy of my 10th update message . . .
Tired of not being able to find the source of the problem, I started trying things that made no sense. And I finally realized that if I put the array containing the list of references to my updates BEFORE that actual updates, everything was fine.
I guess for some reasons the amount of data read by the xml parser is limited. When creating the typed array, only a fixed amount of data is grabbed from the xml resource file. When the number of updates grew, the xml file started being too long and I finally ended up grabbing only part of my array.
So basically, I am now 4 updates later, and things continue to behave normally, so I guess the problem is “fixed”. So far, I am still searching for a proof of these assumptions though. . . In case you know the exact reason why this happened, feel free to drop me a line in the comments of reach me on twitter.