Over a million developers have joined DZone.

In Depth: Android Package Manager and Package Installer

Learn more about Android Package Manager and Installer, including where APK files are stored in Android, where the manager stores data, and more.

· Mobile Zone

We are installing and uninstalling APK(s) every day, sometimes many in a day, but have you tried to get answers to the following questions ?

  1. What are Package Manager and Package Installer ?
  2. Where are APK files stored in Android ?
  3. What is the APK installation process in detail ?
  4. How does Package Manager store data ?
  5. Where I can find the source code of Package Manager and Package Installer ?

What are Package Manager and Package Installer?

PackageInstaller 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. Source code is available at  <Android Source>/packages/apps/PackageInstaller.

Installd  daemon's primary role is to receive request from Package Manager Service via Linux domain socket / dev/ socket/ installed. installd executes a series of steps to install APK with root permission.

[Ref: https://github.com/android/platform_frameworks_base/blob/master/cmds/installd/commands.c]

Package Manage is an API that actually manages application install, uninstall, and upgrade. When we install the APK file, Package Manager parse the package (APK) file and displays confirmation. When the user presses the OK button, Package Manager calls the method named "installPackage" with these four parameters namely uri, installFlags, observer, installPackageName. Package Manager starts one service named "package", and now all fuzzy things happen in this service. You can check "PackageInstallerActivity.java" and "InstallAppProgress.java" in the PackageInstaller source code. Package Manager Service runs in system_service process and  install daemon (installd) runs as a native process. Both start at system boot time.

Where are APK Files Stored in Android?

a. Pre-Install (i.e. Camera, Calendar, Browser,etc.) APK is stored in /system/app/

b. User Install (ApiDemo, Any.do, etc.) APK is stored in /data/app/

c. Package Manager creates a data directory /data/data/<package name>/  to store the database, shared preference, native library and cache data.

You might see an apk file and *.odex file for the same APK. The ODEX file is totally a different discussion and purpose.

What is the APK Installation Process in Detail?

The following process executes in Package Manager Service.

  • Waiting 
  • Add a package to the queue for the installation process 
  • Determine the appropriate location of the package installation 
  • Determine installation Install / Update new 
  • Copy the apk file to a given directory 
  • Determine the UID of the app 
  • Request the installd daemon process 
  • Create the application directory and set permissions 
  • Extraction of dex code to the cache directory 
  • To reflect and packages.list / system / data / packages.xml the latest status 
  • Broadcast to the system along with the name of the effect of the installation is complete package 
  • Intent.ACTION_PACKAGE_ADDED: If the new ( Intent.ACTION_PACKAGE_REPLACED): the case of an update
Package Installer / Manager

How Does the Package Manager Store Data?

Package Manager stores application information in three files, located in  /data/system. The following sample is extracted from Android 4 ICS emulator image.

1. packages.xml :This file contain list of permissions and Packages/Applications.

<last-platform-version external="15" internal="15">
<item name="android.permission.CHANGE_WIFI_MULTICAST_STATE" package="android" protection="1">
<item name="android.permission.CLEAR_APP_USER_DATA" package="android" protection="2">

<package codepath="/system/app/Contacts.apk" flags="1" ft="136567b3990" it="136567b3990" name="com.android.contacts" nativelibrarypath="/data/data/com.android.contacts/lib" shareduserid="10001" ut="136567b3990" version="15">
<sigs count="1">
<cert index="2">
<package codepath="/data/app/com.project.t2i-2.apk" flags="0" ft="13a837c2068" it="13a83704ea3" name="com.project.t2i" nativelibrarypath="/data/data/com.project.t2i/lib" userid="10040" ut="13a837c2ecb" version="1">
<sigs count="1">
<cert index="3" key="308201e53082014ea0030201020204506825ae300d06092a86
<item name="android.permission.WRITE_EXTERNAL_STORAGE">

This XML file stores two things 1. permissions 2. package (application), permission are store under <permissions> tag. Each Permission has three attributes namely name, package and protection. The name attribute has permission name, which we are using in AndroidManifest.xml, the package attribute indicates permission belonging to package. In most cases "android" is the value, because the <permission> tag contains default permissions and protection indicates level of security.

The package tag contain 10 attributes and a few sub tags.

SrAttribute NameDescription
1namepackage name
2codePathAPK file installation location (/system/app/ or /data/app/)
3nativeLibraryPathnative library (*.so file) default path is /data/data/<package name>/lib/
4flagStore ApplicationInfo Flags [http://developer.android.com/reference/android/content/pm/ApplicationInfo.html]
5fttimestamp in hex format
6lttimestamp in hex format of first time installation
7uttimestamp in hex format of last update
8versionVersion Code from AndroidManifest.xml file []http://developer.android.com/guide/topics/manifest/manifest-element.html#vcode]
9sharedUserIdThe name of Linux user ID that will be shared with other applications, It is same parameter which we define in AndroidManifest.xml [http://developer.android.com/guide/topics/manifest/manifest-element.html#uid]
10userIdThe name of a Linux user ID

Sub Tags

a. sigs signature information, count attribute represents the number of cert tag.

b. cert  contain certification key, index attribute represents the global index of certificate. I have found that it increments when new certificates are installed with the application.

c. perms contain permission which developer has set in AndroidManifest.xml

2. packages.list : It is simple text file containing package name, user id, flag and data directory. I can't find any perfect description, but I assume that the packages.list file may provide faster lookup of installed packages, because it keeps important information only.

com.android.launcher 10013 0 /data/data/com.android.launcher
com.android.quicksearchbox 10033 0 /data/data/com.android.quicksearchbox
com.android.contacts 10001 0 /data/data/com.android.contacts
com.android.inputmethod.latin 10006 0 /data/data/com.android.inputmethod.latin

3. packages-stoped.xml : This file contains the package list which has stopped state. Stopped state applications cannot receive any broadcast. Refer to this link for more information about stopped state applications.

<pkg name="com.android.widgetpreview" nl="1"></pkg>
<pkg name="com.example.android.livecubes" nl="1"></pkg>
<pkg name="com.android.gesture.builder" nl="1"></pkg>
<pkg name="com.example.android.softkeyboard" nl="1"></pkg>

Where I Can Find the Source Code of Package Manager and Package Installer?

Package Manager

Package Installer

java,mobile,android,tips and tricks,package manager,package installer

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}