58

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.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
user1631295
  • 709
  • 2
  • 7
  • 5
  • I'm curious to learn why you are so interested in this? That said, it's all open-source, so have at it. – 323go Sep 15 '12 at 22:45
  • The zygote is not involved into the installation process. It is a completely seperate mechanism. You can check my answer in this (http://stackoverflow.com/questions/9153166/understanding-android-zygote-and-dalvikvm/9153830#9153830) question to learn more about it. – Paul Sep 15 '12 at 22:51
  • 3
    I have been asked to figure this out for a research project, I need specifics, no one seems to have taken the time to document the installation process details online (or at least I have not found it), by understanding the installation process, I can verify I am aaccurately capturing it using strace or some equivalent option. do you have a url for me or can explain to me how this works? – user1631295 Sep 15 '12 at 22:53
  • Thanks Paul, links are great in explaining Dalvik, but I am still needing the details on the apk installation process. J – user1631295 Sep 15 '12 at 22:56
  • http://android.stackexchange.com/ has answers – MarcH Dec 29 '12 at 02:34
  • 1
    Hello, This is an wonderful question. Though am pretty much late into this. Can you please share with me if you have found the answer? **Am i need of the same**. Thanks in advance. – Sathish Jul 03 '14 at 09:23
  • @user1631295 Please have a look at my answer. Hope I addressed it. – sappu Sep 07 '15 at 11:10
  • https://dzone.com/articles/depth-android-package-manager – Ronak Poriya Jan 28 '16 at 11:49
  • http://stackoverflow.com/questions/30358687/how-are-android-applications-facebook-etc-installed-to-an-android-phone – Sunny Kumar Aditya Jun 28 '16 at 09:43

4 Answers4

41

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

kwesolowski
  • 695
  • 8
  • 18
sappu
  • 6,665
  • 4
  • 19
  • 18
  • Can you guide a bit about this base.apk? This stuff is introduced after kitkat, /data/app/com.package.name-RY_AUYdNAwLZg==/base.apk – suv Apr 12 '19 at 10:12
  • Can you please help with this https://stackoverflow.com/questions/55649767/rename-base-apk ? – suv Apr 12 '19 at 10:38
  • Dex file isn't generated during install? – QkiZ Jul 26 '19 at 05:51
  • Excellent answer! Not sure why the questioner did not accept it. Do you know if the packages.xml file also contains configuration from the manifest? I am interested in finding in an APK has `android:debuggable="true"` set. – russoue Dec 14 '19 at 05:06
  • can you share any official document for this... i am curious to know more detail – irshad May 07 '20 at 21:59
  • The only bit that's missing: how and where are resources deployed? Or are they bundled in the dex file? In some incarnation of Android, resources were parsed, and non-applicable resources were discarded. Where is that done? – Robin Davies Jun 01 '21 at 03:34
12

Everything that you want to know, you can find on the official Android developer website and to maybe better understand packaging of the app itself, here is one image (it's NOT the APK installation process, it's the building process, but it can help you understand the apk structure and instalation). You can also search for 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

androidEnthusiast
  • 1,140
  • 1
  • 12
  • 21
  • 15
    Hi and thanks Natasa, this link (and figure) explain very well the details of building the apk, BUT it does not explain the details of the APK installation process, it only states for both running on an emulator and a device "...and install your apk on the device (emulator)..." I still need to understand what Anroid OS modifications occurr during an apk installation, which so far in the awesome thread, remains unanswered, please assist. J – user1631295 Sep 16 '12 at 14:41
  • 4
    This is not the answer to the question: This explains the APK Build Process, but not the installation process on the device. – sweisgerber.dev Jun 30 '15 at 11:05
0

"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

angeloplc
  • 1
  • 2
-1

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.

Yogesh Sharma
  • 21
  • 2
  • 7