Android: Understanding the APK installation process

AndroidInstallationApk

Android Problem Overview


I am trying to understand the process of how an apk is installed on Android, specifically the Android SDK emulator via adb install (where i am testing).

In searching I have found no satisfactory answer outside of "the apk is simply copied to /data/app and is installed when you run it. This is not satisfactory to me as it does not explain how the apk's icon appears on the menu amongst other issues.

Coming from a Windows background, running an .exe or installer to install a program modifies registries, files, services, etc.... I need to understand if this or something similar occurrs when an apk is installed on Android.

So if anyone can explain to me what occurrs specifically when an apk is installed on Android I would greatly appreciate it.

On a side note I would also like to know if the Dalvik VM "zygote" is involved in the installation or does it occur at the lower linux kernel level?

My ultimate goal here is to use strace to caputer the installation process of an apk to document system modifications, file creations, network activity and other events of interest.

Android Solutions


Solution 1 - Android

There are mainly two categories of Android applications.

  1. System Apps: installed when system is initialized
  2. User Apps: installed from Play store, using ADB or copying .apk file in SD card.

Following are the step by step installation process.

  1. AndroidManifest.xml is parsed, information is extracted and stored into /data/system/packages.xml and /data/system/packages.list
  2. .apk file is copied to a specific directory and a directory for data storage is created for this app

XML parsing, resource analysis, and .apk file copying are done by

> PackageManageService.java

however, directory creation is done by

> installd.c

PackageManageService.java communicates with installd.c via a local socket, located at /dev/socket/installed

Package where .apk file got copied is different for system apps and user apps. for system apps it is

> /system/app/

Where as for user app .apk file copied in to .apk file is copied to

> /data/app

.dex file, which is extracted from the .apk file, is copied to /data/dalvik-cache/.

Package Manager creates data directory /data/data// to store database, shared preference, native library and cache data

Solution 2 - Android

Everything that you want to know, I think, is in the Android develeper website http://developer.android.com/tools/building/index.html and to understand packaging of the app itself, here is the image better (it's not the APK installation process, but it can help you understand the apk structure and instalation). You can also google about aapt tool and .dex to understand more because classes.dex is the substruction of your application - contains the java and classes compiled. One dex file contains multiple classes as opposed to java class file which contain only that one class. dex file is java bytecode converted with DX tool which is integral part of Android SDK. As a result of that, it allows every application to run as its own process with its own instance of the Dalvik virtual machine. apk

Solution 3 - Android

"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"

More info at Link https://dzone.com/articles/depth-android-package-manager

Solution 4 - Android

This is done at two levels 1.) PackageInstaller(UI) using PackageManagerService(System service) sending command to 2nd part(Installd) 2.) Installd is a daemon which actually install the apk. Installd and PackageManagerService communicate using socket communication . Socket used is installd itself. You can refer to init.rc for the socket creation at init.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1631295View Question on Stackoverflow
Solution 1 - AndroidsappuView Answer on Stackoverflow
Solution 2 - AndroidandroidEnthusiastView Answer on Stackoverflow
Solution 3 - AndroidangeloplcView Answer on Stackoverflow
Solution 4 - AndroidYogesh SharmaView Answer on Stackoverflow