Android PMS之启动流程
作者:互联网
一、PMS整体的结构图
PMS是通过Binder机制进行与客户端的通信,其中:
- Binder服务端:即系统服务PackageManagerService extends IPackageManager.Stub
- Binder客户端:即我们应用层经常使用的PackageManager,但实际上PackageManager是一个抽象类,所以实际使用的ApplicationPackageManager
二、PMS的简单启动流程
1、PMS是核心系统服务,所以一定是在SystemServer中启动的
在SystemServer的main()方法->run()方法中会启动一些系统服务,其中包括PMS。PMS是在startBootstrapServices方法中进行启动的,这里是优先启动一些比较重要的系统服务,包括PMS、AMS等
private void run() {
·····
// Start services.
try {
t.traceBegin("StartServices");
startBootstrapServices(t);
startCoreServices(t);
startOtherServices(t);
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd(); // StartServices
}
·········
}
在startBootstrapServices方法中调用PMS的main方法
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
`````
t.traceBegin("StartPackageManagerService");
try {
Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
} finally {
Watchdog.getInstance().resumeWatchingCurrentThread("packagemanagermain");
}
// Now that the package manager has started, register the dex load reporter to capture any
// dex files loaded by system server.
// These dex files will be optimized by the BackgroundDexOptService.
SystemServerDexLoadReporter.configureSystemServerDexReporter(mPackageManagerService);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();
t.traceEnd();
``````
}
2、PackageManagerService的main方法
main()方法主要检查一些属性,然后执行PackageManagerService的构造函数进行初始化。最后在ServiceManager进行注册”package”和”package_native”
public static PackageManagerService main(Context context, Installer installer,
boolean factoryTest, boolean onlyCore) {
// Self-check for initial settings.
PackageManagerServiceCompilerMapping.checkProperties();
``````
PackageManagerService m = new PackageManagerService(injector, onlyCore, factoryTest);
m.installWhitelistedSystemPackages();
ServiceManager.addService("package", m);
final PackageManagerNative pmn = m.new PackageManagerNative();
ServiceManager.addService("package_native", pmn);
return m;
}
3、PackageManagerService的构造函数
PackageManagerService构造函数做了pms启动的主要事情。
主要包含以下五个阶段
阶段1:BOOT_PROGRESS_PMS_START
阶段2:BOOT_PROGRESS_PMS_SYSTEM_SCAN_START
阶段3:BOOT_PROGRESS_PMS_DATA_SCAN_START
阶段4:BOOT_PROGRESS_PMS_SCAN_END
阶段5:BOOT_PROGRESS_PMS_READY
阶段1:BOOT_PROGRESS_PMS_START
主要工作:
(1)构造 DisplayMetrics ,保存分辨率等相关信息;
(2)创建Installer对象,与installd交互;
(3)创建mPermissionManager对象,进行权限管理;
(4)构造Settings类,保存安装包信息,清除路径不存在的孤立应用,主要涉及/data/system/目录的packages.xml,packages-backup.xml,packages.list,packages-stopped.xml,packages-stopped-backup.xml等文件。
(5)构造PackageDexOptimizer及DexManager类,处理dex优化;
(6)创建SystemConfig实例,获取系统配置信息,配置共享lib库;
(7)创建PackageManager的handler线程,循环处理外部安装相关消息。
阶段2:BOOT_PROGRESS_PMS_SYSTEM_SCAN_START
主要工作:
(1)从init.rc中获取环境变量BOOTCLASSPATH和SYSTEMSERVERCLASSPATH;
(2)对于旧版本升级的情况,将安装时获取权限变更为运行时申请权限;
(3)扫描system/vendor/product/odm/oem等目录的priv-app、app、overlay包;
(4)清除安装时临时文件以及其他不必要的信息。
阶段3:BOOT_PROGRESS_PMS_DATA_SCAN_START
主要工作:
(1)处理data目录的应用信息,及时更新,祛除不必要的数据。
阶段4:BOOT_PROGRESS_PMS_SCAN_END
主要工作:
(1)sdk版本变更,更新权限;
(2)OTA升级后首次启动,清除不必要的缓存数据;
(3)权限等默认项更新完后,清理相关数据;
(4)更新package.xml
阶段5:BOOT_PROGRESS_PMS_READY
主要工作:
(1)创建PackageInstallerService对象
(2)GC回收内存
标签:PackageManagerService,PMS,流程,package,BOOT,PROGRESS,Android,main 来源: https://blog.csdn.net/kh896424665/article/details/111601078