-
Notifications
You must be signed in to change notification settings - Fork 6
How Android updates an app?
- Download Android Market application, decompile it
- Android source code, 4.1.1 for the best as we worked on this version
- Once a new version of an app is downloaded, Android Market will uninstall the old version and re-install the new one. This is done by calling .deletePackage and .installPackage of android.content.pm.PackageManager. However, these two functions are not visible normally according to [this stackoverflow thread](http://stackoverflow.com/questions/6813322/install-uninstall-apks-programmatically-packagemanager-vs-intents a thread on stack overflow).
They (Android market) use a richer version of the PackageManager.
This can bee seen by downloading the Android source code from the Android Git repository.
Below are the two hidden methods that corresponds to the Intent approach.
Unfortunately they are not available to external developers. But perhaps they will be in the future?
So in order to retrieve these two function, we go to the android source code (for private SDK)
- deletePackage needs a packageName and an observer which can be notified once the package deletion is completed. It is implemented in IPackageManager.java. This file is generated from .aidl file, which contains mainly binder call into system code. IPackageManager contains Stub and Proxy, which is the standard notions of binder protocol. The stub resides in the caller code (private SDK) and the Proxy stays in the calle code (Android service code). You can check out this slides for details about the binder mechanism.
- binder call to the service code of PackageManager
frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
start from the .deletePackage, we have
.deletePackage
in new thread
.deletePackageX
failed if this package has active device admin
.deletePackageLI
notify all listeners
gc
notify the observer
So the .deletePackageLI does the actual work
remove data first
if isSystemApp(...)
deleteSystemPackageLI(...)
else
killApplication(...)
deleteInstalledPackageLI(...)
end if
So to delete an application it will first be shut down (as expected). killApplication calls ActivityManager.killApplicationWithUid.
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
killApplicationWithUid posts an async message to with "KILL_APPLICATION_MSG" as header to kill the application. Search for the receiver for this message, forceStopPackageLocked() does the dirty job. killPackageProcessesLocked continues. This function will retrieve a list of processes associated with the package and then call removeProcessLocked to terminate them.
- removeProcessLocked checks the state of the process, waits for the deadlock if existed and then call Process.killProcess() to kill the process. killProcess() is nothing but sending a standard Linux "kill" signal to the process and terminate it.
Android Market will first close all the process of the package being updated, as observed. To close these process, Android Market makes a serie call and eventually nothing but a killProcess to do so. So we can tell that Android Market does nothing special (e.g., notify the app being killed). It is safe to kill a process on Android.