Its not yet clear to me what happens when an app gets updated.

Is the new updated app simply being "installed over" the old one maintaining data and cache from the old one? What actually happens when an app is updated?

In searching internet1, and SE, i found no useful information (I am not asking about how to update using googleplay.

PS: In the comments AndrewT and Firelord actually gave me useful pointers and I feel the informations I was looking for center around the workings of PackageManager and PackageInstaller.

1: See e.g. "how does android update apps", how does android app-update work, android app-update process explained

  • I don't think that last statement is well-adhered. Many apps distributed through Google Play have other avenues of distribution. Just think 'Monument Valley', which their own developers say is highly pirated to the tune of 90%. If you just Google it, you'll find dozens of distribution points. Google query – wbogacz Mar 22 '16 at 16:30
  • It was just an example but I do not want that mentioning it would bog down the answers, so I edited that part out. My question is not about google or other avenues of distribution, it's limited to the update process proper. Wbogacz comment refers to me mentioning that "apps distributed using GooglePlay cannot be updated in any other way than through GooglePlay according to the Developer Policies" in the question before i edited it – Erik vanDoren Mar 22 '16 at 16:57
  • 2
    pm install -r FILE. Whatever that command would do is what technically happens in the background during an app update. – Firelord Mar 22 '16 at 17:02
  • Can you provide link to the Developer Policies? I think it's the case for developer to not update their apps outside from Play Store (e.g. push/forced update from other sites.. probably for security reason), because AFAIK, I could update any apps using their APK (as long as it's using the same certificate) – Andrew T. Mar 22 '16 at 17:03
  • 3
    I found the article when searching that statement, but never mind if it's unrelated. I'm not sure how to limit the scope on answer this question: yes, an update will overwrite the "binary" of the app (APK file) while retaining the data/cache. However, there are requirements before it can be updated, such as "app version must be higher/same than current installed", "must use the same certificate", etc. More than that, I can't explain the detail. – Andrew T. Mar 22 '16 at 17:25

It's important to note that Android apps don't get installed in the same way as on Windows or on GNU/Linux systems. The app runs entirely from the APK file. Installing an app for the first time includes the following steps:

  1. Download the APK to the storage. All apps' APK files get downloaded to the same location.

  2. Create a user ID for the new app. Each app in Android runs in its own Linux user ID. This is for security, and is how the permissions system works.

  3. Create a data dir for the new app. This is initially empty.

  4. Add the app to the phone's database of installed apps. This database includes the name (package ID) of the app, the path to the APK file, the user ID, amongst other things.

That's all it takes. So after that, it's pretty easy to see how to upgrade it:

  1. Download the new APK file. This is a new file in the same location as all the other APK files.

  2. Check that the old APK can be upgraded to the new APK. It checks that they were both signed by the same key, to avoid data theft, and that the new APK is not an older version, and a few other settings from the app's manifsest. If any of the checks fail so the old APK can't be upgraded to the new APK, the upgrade stops with an error at this point.

  3. Stop the old version of the app cleanly.

  4. Check for changes to the app (such as new permissions) and update the app's user ID accordingly. Update the database of installed apps to point to the new APK file (and with other data about the new app).

  5. Delete the old APK file.

The upgrade process doesn't even have to look at the data directory. It's up to the app to change its data if the new version uses a different format or whatever. (This also means that it's up to the app what old versions can be updated cleanly without losing data.)

The process works the same way whether you're upgrading the app through Google Play, or some other app store, or by clicking on the APK file, or using adb install. Whichever front-end you use, it goes through the same package manager. The only difference is that if you use an app store, it probably also keeps its own record of what versions are installed, both to help the app developer understand their user base, and so it can check for future updates.

  • "It's up to the app to change its data if the new version uses a different format or whatever" Does that mean that if I have, lets say, version 1 and intend to upgrade directly to version 7 the upgrade might fail if the data of version1 are retained but there has been a change in format in version3 and the newest version has no instructions to change the data format? Or any version after the change will likely be able to upgrade directly from a very old one? – Erik vanDoren Mar 23 '16 at 15:23
  • 3
    @ErikvanDoren It's up to the app author. It's possible that if you skip a lot of versions, you might hit a case the author hasn't tested, and the app then won't work properly unless you clear its data (or it might clear the data itself, if it's especially badly written). I'd expect most app authors to handle it properly, though. – Dan Hulme Mar 23 '16 at 15:36
  • Important detail missing: signature check :) On update, this would be the step before stopping the old version. If signatures don't match, the update process would be cancelled at this point. – Izzy Mar 24 '16 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.