I'm having issues with in app updates. My procedure as to testing it is I release two versions of the app, with one that has a higher version code(in gradle). When I check my log statements, they say that there is no update available. However, I did install the app from the Play Store(using the internal testing track), and the play store does show an update button(so my version code, etc, should be right). I have also tried refreshing the cache on the PlayStore app and my own, and have tried rebuilding with several other version codes with an even wider gap, all to no help. Here is all of my code regarding in-app updates. I call the checkUpdate function in onCreate().
class MainActivity : AppCompatActivity() {
private lateinit var appUpdateManager: AppUpdateManager
private val MY_REQUEST_CODE = 17326
override fun onCreate(savedInstanceState: Bundle?) {
//Rest of Code
checkUpdate()
}
private fun checkUpdate() {
val listener =
InstallStateUpdatedListener { installState ->
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
Timber.tag("InstallDownloaded").d("InstallStatus success")
Log.d("InstallDownloaded", "Install Successful")
notifyUser()
}
}
appUpdateManager = AppUpdateManagerFactory.create(this)
appUpdateManager.registerListener(listener)
val appUpdateInfoTask: Task<AppUpdateInfo> = appUpdateManager.getAppUpdateInfo()
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
val data = "packageName :" + appUpdateInfo.packageName() + ", " +
"availableVersionCode :" + appUpdateInfo.availableVersionCode() + ", " +
"updateAvailability :" + appUpdateInfo.updateAvailability() + ", " +
"installStatus :" + appUpdateInfo.installStatus() + ", "
Timber.tag("appUpdateInfo :").e("%s", data)
Log.d("UpdatesInfo", data)
Log.d("Updates", "UpdateAvailability: ${appUpdateInfo.updateAvailability()}")
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
) {
requestUpdate(appUpdateInfo)
Timber.tag("UpdateAvailable").d("update is there ")
Log.d("Updates", "Update is there")
} else if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
Timber.tag("Update").d("3")
notifyUser()
} else {
Timber.tag("NoUpdateAvailable").e("update is not there ")
Log.d("Updates", "Update is not there")
}
}
}
private fun requestUpdate(appUpdateInfo: AppUpdateInfo) {
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this@MainActivity,
MY_REQUEST_CODE
)
resume()
} catch (e: IntentSender.SendIntentException) {
e.printStackTrace()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == MY_REQUEST_CODE) {
when (resultCode) {
RESULT_OK -> if (resultCode != RESULT_OK) {
Timber.tag("RESULT_OK :").d("%s", resultCode)
Log.d("RESULT_OK: ", "%s: $resultCode")
}
RESULT_CANCELED -> if (resultCode != RESULT_CANCELED) {
Timber.tag("RESULT_CANCELED :").d("%s", resultCode)
Log.d("RESULT_CANCELED: ", "%s: $resultCode")
}
ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {}
}
}
}
override fun onDestroy() {
super.onDestroy()
try {
appUpdateManager.unregisterListener(this as InstallStateUpdatedListener)
} catch (e: RuntimeException) {
}
}
private fun notifyUser() {
val snackbar = Snackbar.make(
findViewById(R.id.parent),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE
)
snackbar.setAction(
"RESTART"
) { appUpdateManager.completeUpdate() }
snackbar.setActionTextColor(
resources.getColor(R.color.main_red)
)
snackbar.show()
}
private fun resume() {
appUpdateManager.appUpdateInfo
.addOnSuccessListener(OnSuccessListener<AppUpdateInfo> { appUpdateInfo ->
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
notifyUser()
Log.d("Update", "Successful")
}
})
}
}
No matter the gap between the two build's version codes, the app still does not update. I have updated both the version codes in the manifest and the gradle files. Even clearing all Google Play and my app data, did not help. This is my 6th-8th try to no avail. Any help is appreciated. Thank You.