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:
Download the APK to the storage. All apps' APK files get downloaded to the same location.
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.
Create a data dir for the new app. This is initially empty.
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:
Download the new APK file. This is a new file in the same location as all the other APK files.
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.
Stop the old version of the app cleanly.
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).
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.