Android APK的安装过程

来源:互联网 发布:永琪和知画圆房了几次 编辑:程序博客网 时间:2024/05/22 11:53

APK安装方式不同,决定其在设备上的存放位置不同。这个过程由packagemanagerservice主导,具体过程中涉及哪些实体,目的是什么是本文讨论的话题。

APK的存放目的地

预装APK放在/system/app 或者/system/priv-app/; 用户后装APK放在/data/app/;
1. 放在/system/与放在/data/的区别在于: /system/对于非root是read only, 因此,放在/system/app或者/system/priv-app的APK通常没法删除。
2. 放在/system/app/与放在/system/priv-app/的区别在于:只有/system/priv-app/中的APK能获取signatureOrSystem或其他privileged permissions。

PackageManagerService通过上面几个目录会将信息提取出来并放在:
1. 在/data/data/< package name>/处为APK生成一个目录,用来存放database, sharedPreference,native library.
2. 在/data/dalvik-cache/< package name>/处为APK生成一个目录,用来存放odex字节码。
3. 在/data/system/packages.xml中为APK添加package name/codePath/nativeLibraryPath/flag/version/timestamp of first installation/timestamp of last update/shardUserId/userId/permission等信息;
4. 在/data/system/packages.list中存放一个条目(package name, user id, flag, data directory);
5. 以后可能会在/data/system/packages-stopped.xml中添加这个package, stopped state application 将不能接受broadcast;

Application user ID与signature

Android sandboxing机制复用了Linux kernel通过user id 来分离per user resource的机制。与传统Linux不同的是,Android用user id来区分application而不是实际的物理user。
Application的user ID通常在安装的时候就分配好了,并且将伴随Application直至卸载。
具备相同key的application可以采用相同的shared user id,从而可以共享资源甚至共存于同一个process。通常只有核心service或者system application才会采用shared user id. Android 常用的built-in user id 举例:
1. android.uid.system (SYSTEM_UID, 1000)
2. android.uid.phone (PHONE_UID, 1001)
3. android.uid.bluetooth (BLUETOOTH_UID, 1002)
4. android.uid.log (LOG_UID, 1007)
5. android.uid.nfc (NFC_UID, 1027)
Multiple user support: 从4.2开始,Android引入multiple user,对应于每一个物理user,指定一个user id(从0开始),这个user id 与application user id 一起能构成一个组合ID,用来区分在不同物理user 下运行的统一个application。例如: uY_aXXXX, Y 代表物理user id, XXX代表application user id.

APK安装过程

PackageManager->aidl->PackageManagerService->PackageInstaller-> Installd.
1. Waiting
2. Add a package to the queue for the installation process
3. Determine the appropriate location of the package installation
4. Determine installation Install / Update new
5. Copy the apk file to a given directory
6. Determine the UID of the app
7. Request the installd daemon process
8. Create the application directory and set permissions
9. Extraction of dex code to the cache directory
10. To reflect and packages.list / system / data / packages.xml the latest status
11. Broadcast to the system along with the name of the effect of the installation is complete package
12. Intent.ACTION_PACKAGE_ADDED: If the new ( Intent.ACTION_PACKAGE_REPLACED): the case of an update
这里写图片描述

packagemanager关系

这里写图片描述
参见: http://www.cnblogs.com/xiaoxiaoboke/archive/2012/02/08/2342880.html

0 0