Comprehensive Guide to Implementing & Testing In-App Updates in Android App (2024)
Hello everyone 👋 I’m Usman. I’m writing this article to help fellow Android developers who have struggled with implementing In-App updates.
Creating an entire app update feature from scratch can be quite challenging and time-consuming. You’d need to:
- Create APIs for updates
- Show a prompt to the user
- Redirect the user to the Play Store (without a guarantee that the user will actually update the app)
Sounds overwhelming, right? Fortunately, Android engineers have developed a simple, robust, and easy-to-implement solution: the In-App Updates SDK.
Stick with this article as I guide you through every step, from coding to testing In-App Updates.
But before we dive into the implementation, let’s first understand the types of In-App Updates:
- Flexible Updates
- Allows background download and installation.
- Update installation is optional.
- Use Flexible Updates if the update is not critical to your app’s core functionality.
- Immediate Updates
- Provides a full-screen UI for the installation.
- Update installation is mandatory.
- Requires an app restart after installing the update (handled by Android).
- Use Immediate Updates for critical updates.
Now, let’s start the implementation step by step.
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:
private val appUpdateManager: AppUpdateManager by lazy { AppUpdateManagerFactory.create(this) }
Step 3: Add the following code globally in the desired Activity to get results from the prompt (using onActivityResult
is deprecated, so we won't use it):
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, we check if an update is available using
appUpdateInfo
and attachaddOnSuccessListener
. - Second, within the
addOnSuccessListener
scope, we getappUpdateInfo
. - Third, we call
startUpdateFlowForResult
onappUpdateManager
to start the app update process. - Fourth, note that passing
AppUpdate.IMMEDIATE
initiates the Immediate Update process, and similarly forAppUpdate.FLEXIBLE
.
Step 5: Call the checkUpdate()
function in the onCreate
scope and run the app.
That’s it! You’ve completed the implementation part. Now, let’s move on to testing.
Testing In-App Updates
Follow these steps to test In-App Updates:
- Go to the Play Console and push an app to internal testing.
- Push another build with a version number greater than the previous one.
- Copy the shareable link of the build with the lower version and install it on the device. You will see the prompt!
Congratulations, you’ve done it! 🎊
If you liked this article, please hit the đź‘Ź and follow me for more content like this.