In the Play In-App Update library, how do flexible and immediate updates differ, and how do updatePriority and clientVersionStalenessDays help you choose between them?
answer
- keep using the app vs blocked
- who restarts the app
- priority 0 to 5, set per release
- days since Play offered it
basics
~20 sA flexible update downloads in the background while the user keeps working, and the app calls completeUpdate to install it. An immediate update blocks the app until Play installs and restarts it. updatePriority (0-5) and clientVersionStalenessDays decide which to request.
solid answer
~40 sWith `AppUpdateManagerFactory.create(context)` you read `appUpdateInfo`, check `updateAvailability()` and `isUpdateTypeAllowed(...)`, then call `startUpdateFlowForResult` with an activity-result launcher and `AppUpdateOptions.newBuilder(type)`. **Flexible**: after consent the download runs in the background; you watch an `InstallStateUpdatedListener` and, at `InstallStatus.DOWNLOADED`, ask the user to restart and call `completeUpdate()`, because Play won't restart the app for you. **Immediate**: a full-screen flow; Play installs and restarts the app. To choose, `updatePriority()` returns 0-5, set per release through the Play Developer API's `inAppUpdatePriority` and fixed once rolled out, while `clientVersionStalenessDays()` counts days since the update reached the Store. High priority means immediate, a stale medium one means flexible. On resume, finish a `DOWNLOADED` flexible update or resume a stalled immediate one.
code
kotlin · 35 linesclass UpdateGate(private val activity: ComponentActivity) {
private val appUpdateManager = AppUpdateManagerFactory.create(activity)
// Construct this class in onCreate so the launcher is registered before STARTED.
private val updateLauncher = activity.registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult()
) { result ->
if (result.resultCode != Activity.RESULT_OK) {
// Declined or failed: ask again on a later session, with a fresh AppUpdateInfo.
}
}
private val installListener = InstallStateUpdatedListener { state ->
if (state.installStatus() == InstallStatus.DOWNLOADED) showRestartPrompt()
}
fun checkForUpdate() {
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
if (info.updateAvailability() != UpdateAvailability.UPDATE_AVAILABLE) return@addOnSuccessListener
val type = when {
info.updatePriority() >= 4 && info.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE) ->
AppUpdateType.IMMEDIATE
(info.clientVersionStalenessDays() ?: -1) >= 3 && info.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE) ->
AppUpdateType.FLEXIBLE
else -> return@addOnSuccessListener
}
if (type == AppUpdateType.FLEXIBLE) appUpdateManager.registerListener(installListener)
appUpdateManager.startUpdateFlowForResult(info, updateLauncher, AppUpdateOptions.newBuilder(type).build())
}
}
private fun showRestartPrompt() {
// Show a snackbar whose action calls appUpdateManager.completeUpdate().
}
}go deeper
Recall the two flows: flexible keeps the app usable while downloading, immediate blocks the app until Play installs and restarts it.
Explain the check-then-start sequence, InstallStateUpdatedListener and completeUpdate for flexible, and how updatePriority and clientVersionStalenessDays drive the choice.
Handle the stalls on resume, DOWNLOADED for flexible and DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS for immediate, and test through internal app sharing with matching signing keys.
Own the update policy: which releases earn a high priority, how long before a prompt, and how to avoid nagging users into uninstalling.
## What in-app updates are **In-app updates** let an app prompt an active user to install a newer version from Google Play without leaving for the Store listing. They come from the **Play In-App Update Library** (`com.google.android.play:app-update:2.1.0`, plus `app-update-ktx` for Kotlin extensions). It is part of the **Google Play Core libraries**, which are the app's runtime interface to the Google Play Store app. The feature works on Android 5.0 (API level 21) and higher, only on phones, tablets and ChromeOS devices, and not for apps that use APK expansion (`.obb`) files. ## Checking what is available 1. Create the manager: `AppUpdateManagerFactory.create(context)`. 2. Read `appUpdateManager.appUpdateInfo`, a `Task<AppUpdateInfo>`. 3. Check `updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE` and `isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)` or `isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)`. 4. Start the flow with `startUpdateFlowForResult(appUpdateInfo, activityResultLauncher, AppUpdateOptions.newBuilder(type).build())`. The launcher is registered with `ActivityResultContracts.StartIntentSenderForResult`. Each `AppUpdateInfo` can start an update **only once**; to retry, request a fresh one. The launcher's result is `RESULT_OK` when the user accepted, `RESULT_CANCELED` when they declined, or `ActivityResult.RESULT_IN_APP_UPDATE_FAILED` when something else stopped the consent or the update. ## Flexible versus immediate | | Flexible | Immediate | |---|---|---| | User experience | Consent dialog, then a background download while the app stays usable | Full-screen flow; the user must update and restart to continue | | Who restarts the app | You call `completeUpdate()` after `InstallStatus.DOWNLOADED` | Google Play installs and restarts the app | | Good for | A new feature or performance work | A change critical to core functionality, such as a security fix | | Stalled state to check on resume | `installStatus() == InstallStatus.DOWNLOADED` | `UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS` | For a **flexible** update, register an `InstallStateUpdatedListener` before starting, show progress with `bytesDownloaded()` and `totalBytesToDownload()`, and when the status reaches `DOWNLOADED` ask the user to restart. Google Play does **not** restart the app for you. Calling `completeUpdate()` in the foreground shows a full-screen UI and restarts into the main activity; calling it in the background installs silently. Unregister the listener when you no longer need it. For an **immediate** update, Play shows progress over your UI. If the user leaves or kills the app, the update should continue in the background without asking again. ## Choosing with priority and staleness Two signals on `AppUpdateInfo` let you choose the flow: - **`updatePriority()`**: an integer from 0 (default) to 5. You set it per release with the `inAppUpdatePriority` field under `Edits.tracks.releases` in the Google Play Developer API. It can only be set when the release rolls out and cannot be changed later. The value the app sees covers every version code between the installed and the latest version, **regardless of track**, so a priority 5 build on an internal track raises it for production users who skip over it. - **`clientVersionStalenessDays()`**: the number of days since the update became available on the Play Store. It can be `null`, so give it a default. A common policy: low priority means no prompt; medium priority, or a few stale days, means flexible; high priority (for example 4 or more) means immediate. ## Options worth knowing - `AppUpdateOptions.newBuilder(type)` carries the flow type, and `setAllowAssetPackDeletion(true)` lets the update clear asset packs when storage is low. It is `false` by default; Google advises checking first whether the update is allowed without it. - Request updates only for changes that matter. A prompt on every launch for a cosmetic release trains users to dismiss it. - The listener is an ordinary object: register it before starting a flexible flow and unregister it when the screen goes away, or it outlives the screen. ## Stalls, testing and traps - **Check at every entry point.** On `onResume`, a flexible update sitting in `DOWNLOADED` still occupies storage until the user installs it; prompt again. An immediate update left in `DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS` should be resumed by calling `startUpdateFlowForResult` with `AppUpdateType.IMMEDIATE` again. - **Testing** uses Play's internal app sharing: install from an internal sharing link, upload a higher version code, open that link without installing, then launch the app. The account must own the app, the application ID and signing key must match, and `inAppUpdatePriority` is not supported there. - **Don't nag.** Google asks you to request updates only for changes that matter to core functionality. - **Reusing one `AppUpdateInfo`** after a failure does nothing; fetch a new one.
- A user accepted a flexible update, then left the app before restarting; what happens and what should the app do?The download can finish and sit in `InstallStatus.DOWNLOADED`, taking device storage until it is installed. Google Play does not restart the app for a flexible update. On every entry point, such as `onResume`, read `appUpdateInfo` and, if `installStatus()` is `DOWNLOADED`, prompt the user and call `completeUpdate()`.
- You released a priority 5 build only to the internal track; why do production users skipping it see priority 5?`updatePriority()` takes into account the `inAppUpdatePriority` of every version code between the installed version and the latest available one, irrespective of track. A production user jumping from version 1 to version 3 inherits the priority 5 of version 2, even though version 2 shipped only on the internal track.
- Google Play restarts the app automatically when a flexible update finishes downloading.
- updatePriority can be raised in the Play Developer API after the release has rolled out.
- One AppUpdateInfo can be reused to retry the update flow after a failure.
- In-app updates are served by the Google Play services APK rather than the Play Store app.
- An immediate update stops forever if the user closes the app during the download.
Assumes the Play In-App Update library 2.1.0 (com.google.android.play:app-update), where startUpdateFlowForResult takes an ActivityResultLauncher and AppUpdateOptions.