Using Firebase for setting update priorities on Android

Update (27/04/2022): Fixed JSON to have String as a key type. Thanks, @janhavisinghh for letting me know.

Keeping the app up-to-date has a lot of benefits for the user, they can use all the new features, have the latest bug fixes and improvements and have access to the latest security patches. While some users update the app periodically or have the background updates enabled, others might not.

Google has a library to address this issue, which is aptly named In-app updates. which allows us to prompt the users to update their app. It gives a lot of different information and allows us to start the update right from the app. One of the interesting properties available as part of this library is updatePriority . Which lets us handle how we want to display a message that there is an update or how we update the app (flexibile or immediate).

We can set integer values between 0 & 5 for update priority, with 0 being the default and 5 being the highest priority. In order for us to set the update priority, we have to use the Google Play Developer API, and then we can check it on the client-side using AppUpdateInfo#getPriority().

While this is a great way for us to define priorities for our updates, and handle them accordingly in our app. We are required to use Google Developer API to set this, there is no other way around it currently. We cannot update it from Google Play Console while making a release as well. Personally, I would like to have control over it. I want to be able to update the priority while or after making a release. For that, we can use Firebase Remote Config.

Firebase Remote Config is a service that lets us control the behavior and appearance of the app. It will fetch this information from the Firebase servers periodically and update them. We can use this to pass in a minimal JSON file that let us define essentially a key/value pair with version code being the key and priority being the value.

Something like this

 "8145": 1,
 "8150": 5

Now, all we have to do is parse this information and use it on the client-side.

There are a lot of JSON parsing libraries for Android out there. I am using Moshi for this, but you can use any one of them. We can first start by defining what type we are expecting the parsed JSON is and create a Moshi adapter for it.

val type = Types.newParameterizedType(,,
val updatePrioritiesAdapter = moshi.adapter<Map<String, Int>>(type)

Then we can fetch our JSON from the Firebase Remote Config and use the adapter to parse it

val json = remoteConfig.getValue("update_priorities").toString()
val updatePriorities: Map<String, Int> = updatePrioritiesAdapter.fromJson(json)

So, we have our Map<Int, Int> which contains our update priorities information. Now we need to get the version code for the update from our app. For that, we can use the in-app updates library which provides us with other information like the available version code which is the version code of the update that is available from Google Play store.

appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
  val availableVersionCode = appUpdateInfo.availableVersionCode().toString()
  val updatePriority = updatePriorities.getOrDefault(availableVersionCode, defaultUpdatePriority)
  // handle priority

That’s it. Now we can easily update the update priority for a new version without having to do that using Google Developer API 🙌