In-App Update for Android —in Kotlin

Saqib Ahmed
2 min readApr 21, 2021

When a users keep your app up to date on their devices, they can try new features, as well as benefit from performance improvements and bug fixes. In-app updates is a Play Core library feature that prompts active users to update your app.

The in-app updates feature is supported on devices running Android 5.0 (API level 21). There are basically two type of updates.

1. Flexible Update.
2. Immediate Update.

Flexible updates :

Flexible updates provide background download and installation with graceful state monitoring. This UX flow is appropriate when it’s acceptable for the user to use the app while downloading the update. For example, you might want to encourage users to try a new feature that’s not critical to the core functionality of your app.

Immediate updates

Immediate updates are full screen UX flows that require the user to update and restart the app in order to continue using it. This UX flow is best for cases where an update is critical to the core functionality of your app. After a user accepts an immediate update, Google Play handles the update installation and app restart.

Here are the code for in-app update :

Add the dependencies :

implementation ‘com.google.android.play:core-ktx:1.8.1’

Enter the variable in the main activity.

class MainActivity : AppCompatActivity() {


private val MY_REQUEST_CODE = 101
private var appUpdateManager: AppUpdateManager? = null

In the onCreate() method call the getCurrentVersion function. After that paste the below code in that function as below.

private fun getCurrentVersion(context: Context) {
appUpdateManager = AppUpdateManagerFactory.create(context)
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager!!.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener {
appUpdateInfo: AppUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
AppUpdateType.FLEXIBLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
// Request the update.
try {
appUpdateManager!!.startUpdateFlowForResult(
// Pass the intent that is returned by ‘getAppUpdateInfo()’.appUpdateInfo,// Or ‘AppUpdateType.FLEXIBLE’ for flexible updates.AppUpdateType.IMMEDIATE,// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE
)
} catch (e: IntentSender.SendIntentException) {
e.printStackTrace()
}
}
}
}

That it, The In -app update will automatically update the popup when there is an update in a play store. Here we implement a Immediate update, for the Flexible update, change from IMMEDIATE to FLEXIBLE.

appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)
) {
// Request the update.
try {
appUpdateManager!!.startUpdateFlowForResult(
// Pass the intent that is returned by ‘getAppUpdateInfo()’.appUpdateInfo,// Or ‘AppUpdateType.FLEXIBLE’ for flexible updates.AppUpdateType.FLEXIBLE,// The current activity making the update request.
this,
// Include a request code to later monitor this update request.
MY_REQUEST_CODE
)

Thanks for the reading, hope you like it.

--

--

Saqib Ahmed

I'm basically a mobile developer and became a self-time freelance writer. I spend most of my time with tech related stuff. I’m sharing my learning with you!