steps to implement In app update :
-
add dependecy in build.gradle(Module :app)
// dependency for Playstore In-App Update: implementation("com.google.android.play:app-update:2.1.0") implementation("com.google.android.play:app-update-ktx:2.1.0")
-
mainactivity code:
package com.rajapps.inappupdate
import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import com.google.android.play.core.appupdate.AppUpdateManager import com.google.android.play.core.appupdate.AppUpdateManagerFactory import com.google.android.play.core.install.InstallStateUpdatedListener import com.google.android.play.core.install.model.AppUpdateType import com.google.android.play.core.install.model.InstallStatus import com.google.android.play.core.install.model.UpdateAvailability import com.google.android.play.core.ktx.isFlexibleUpdateAllowed import com.google.android.play.core.ktx.isImmediateUpdateAllowed import kotlinx.coroutines.delay import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
class MainActivity : AppCompatActivity() {
private lateinit var appUpdateManager: AppUpdateManager // in app update
private val updateType = AppUpdateType.IMMEDIATE // in app update
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//in app update
appUpdateManager = AppUpdateManagerFactory.create(applicationContext)
if (updateType == AppUpdateType.FLEXIBLE){
appUpdateManager.registerListener(installStateUpdatedListener)
}
checkForAppUpdates()
}
private val installStateUpdatedListener = InstallStateUpdatedListener { state ->
if (state.installStatus() == InstallStatus.DOWNLOADED) {
Toast.makeText(
applicationContext,
"Download successful, Restarting app in 5 seconds",
Toast.LENGTH_LONG
).show()
appUpdateManager.completeUpdate()
}
}
//in app update
private fun checkForAppUpdates() {
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
val isUpdateAvailable = info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
val isUpdateAllowed = when (updateType) {
AppUpdateType.FLEXIBLE -> info.isFlexibleUpdateAllowed
AppUpdateType.IMMEDIATE -> info.isImmediateUpdateAllowed
else -> false
}
if (isUpdateAvailable && isUpdateAllowed) {
appUpdateManager.startUpdateFlowForResult(
info,
updateType,
this,
123
)
}
}
}
//in app update
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 123) {
if (resultCode != RESULT_OK) {
// Handle update failure
println("something went wrong ...")
}
}
}
//in app update
override fun onResume() {
super.onResume()
if (updateType == AppUpdateType.IMMEDIATE) {
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
if (info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
appUpdateManager.startUpdateFlowForResult(
info,
updateType,
this,
123
)
}
}
}
}
override fun onDestroy() {
super.onDestroy()
if (updateType == AppUpdateType.FLEXIBLE){
appUpdateManager.unregisterListener(installStateUpdatedListener)
}
}
}