In this post, I will try to explain what is Android PackageManagerService, what is its importance, what is a PackageManager al-together, what is installer, and what is installd?
We used to install, uninstall multiple numbers of APKs in our Android device, but have you thought how the installation process happens? How the apps get updated when any new update comes? To which directory, your application gets installed. Well, to answer these questions, Android is using a SystemService called PackageManagerService, and its associates. The associates includs PackageInstaller, installd, Installer.java, etc.
What is Android PackageManagerService?
Android PackageManagerService is a System service that comes into picture when you perform any of the activities such as install a package, uninstall a packge, update a packge, etc. It is started by SystemServer as a part of bootStrapServices. BootStrapServices are nothing but the number of critical services that are required to fully function the Android.
It is the Android PackageManagerService responsibility to install the package on your device. It also allocates an ID and as well as space to the package. The space, where the application data to be stored.
If you see the bootStrapServices(), there is a sequence in which the services are getting started
So, do we need a sequence like this? Can we start the services independent of the sequence?
The answer is Yes, there is a need to have a sequence like this. I will talk about this later.
How Android PackageManagerService install apk at boot-time?
Do you know how will the PackageManagerService comes to know, which all applications need to be installed and where it needs to be installed at boot-time?
It’s simple, it scans all the directories. When PMS is started, it calls a method
scanDirTracedLI() which starts the scan of directory provided in its argument.
scanDirTracedLI() uses ParallelPackageParser class to parse the package when it is found. For example, if you have defined
LOCAL_PRIVILEGED_MODULE := true in your application’s Android.mk file, then ParallelPackageParser will parse this information and place the apk in
/system/priv-app/ dir. Once all the files are scanned,
scanDirLI() method calls
scanChildPackageLI() method will call
addForinitLI() method for lock, then it adds the package and updates the package information in packages.xml.
If you look at the SystemServer on how PackageManagerService is started.
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
You will notice that PackageManagerService requires, the installer service as one of its argument. Now at this point, you will get the answer to the question of why the sequence is required? PMS uses installer, DisplayMetrics, and other components on its execution. So, these services need to be started first.
Android PackageManagerService during package installation
Now, whenever you try to install any package, you will get a popup asking whether you want to install the package or not. This is the case where the PackageInstaller comes into picture.
It is the default application for Android to interactively install a normal package. PackageInstaller provide user interface to manage applications/packages. PackageInstaller calls InstallAppProgress activity to receive instructions from the user. InstallAppProgress will ask Package Manager Service to install package via installd.
Now when you click on install, the package manager will calls
installPackageLI(). Here LI means Locked install. In PackageManagerService, if a method ends with ‘LI’, it means that during the install time it has a lock and no other process can manipulate it.
In Android Q, installPackageLI() is replaced with preparepackageLI().
When PackageManagerService calls the installPackageLI(), it is like confirming that you have to install the APK and it will be installed.
This is the guy who is responsible for mantaining the session, if you are trying to install/uninstall multiple applications at a time. It tries to create a session for every installation/uninstallation which is going on.
Installer and Installd
When SystemServer starts, it start all the system critical services. It also starts the Installer which will bind to the installd.
Installer is a SystemService that is responsible for linking the installd to the PackageManagerService. It is like binding the installd to PackageManagerService. Previously it was more like a socket-based communication where PMS uses linux domain socket /dev/socket/installd to send any message to installd.
Installd is a daemon service responsible for handling the installation of packages.
APK file storage in Android
Now APK is installed, but where are these applications are stored in your device.
- Pre-installed apk such as camera, phone, calender etc are stored in /system/[app/priv-app]
- User install apk such WhatsApp, Facebook, etc are stored in /data/app
- PackagerManager creates a data directory /data/data/<package name>/ to store the database, shared preference, native library, and cache data.
How PackageManagerService Store its data?
PackageManagerService store its data across three different files stored at location /data/system
Packages.xml file contains two things: 1. all the packages information and 2. all the permissions that are present in the device.
The Package will contains every detailed information about the package such as name, it’s location in the device, the location of the libraries that it is using, its userId, version, the certificate with which it is signed, the list of permissions the application is requesting, etc. You can see the example below, here is the application named DhcpInfo.
The permissions will have three different attributes.
By default the value for package is android in the permissions tag.
<permissions> <item name="com.google.android.gms.auth.api.phone.permission.SEND" package="com.google.android.gms" protection="2" /> <item name="android.permission.REAL_GET_TASKS" package="android" protection="18" /> <item name="android.permission.ACCESS_CACHE_FILESYSTEM" package="android" protection="18" /> <item name="android.permission.REMOTE_AUDIO_PLAYBACK" package="android" protection="2" /> <item name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" package="com.android.providers.downloads" /> <item name="com.google.android.apps.photos.permission.C2D_MESSAGE" package="com.google.android.apps.photos" protection="2" /> <item name="android.permission.MANAGE_APPOPS" package="android" protection="2" /> <item name="android.permission.REGISTER_WINDOW_MANAGER_LISTENERS" package="android" protection="2" /> <item name="android.permission.INTENT_FILTER_VERIFICATION_AGENT" package="android" protection="18" /> <item name="android.permission.BIND_INCALL_SERVICE" package="android" protection="18" /> <item name="android.permission.PROVIDE_RESOLVER_RANKER_SERVICE" package="android" protection="18" /> . . . </permission>
It is a simple text file that contains the package name, user id, flag, data directory, and target SDK version. The assumption is packages.list file may provide faster lookup of installed packages, because it keeps important information only.
com.swetabh.dhcpinfoapp 10236 1 /data/user/0/com.swetabh.dhcpinfoapp default:targetSdkVersion=29 none 1 1 com.android.storagemanager 10200 0 /data/user/0/com.android.storagemanager default:privapp:targetSdkVersion=29 1065 0 29 com.android.bookmarkprovider 10150 0 /data/user/0/com.android.bookmarkprovider default:targetSdkVersion=29 none 0 29 com.android.settings 1000 0 /data/user_de/0/com.android.settings platform:privapp:targetSdkVersion=19 . . .
This file contains the list of packages that has been stopped. Suppose you have stopped any package, then the packages-stopped.xml will be updated with that value.
How does PackageManagerService install APK in android?
These are the following steps which are performed in PMS
- It keeps on waiting to get request for installing an APK.
- After getting the request, add the package to the queue for installation process.
- It determines appropriate location for the package installation. It will decide, where the APK must be installed. For system apps or priv-app this is done with the help of ParallelPackageParser at boot time.
- Determine the installation, whether it is fresh install or to update the existing one.
- Copies the APK to the directory where it is getting installed.
- Determine the UID of the application.
- Request the installd daemon process (via installer -> installd native calls) to do an installation.
- Then creates the application directory and set required permissions.
- Reflect the latest changes in packages.list and packages.xml.
- Lastly, it sends a Broadcast to the system along with the name of the effect of installation.
- Intent: ACTION_PACKAGE_ADDED -> If it is a new installation
Intent: ACTION_PACKAGE_REPLACED -> If it is an update.
I hope in this post, at least you got the undertsanding of what is PackagerManagerService, what are its use, and how it works in in order to install/uninstall APK in android device.
You can also watch this video to understand how it work.