I have been fascinated with the new In-App Update API provided by Google as it helps me to ask my users to update the app easily. Previously, I had to work around a lot of things like creating proper dialogs or showing an appropriate card to the users and ask them to update the app from the play store where many users complained that they did not get the update option (due to cache issues) or they were getting errors while trying to update the app.

But now, with the In-App Update API, the work has been less tiresome and easier. While the feature is easy enough to integrate, many developers find it difficult in testing it and making sure that it does not break. Below you will find code samples that will help you to implement the In-App update and test it within minutes.

In-App Update basically works on the principle — It checks if there is any higher VERSION of the app available on the play store by checking the VERSION CODE of the build. So for the In-App update to detect if there is an update available, make sure you keep the version code higher than the previous version.

Also, there are two different In-App update modes — Flexible & Immediate. Flexible downloads the update in the background which allows the users to use the app while the update is getting downloaded whereas Immediate blocks any action which can be performed on the application and the users have to wait till the update gets downloaded. By reading this, it is clear enough that Flexible updates might provide a very good User Experience but in some cases, we would have to go with an Immediate update too. Both the mode will require the users to restart the app to update the app after it is downloaded.

Implementation

Let’s dive into the code —

  1. For the InApp Update feature, we would have to include the PlayCore dependency in our app —
//Play Core (For In-App Update API) implementation ‘com.google.android.play:core:1.6.5’

2. Now, we would declare some variables and initialize them that we will be using —

3. It might happen that an update was getting downloaded and was completed but the user closed the app beforehand thus we were not notified to restart or apply the changes. We can do so by using the InstallStateUpdatedListener(). In the initialization, we will check for DOWNLOADED update —

4. Now ideally have a check which will check for if the latest version is available and has been released. To do so, you can use Firebase RemoteConfig or a simple API call to your backend. Once done, we will create a task and attach the corresponding listeners —

5. We can write the startInAppUpdate() function as —

6. Now, we will set a listener for the In-App Update which we started —

7. Once done, we will show a popup to the user and ask them to restart the app —

Congratulations! We have successfully implemented the InApp flow. But we do have to test it right?!

Source — Android Developers

Testing

For testing, there are a few steps that will help you test the new feature easily —

  1. We will be using the Internal App Sharing feature of Google Play to test our app. For that, first set it up by following the instructions here. Opt-in for App Signing.
  2. Build an AAB or APK and upload it to the Internal App Sharing.
  3. Use the link provided and download the app onto your device.
  4. Now, increase the VERSION CODE for the next build, generate and share it again on the internal App Sharing as mentioned in Step 2.
  5. Once done, open the new link on your device and you will see an UPDATE button on the Play Store listing page. Do not update the app from there.
  6. Open the application and you will see the popup we wrote asking the users to update the app. Click on Update and you will see the InApp flow which will begin and download and install the newer update onto your device. (For testing, try calling the startInAppUpdate() directly with true or false to facilitate Flexible or Immediate update and verify the flow.
Source — Android Developers

We are done. This API by Google is too easy to implement and takes the work off the developer in trying to get the user onto the new version seamlessly. Let’s hope Google keeps bringing in new APIs which will make it easier and more seamless for the users to use the app.


I implemented this feature in one of the applications published by Dwarsoft – One News.

Leave a Reply

Your email address will not be published. Required fields are marked *