原创内容,转载请注明出处,多谢配合。
经过前面三篇文章的分析,基本上对Apk的安装流程有了个大体的了解。
再来一张图回顾下整个大框架:
下面简单来实验下安装微信:
我选择在应用市场安装微信,流程是先下载再安装,多个应用的场景下是并行下载,串行安装。
根据安装流程粗分为三步:
一、复制Apk
三方应用安装在data/app下,这个是系统目录,直接看复制之后的结果:
/data/app # ls -al
drwxr-xr-x 4 system system 4096 2019-07-19 13:45 com.tencent.mm-jIsm1s1QsgU_5Q6XcQFYfA==
/data/app/com.tencent.mm-jIsm1s1QsgU_5Q6XcQFYfA== # ls -al
-rw-r--r-- 1 system system 111405470 2019-07-19 13:45 base.apk //主apk在这
drwxr-xr-x 3 system system 4096 2019-07-19 13:45 lib //so库文件
drwxrwx--x 3 system install 4096 2019-07-19 13:45 oat //dex文件
另外还有两个APP自己私有目录在这里一并说下:
内部存储:
/data/data # ls -al
drwx------ 18 u0_a167 u0_a167 4096 2019-07-19 15:08 com.tencent.mm
外部存储:
/sdcard/Android/data # ls -al
drwxrwx--x 3 u0_a167 sdcard_rw 4096 2019-07-19 13:55 com.tencent.mm
点击应用管理的清除全部数据:
data/data/com.tencent.mm 目录还在,内部文件全部被清理掉
/sdcard/Android/data/com.tencent.mm 目录被删除
点击应用管理的清除缓存:
data/data/com.tencent.mm 目录还在,内部子文件夹也在,这就是只清理缓存
/sdcard/Android/data/com.tencent.mm 目录还在
二、安装Apk
安装的本质是pull解析AndroidManifext.xml,按组件分类保存在Package数据结构中,最后通过Settings将所有安装app的信息 ,当系统进行程序安装、卸载和更新等操作时,均会更新该文件。通过readLPw()函数和writeLPr函数分别来读写packages.xml。
- packages.xml 记录所有安装app的信息,当系统进行程序安装、卸载和更新等操作时,均会更新该文件。
- packages-backup.xml 备份文件
- packages-stopped.xml 记录被用户强行停止的应用的Package信息
- packages-stopped-backup.xml 备份文件
- packages.list 记录非系统自带的APK的数据信息,这些APK有变化时会更新该文件
当系统启动、升级或者有APK安装、升级、删除时会重新scan,有变化会更新文件信息。这些xml文件相当于是安装解析之后得到的PackageParser.Package的持久化存储方案。
挑一个packages.xml pull出来看看:
adb root
adb disable-verity
adb reboot
adb root
adb remount
adb pull /data/system/packages.xml .
微信相关的:
<package name="com.tencent.mm" codePath="/data/app/com.tencent.mm-jIsm1s1QsgU_5Q6XcQFYfA==" nativeLibraryPath="/data/app/com.tencent.mm-jIsm1s1QsgU_5Q6XcQFYfA==/lib" primaryCpuAbi="armeabi-v7a" publicFlags="941112900" privateFlags="0" ft="16c08c37d08" it="16c08c387b2" ut="16c08c387b2" version="1440" userId="10167" installer="com.xiaomi.market">
<sigs count="1" schemeVersion="1">
<cert index="12" key="308202eb30820254a00302010202044d36f7a4300d06092a864886f70d01010505003081b9310b300906035504061302383631123010060355040813094775616e67646f6e673111300f060355040713085368656e7a68656e31353033060355040a132c54656e63656e7420546563686e6f6c6f6779285368656e7a68656e2920436f6d70616e79204c696d69746564313a3038060355040b133154656e63656e74204775616e677a686f7520526573656172636820616e6420446576656c6f706d656e742043656e7465723110300e0603550403130754656e63656e74301e170d3131303131393134333933325a170d3431303131313134333933325a3081b9310b300906035504061302383631123010060355040813094775616e67646f6e673111300f060355040713085368656e7a68656e31353033060355040a132c54656e63656e7420546563686e6f6c6f6779285368656e7a68656e2920436f6d70616e79204c696d69746564313a3038060355040b133154656e63656e74204775616e677a686f7520526573656172636820616e6420446576656c6f706d656e742043656e7465723110300e0603550403130754656e63656e7430819f300d06092a864886f70d010101050003818d0030818902818100c05f34b231b083fb1323670bfbe7bdab40c0c0a6efc87ef2072a1ff0d60cc67c8edb0d0847f210bea6cbfaa241be70c86daf56be08b723c859e52428a064555d80db448cdcacc1aea2501eba06f8bad12a4fa49d85cacd7abeb68945a5cb5e061629b52e3254c373550ee4e40cb7c8ae6f7a8151ccd8df582d446f39ae0c5e930203010001300d06092a864886f70d0101050500038181009c8d9d7f2f908c42081b4c764c377109a8b2c70582422125ce545842d5f520aea69550b6bd8bfd94e987b75a3077eb04ad341f481aac266e89d3864456e69fba13df018acdc168b9a19dfd7ad9d9cc6f6ace57c746515f71234df3a053e33ba93ece5cd0fc15f3e389a3f365588a9fcb439e069d3629cd7732a13fff7b891499" />
</sigs>
<perms>
<item name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" granted="true" flags="0" />
<item name="com.google.android.finsky.permission.BIND_GET_INSTALL_REFERRER_SERVICE" granted="true" flags="0" />
<item name="com.google.android.c2dm.permission.RECEIVE" granted="true" flags="0" />
<item name="android.permission.MODIFY_AUDIO_SETTINGS" granted="true" flags="0" />
<item name="android.permission.ACCESS_NOTIFICATION_POLICY" granted="true" flags="0" />
<item name="android.permission.MANAGE_ACCOUNTS" granted="true" flags="0" />
<item name="com.tencent.mm.matrix.strategynotify" granted="true" flags="0" />
<item name="android.permission.NFC" granted="true" flags="0" />
<item name="android.permission.FOREGROUND_SERVICE" granted="true" flags="0" />
<item name="android.permission.WRITE_SYNC_SETTINGS" granted="true" flags="0" />
<item name="android.permission.RECEIVE_BOOT_COMPLETED" granted="true" flags="0" />
<item name="com.tencent.mm.permission.MM_MESSAGE" granted="true" flags="0" />
<item name="com.android.launcher.permission.UNINSTALL_SHORTCUT" granted="true" flags="0" />
<item name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" granted="true" flags="0" />
<item name="android.permission.READ_PROFILE" granted="true" flags="0" />
<item name="android.permission.BLUETOOTH" granted="true" flags="0" />
<item name="android.permission.CHANGE_WIFI_MULTICAST_STATE" granted="true" flags="0" />
<item name="android.permission.GET_TASKS" granted="true" flags="0" />
<item name="android.permission.AUTHENTICATE_ACCOUNTS" granted="true" flags="0" />
<item name="android.permission.INTERNET" granted="true" flags="0" />
<item name="com.android.vending.BILLING" granted="true" flags="0" />
<item name="android.permission.BLUETOOTH_ADMIN" granted="true" flags="0" />
<item name="android.permission.GET_PACKAGE_SIZE" granted="true" flags="0" />
<item name="com.tencent.mm.plugin.permission.READ" granted="true" flags="0" />
<item name="com.tencent.mm.wear.message" granted="true" flags="0" />
<item name="com.tencent.mm.ext.permission.WRITE" granted="true" flags="0" />
<item name="com.android.launcher.permission.READ_SETTINGS" granted="true" flags="0" />
<item name="android.permission.BROADCAST_STICKY" granted="true" flags="0" />
<item name="android.permission.CHANGE_WIFI_STATE" granted="true" flags="0" />
<item name="android.permission.FLASHLIGHT" granted="true" flags="0" />
<item name="android.permission.ACCESS_NETWORK_STATE" granted="true" flags="0" />
<item name="miui.permission.READ_STEPS" granted="true" flags="0" />
<item name="com.tencent.mm.permission.C2D_MESSAGE" granted="true" flags="0" />
<item name="android.permission.USE_FINGERPRINT" granted="true" flags="0" />
<item name="android.permission.READ_SYNC_SETTINGS" granted="true" flags="0" />
<item name="com.tencent.mm.plugin.permission.WRITE" granted="true" flags="0" />
<item name="android.permission.VIBRATE" granted="true" flags="0" />
<item name="android.permission.ACCESS_WIFI_STATE" granted="true" flags="0" />
<item name="com.android.launcher.permission.INSTALL_SHORTCUT" granted="true" flags="0" />
<item name="android.permission.WAKE_LOCK" granted="true" flags="0" />
<item name="com.tencent.mm.ext.permission.READ" granted="true" flags="0" />
</perms>
<proper-signing-keyset identifier="32" />
</package>
主要还是权限
这里就不对每个xml一一分析了。
详细信息可以dumpsys package com.tencent.mm查看。
三、编译Apk
frameworks/base/services/core/java/com/android/server/pm/Installer.java
public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
int dexoptNeeded, @Nullable String outputPath, int dexFlags,
String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
@Nullable String seInfo, boolean downgrade, int targetSdkVersion,
@Nullable String profileName, @Nullable String dexMetadataPath,
@Nullable String compilationReason) throws InstallerException {
Slog.i(TAG,"installdebug: getCallers:"+ Debug.getCallers(20)+"; apkPath:"+apkPath+"; pkgName: "+pkgName+"; compileFilter: "+compilerFilter);
assertValidInstructionSet(instructionSet);
if (!checkBeforeRemote()) return;
try {
mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo, downgrade,
targetSdkVersion, profileName, dexMetadataPath, compilationReason);
} catch (Exception e) {
throw InstallerException.from(e);
}
}
这里加了个Log,打印信息如下:
07-19 13:45:27.855 1501 1596 I Installer: installdebug: getCallers:
com.android.server.pm.PackageDexOptimizer.dexOptPath:305
com.android.server.pm.PackageDexOptimizer.performDexOptLI:253
com.android.server.pm.PackageDexOptimizer.performDexOpt:149
com.android.server.pm.PackageManagerService.installPackageLI:18215
com.android.server.pm.PackageManagerService.installPackageTracedLI:17635
com.android.server.pm.PackageManagerService.access$3300:407
com.android.server.pm.PackageManagerService$10.run:15465
android.os.Handler.handleCallback:873
android.os.Handler.dispatchMessage:99
android.os.Looper.loop:201
android.os.HandlerThread.run:65
com.android.server.ServiceThread.run:44
就是之前介绍的流程
apkPath: /data/app/com.tencent.mm-jIsm1s1QsgU_5Q6XcQFYfA==/base.apk;
pkgName: com.tencent.mm;
compileFilter: quicken 编译方式为quicken
网友评论