Comprehensive guide to implementing & testing In-App Updates in Android App (2024)

Ritesh Gupta
3 min readApr 13, 2024

--

Hello guys 👋 I am Ritesh. I am writing this article because It is quite possible that many Android Devs like us faced a great hustle for implementing In-App updates.

If you develop an entire app update feature from scratch, it will be quite messy & require a lot of work:

  • creating APIs for updates
  • Showing prompt to the user
  • redirecting the user to PlayStore (And it doesn’t guarantee if the user has updated the app or not)

Too lengthy right? But don't worry engineers of Android made a simple, robust & easy to implement SDK for this. Yes, you guessed right It is nothing but In-App Updates SDK.

Stick to this article I will explain every step from coding to testing the In-App Updates.

So, without any further delay… (Let’s make some more delay..) Ohh sorry 😂

Before starting the implementation steps, let’s first understand the types of In-App Updates:

1. Flexible updates

  • It provides background download and installation.
  • Update Installation is optional.
  • You should use Flexible Updates if the update is neither critical nor from the core functionality of your app.
Credit: Official Android Documentation

2. Immediate updates

  • It provides a full-screen UI for the installation.
  • Update Installation is mandatory.
  • It requires an app restart after installing the update (Don’t worry android handles this also)
  • You should use Immediate Updates if the update is critical.
Credit: Official Android Documentation

Now let’s start the implementation step by step.

Step 1: Add the following lines in build.gradle.kts file & sync project with Gradle.

dependencies {
implementation("com.google.android.play:app-update-ktx:2.1.0")
}

Step 2: Go to the desired Activity where you want to show the app update prompt. Declare and initialize a global variable for 👇

private val appUpdateManager: AppUpdateManager by lazy { AppUpdateManagerFactory.create(this) }

Step 3: Now add the below code globally in the desired Activity. Using this we will be able to get results from the prompt. (We can use onActivityResult here, but it is deprecated so we won’t use that).

private val updateLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
if (it?.data == null) return@registerForActivityResult
if (it.resultCode == UPDATE_REQUEST_CODE) {
// Download started
if (it.resultCode != Activity.RESULT_OK) {
// Download failed
}
}
}

Step 4: Create a function like this 👇

private fun checkUpdate() {
val appUpdateInfoTask = appUpdateManager?.appUpdateInfo
appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
updateLauncher,
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())
}
}
}

First, Here we are getting the info if the update is available or not. For this, we have called appUpdateInfo and attached addOnSuccessListener

Second, In addOnSuccessListener scope, we are getting appUpdateInfo.

Third, we are calling startUpdateFlowForResult function on appUpdateManager, to start the app update process.

Fourth, The most important point to notice is if you pass AppUpdate.IMMEDIATE it will start the process for Immediate Update, and the same for AppUpdate.FLEXIBLE.

AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())

Step 5: Just call the checkUpdate() function in onCreate scope & just run the app.

Yeah, you guessed right. You have completed the implementation part. Now let’s do the testing for the same.

Testing In-App Updates

For testing, you just need to follow these steps:

Step 1: Go to the play console and push an app to internal testing.

Step 2: Again push another build with one greater version of the previous one.

Step 3: Copy the shareable link of the build with the lower version & install it on the device. You will see the prompt!

Congratulations, You did it! 🎊

If you like this article, Please hit the 👏 and follow me for more such content.

--

--