androidin-app-updategoogle-play-core

The InAppUpdate dialog pops up every time, even if I click the update button


I have an app published in the play store with versionCode 3, in a new version, I want to implement the in app update functionality, so in build.gradle I add:

implementation 'com.google.android.play:core:1.8.2'

And in the main activity:

public class MainActivity extends BaseActivity {
    //...
    private AppUpdateManager mAppUpdateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        
        //....
        checkForUpdate();
    }

    /**
     * check for update
     */
    private void checkForUpdate() {
        // Creates instance of the manager.
        mAppUpdateManager = AppUpdateManagerFactory.create(this);
        mAppUpdateManager.registerListener(state -> {
            if (state.installStatus() == InstallStatus.DOWNLOADED) {
                // After the update is downloaded, show a notification
                // and request user confirmation to restart the app.
                popupSnackbarForCompleteUpdate();
            }
        });

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = mAppUpdateManager.getAppUpdateInfo();

        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    //&& appUpdateInfo.clientVersionStalenessDays() != null
                    //&& appUpdateInfo.clientVersionStalenessDays() >= DAYS_FOR_FLEXIBLE_UPDATE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {

                // Request the update.
                try {
                    mAppUpdateManager.startUpdateFlowForResult(
                            // Pass the intent that is returned by 'getAppUpdateInfo()'.
                            appUpdateInfo,
                            // Or 'AppUpdateType.IMMEDIATE' 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);
                } catch (IntentSender.SendIntentException e) {
                    Toast.makeText(this, R.string.update_failed, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

        /**
     * Displays the snackbar notification and call to action.
     */
    private void popupSnackbarForCompleteUpdate() {
        final Snackbar snackbar =
                Snackbar.make(
                        findViewById(R.id.activity_main_layout),
                        R.string.an_update_has_been_just_downloaded,
                        Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction(R.string.install, view -> mAppUpdateManager.completeUpdate());
        snackbar.setActionTextColor(
                ContextCompat.getColor(this, R.color.color_snackbar_action_text_color));
        snackbar.show();
    }
}

To be able to test this functionality, I change the versionCode to a lower number than the one published in the Playstore (2 in this case and the app published with 3)When I run my app, it shows me the dialog to update the app and when I click the update button the update starts and installs without any errors, but when the app restarts, it shows me the update dialog again and the app seems to be not updated, I don't know if this is a bug in my app or i should upload the app to play store to be working correctly.

I am using a app bundle (.aab)


Solution

  • You need a Signed .apk for In App Update. Apps in debug mode will show you update dialog & even download new version, but new app wont be installed/updated.