Handling old data in new app versions


SDK Version: 
M3

When you release a newer version of your application, on update the android system identifies from the package name and version number, that the user has an older version installed. During updating not all data of the previous version is overwritten, databases, preferences, previously downloaded/created files on sdcard or internal storage remain unchanged. If there are any incompatibility with the old version of these, is must be taken care of.

First of all, files on the external storage can be lost or corrupted due to many other reasons, so version incompatibility is just one more reason to check those files before using them.
However the internal storage is theoretically not modified by external sources, if there are version incompatibilities, you have to check the state of this files too.

Values stored in preferences are also unaffected by updating app version. For example if you stored a string under the same key in the previous version, and the current version stores an integer under the same key, the attempt to read the value as an integer can result in a crash.

For databases if you are using the built in SQLiteOpenHelper class it has the onUpgrade method, which you are forced to implement, to handle database version changes. Just be sure to call its constructor with bigger version number when you change the database structure, and the onUpgrade method will be called when first using the database after updating to an application version with a new database structure. Here you can do whatever is needed to solve the issue.

If you can not use the SQLiteOpenHelper class, you must take care of detecting new database version yuorself. With the SQLiteDatabase.setVersion() and getVersion() methods it is not a difficult task anyway.

So, do not forget to test applications by installing them over an older version too, and if there are any problems take care of it.
For example you can compile a constant version number to the builds, and store it in preferences, and check against it on every start. If the data stored by previous versions are incompatible, the easiest solution is to delete, the all the old data to simulate a "fresh" install. But if you want to keep previous user data, you may have to do complex conversions depending on your application.