"How are" Android applications (Facebook etc.) installed to an android phone? [closed]
Asked Answered
S

1

8

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.

Shofar answered 20/5, 2015 at 19:26 Comment(3)
One thing you can be sure of: there is no one general answer. Especially now after the switch from Dalvik to ART. Have you ever considered rooting a device and just looking what you'll find?Roxannaroxanne
You don't need to root a device to take a look around. Plug a phone into your computer and run adb shell. The commands are similar to a unix shell - e.g. you can type cd and ls. Check out directories like /data/app. blog.shvetsov.com/2013/02/…Mattingly
Beyond finding a description of the APK format at your favorite wiki - the newer stuff about ART was covered at the last Google IO: youtu.be/EBlTzQsUoOwBaccy
L
13

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.

Leavis answered 22/5, 2015 at 3:6 Comment(6)
How does the android O.S extract .dex files and where does it extract them to, for each installed application?Shofar
OS retrieve package from apk, so it store it in /data/system. please refer to my answer's titles What is APK installation process in detail and How Package Manager store data. There is everything described in details.Leavis
I meant, how does the Android O.S "extract dex code to cache directories" and where are these cache directories located for each application?Shofar
ah, sorry. So, since apk is sort of archive, so it convert apk to zip and simply extract files. This is the example of the path /data/dalvik-cache/x86/data@[email protected]@[email protected]. Found one more link, could be useful.href="github.com/dogriffiths/HeadFirstAndroid/wiki/…Leavis
What I mean is, I know how to extract the apk file: so I can see "AndroidManifest.xml", "classes.dex" and "resources.arsc" etc. But you mentioned above, that after the O.S extracts the apk, it then extracts classes.dex to a cache location: where is this location? and how does it extract the classes.dex file to show the original classes?Shofar
I don't fully understand what you mean... if you want to convert dex to java source by yourself, so you use dex2jar , path is the same as in my comment above /data/dalvik-cache/x86/data@app@[email protected]@classes.dex and OS doesn't convert dex file to original. It is specially converted to dex that Dalvik VM can execute them... About Manifest I didn't check and cannot find any relevant link, but I guess it must be on same path. Just check it yourself. Maybe you need a root for this, but cannot say for sure.Leavis

© 2022 - 2024 — McMap. All rights reserved.