Atulmaharaj
Atulmaharaj

Reputation: 63

Display Dialog only on App Upgrade

I want to verify a scenario where-in the app is upgraded to latest version and then perform an action. Consider the version of the app is 2.4 on the user's phone. He/She upgrades to the latest version (2.5) from Play Store. I will check if it is the first run after upgrade and show a dialog. (I'm using the solution given here: Check if application is on its first run to verify first launch)

However, how do I verify such scenario before pushing the update to Play Store. What I've tried is:

  1. Install the App from store manually
  2. Run the latest version in debug / release mode from Android Studio.

But when I'm doing the above, I get the following error: INSTALL_FAILED_UPDATE_INCOMPATIBLE

I've also tried installing the app from an older apk that I have, but get the same error while verifying the upgrade.

Is there any other means by which I can verify that the dialog appears only when the user upgrades the app to the latest version ?

Upvotes: 1

Views: 808

Answers (2)

Yazan
Yazan

Reputation: 6082

i think you are getting this error because the signature is not the same between store version (signed by your release key) and the studio-run version which is signed by the debug key (exist in android data folder)

so this is regarding the error, you have to build the APK as if you will upload it to the store, and sign it with your release keystore... then install it on the device (using adb install or adb push then install from mobile, or copy apk to mobile memory then install from mobile ... etc) maybe?

now regarding the second part, detect 1st run of app/ detect 1st run after app update, you can use SharedPreferences as advised by that answer. but this will not be sufficient, as it will not be linked/effected by app version, and thats what you need to detect 1st run after upgrade.

what you need to do is to get app version-code (which is integer) that you specify on manifest file android:versionCode, and you have to increase it every time you upload a version to the PlayStore.

this code will do what you need (you can use it in the activity/fragment that you wish to show the dialog on)

//this code gets current version-code (after upgrade it will show new versionCode)
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
int versionCode = info.versionCode;
SharedPreferences prefs = this.getSharedPreferences("yourAppName", Context.MODE_PRIVATE);
if(prefs.getInt("last_version_code", -1) > 0){
    if(prefs.getInt("last_version_code", -1) != versionCode){
        //save current versionCode: 1st-run after upgrade
        prefs.edit().putInt("last_version_code", versionCode).commit();

        //put show the dialog code here...
    } //no need for else, because app version did not change...
}else{
    //save current versionCode for 1st-run ever
    prefs.edit().putInt("last_version_code", versionCode).commit();
}

prefs.getInt("last_version_code", -1) this will get last saved value in the preferences for key last_version_code and in case no value (1st run ever) it will return -1 as default.

the if checks the value returned from preferences > 0 (not first run) and it does NOT equal the one in manifest. that will mean (UPGRADED)

in case of ==-1 you just write the versionCode so next time you try to get that value it will return a number (last version code saved)

if you want to use it as a method (may be called in multiple places) create a method to return true/false if app was updated

public static boolean appWasUpdated(Context context){
    //this code gets current version-code (after upgrade it will show new versionCode)
    PackageManager manager = context.getPackageManager();
    PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
    int versionCode = info.versionCode;
    SharedPreferences prefs = context.getSharedPreferences("yourAppName", Context.MODE_PRIVATE);
    if(prefs.getInt("last_version_code", -1) > 0){
        if(prefs.getInt("last_version_code", -1) != versionCode){
            //save current versionCode: 1st-run after upgrade
            prefs.edit().putInt("last_version_code", versionCode).commit();

            return true;
        } //no need for else, because app version did not change...
    }else{
        //save current versionCode for 1st-run ever
        prefs.edit().putInt("last_version_code", versionCode).commit();
    }
    return false;
}

and just call this method from activity/fragment

if(appWasUpdated(this)){
    showMyDialog();
}

Upvotes: 4

Tamás Kozmér
Tamás Kozmér

Reputation: 546

  1. Increase your APK's versionCode. It should be higher than the versionCode of the APK in the Store.

  2. Sign the APK with the same keystore and make a release build.

  3. Download the old APK from the Store.

  4. Push the new release APK to your device. adb push myapp.apk /sdcard/0/. adb install won't work in this scenario.

  5. Install the new APK from the phone. It will update the existing app.

Upvotes: 0

Related Questions