Get App version in Play Store

Asked

Viewed 48 times

-2

I’m trying to get the APP version from the Play Store to compare with the installed version and be able to force the update if the device is lower than the Store, but I’m not getting the Store version. Use eclipse. How can I do this?

1 answer

1

You can use what is indicated in the documentation itself https://developer.android.com/guide/playcore/in-app-updates

To install you can import the Play Core library to the Android project as a dependency of Gradle, in your build.grandle:

dependencies {
    implementation 'com.google.android.play:core:1.9.1'

    implementation 'com.google.android.play:core-ktx:1.8.1'
    ...
}

The library Play Core KTX is optional and provides versions of Kotlin coroutines for asynchronous method calls in the normal Play Core library, as well as other useful extensions using the most idiomatic Kotlin Apis, so using Java does not require installing this library.

In Java:

AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);

Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
    // Faz o que deseja aqui, inclusive forçar iniciar o update
    }
});

But you can use it too AppUpdateType.FLEXIBLE, which depends on the need.

In Kotlin will be:

val appUpdateManager = AppUpdateManagerFactory.create(context)
val appUpdateInfoTask = appUpdateManager.appUpdateInfo

appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
    ) {
        // Faz o que deseja aqui, inclusive forçar iniciar o update
    }
}

You can display a message within the IF, or an alert, or close the app, or open the playstore, or directly use the appUpdateManager.startUpdateFlowForResult(), also as it is said in the documentation https://developer.android.com/guide/playcore/in-app-updates#start_update, example in Java:

if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
        ) {

    appUpdateManager.startUpdateFlowForResult(
        appUpdateInfo,
        AppUpdateType.IMMEDIATE,
        this,
        MEU_CODIGO_REQUISICAO);
}

In Kotlin:

appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
    ) {
    appUpdateManager.startUpdateFlowForResult(
        appUpdateInfo,
        AppUpdateType.IMMEDIATE,
        this,
        MEU_CODIGO_REQUISICAO)
}

Note that the MEU_CODIGO_REQUISICAO is not a "native" code, it is a value you can set to be able to use a callback to know if the UPDATE failed, which you can add in your Activity to monitor based on that code (which may be a constant), in Java the value of the request code can look like this:

private static final int MEU_CODIGO_REQUISICAO = 90002;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == MEU_CODIGO_REQUISICAO && resultCode != RESULT_OK) {
    // Se falhou e é sobre o seu update você pode fazer o que desejar, por exemplo um "alert" que diga que isso falhou
  }
}

In Kotlin the constant can be something like:

// Um valor qualquer
const val MEU_CODIGO_REQUISICAO = 90002

And the callback:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    if (requestCode == MEU_CODIGO_REQUISICAO && resultCode != RESULT_OK) {
        // Se falhou e é sobre o seu update
    }
}
  • My problem is that I can’t download the library with.google.android.play for Eclipse.

  • Hello @Trapé. Reply edited.

  • @Was Trapé able to download the library using Gradle as I showed in the reply? If you don’t warn me, so I can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.