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.
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.
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
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.
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.
APK file installation location (/system/app/ or /data/app/)
native library (*.so file) default path is /data/data/<package name>/lib/
Store ApplicationInfo Flags [http://developer.android.com/reference/android/content/pm/ApplicationInfo.html]
timestamp in hex format
timestamp in hex format of first time installation
timestamp in hex format of last update
Version Code from AndroidManifest.xml file http://developer.android.com/guide/topics/manifest/manifest-element.html#vcode]
The 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]
The 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.
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.