Search code examples
javaandroidapkandroid-package-managersandroid-install-apk

"How are" Android applications (Facebook etc.) installed to an android phone?


I know how to install an application to an Android device e.g from Play-Store or via an .apk file. But I'd like to understand the actual process of installation.

E.g. on Windows:

  • Serial codes etc. are placed in the registry
  • Files important to the running of software are placed within the Program Files folder (the main .exe etc.)

So far, what I do know about the Android application installation process is:

  • After an android application has been executed (post-installation), data freshly downloaded is placed in locations like: Android/data or Android/obb etc.

  • If specifically expressed by an Android developer, files can also be placed elsewhere e.g. on secondary storage (memory cards, cloud storage etc.)

But other than that, my knowledge concerning the Android installation process is pretty slim e.g.:

  • I don't know where important files relating to an Android program's execution are placed (in the case of Windows, the .exe, related media, libraries etc.)

  • I similarly don't know how these files are structured upon an Android device (post-installation) e.g. are these files structured in folders like: java, res, menu, layout etc. as during development within environments like Android Studio?

  • Neither do I know how what application file-types are stored on a user's device post-installation (after being unpacked from the android installation file or apk) e.g. are they stored as .java and or .xml files, as during development etc.

Hopefully someone can fill in the blanks, thank you.


Solution

  • Beginnning

    PackageInstaller calls InstallAppProgress activity to receives an instruction from the user. InstallAppProgress will ask PackageManager Service to install package via installd. Source code is available at <Android Source>/packages/apps/PackageInstaller.

    When we install APK file, Package Manager parse the package(APK) file and display confirmation, When user press OK button, Package Manager call method named installPackage with these four parameters namely uri, installFlags, observer, installPackageName. Package Manager start one service named "package", now all fuzzy things happen in this service. you can check PackageInstallerActivity.java and InstallAppProgress.java in PackageInstaller source code. Package Manager Service running in system_service process and install daemon (installd) that runs as a native process both start at system boot time.

    Where APK files stores in Android ?

    1. Pre-Install (i.e. Camera, Calendar, Browser,etc.) APK stored in /system/app/
    2. User Install (ApiDemo, Any.do, etc.) APK stored in /data/app/
    3. Package Manager create data directory /data/data/<package name>/ to store database, shared preference, native library and cache data

    You might see apk file and *.odex file for same APK, ODEX file is totally different discussion and purpose.

    What is APK installation process in detail ?

    Following process execute 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
    • A copy of the apk file to a given directory
    • Determine the UID of the app
    • Request to 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.

    enter image description here

    How Package Manager store data ?

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

    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. Name attribute has permission name which we are using in AndroidManifest.xml, package attribute indicate permission belong to package, In majority cases "android" is values because <permission> tag contain default permissions and protection indicate level of security.

    packages.list: It is simple text file contain package name, user id, flag and data directory, I can't find any perfect description but I assume it that packages.list file may provide faster lookup of installed package because it file keep 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
    

    packages-stoped.xml: This file contain package list which has stopped state, Stope stated applications can not receive any broadcast.

    Where I can find the source code of Package Manager and Package Installer ?

    Package Manager

    frameworks/base/services/java/com/android/server/pm/Settings.java
    frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
    frameworks/base/services/java/com/android/server/pm/IPackageManager.aidl
    frameworks/base/services/java/com/android/server/pm/PackageSignatures.java
    frameworks/base/services/java/com/android/server/pm/PreferredActivity.java
    frameworks/services/java/com/android/server/PreferredComponent.java
    frameworks/core/java/android/content/IntentFilter.java
    frameworks/base/core/java/android/content/pm/PackageParser.java
    frameworks/base/services/java/com/android/server/pm/Installer.java
    frameworks/base/core/java/com/android/internal/app/IMediaContainerService.aidl
    frameworks/base/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.java
    

    Package Installer

    packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
    packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageUtil.java
    packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java.
    

    Links to refer: link 1 and link 2.