skip to Main Content

In my Android application, after converting a custom data class to a string as json, I save it as shared preferences. In the new version, I need to add a new parameter to the data class here and check it and display it on the screen where relevant.
Will users of the old version experience a crash when I release the new version of the application with the added parameter?
Or what are the possible scenarios. Does anyone have an idea?

I am using Gson to convert data class to json and back to data class.

2

Answers


  1. You can make new parameters is nullable, so the old user will not crashed until parsing.

    or if you use Gson you can create the TypeAdapter for data class and do logic when parse json string to make sure it succeed.

    Let check: https://www.tutorialspoint.com/gson/gson_custom_adapters.htm

    Login or Signup to reply.
  2. Most likely this will not be an issue because Gson will not fail if the value for a field is missing (see related Gson feature request). However, your code has to account for the new field being null (or for primitive types having the default value) if the data comes from a previous version of your app and no value for this field is present.

    You can also assign a default value to the field (e.g. public String newField = "default";) which is used if the field is missing in the JSON data, but in that case you have to make sure your class is static (explicitly, or implicitly) and has a no-args constructor. Otherwise you could run into issues with Gson’s usage of JDK Unsafe (see also GsonBuilder.disableJdkUnsafe()).

    Since this is an Android app, you also have to make sure you had configured ProGuard or R8 properly in the past, or used @SerializedName. Otherwise your JSON data might use obfuscated field names such as a, b… and those names might change in the new version of your app, which would prevent it from reading the JSON data. See the Gson Troubleshooting Guide for more information.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search