核心功能
1.解析AndroidNanifest.xml清单文件,解析清单文件中的所有节点信息
2.扫描.apk文件,安装系统应用,安装本地应用等
3.管理本地应用,主要有, 安装,卸载,应用信息查询 等
启动流程
PackageManagerService和AMS一样都在SystemServer的startBootstrapServices中创建
Installer installer = mSystemServiceManager.startService(Installer.class);
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
//PackageManagerService
main(Context context, Installer installer,boolean factoryTest, boolean onlyCore) {
PackageManagerService m = new PackageManagerService(injector, onlyCore, factoryTest);
ServiceManager.addService("package", m);//将PMS注册到SM中
final PackageManagerNative pmn = m.new PackageManagerNative();
ServiceManager.addService("package_native", pmn);
}
public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
阶段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
public PackageManagerService(Injector injector, boolean onlyCore, boolean factoryTest) {
mMetrics = new DisplayMetrics(); // 分辨率配置
mInstaller = installer; //保存installer对象
mPermissionManager = injector.getPermissionManagerServiceInternal();//创建权限管理服务
mSettings = injector.getSettings();//创建setting
//添加system, phone, log, nfc, bluetooth, shell,se,networkstack 这8种shareUserId到mSettings;
//DexOpt优化
mPackageDexOptimizer = new PackageDexOptimizer(mInstaller, mInstallLock, mContext,"*dexopt*");
mDexManager = new DexManager(mContext, this, mPackageDexOptimizer,installer, mInstallLock);
// ART虚拟机管理服务
mArtManagerService = new ArtManagerService(mContext, this, mInstaller, mInstallLock);
//创建PackageManager的handler线程,循环处理外部安装相关消息
mHandlerThread = new ServiceThread(TAG,Process.THREAD_PRIORITY_BACKGROUND, true/*allowIo*/);
mHandlerThread.start();
mHandler = new PackageHandler(mHandlerThread.getLooper());
}
//Settings 阶段1:BOOT_PROGRESS_PMS_START
Settings(File dataDir, PermissionSettings permission,Object lock) {
//packages.xml 所有安装app信息 PKMS 扫描完目标文件夹后会创建该文件。
//当系统进行程序安装、卸载和更新等操作时,均会更新该文件。该文件保存了系统中与 package 相关的一些信息。
mSettingsFilename = new File(mSystemDir, "packages.xml");
//所有安装app信息的备份
mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
//描述系统中存在的所有非系统自带的 APK 的信息。当这些程序有变动时,PKMS 就会更新该文件。
mPackageListFilename = new File(mSystemDir, "packages.list");
final File kernelDir = new File("/config/sdcardfs");
mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
}
boolean readLPw(@NonNull List<UserInfo> users) {
//解析"/data/system/packages.xml"
XmlPullParser parser = Xml.newPullParser();
parser.setInput(str, StandardCharsets.UTF_8.name());
}
阶段2:BOOT_PROGRESS_PMS_SYSTEM_SCAN_START
scanDirTracedLI()
扫描system/vendor/product/odm/oem等目录的priv-app、app、overlay包;系统apk的安装
private void scanDirLI(File scanDir, int parseFlags, int scanFlags, long currentTime,
PackageParser2 packageParser, ExecutorService executorService) {
ParallelPackageParser parallelPackageParser =new ParallelPackageParser(packageParser, executorService);
for (File file : files) {
parallelPackageParser.submit(file, parseFlags);//将apk放入队列中
}
for (; fileCount > 0; fileCount--) {
ParallelPackageParser.ParseResult parseResult = parallelPackageParser.take();
//下面去解析
//PackageParser.parseBaseApk()主要是对AndroidManifest.xml进行解析,解析后所有的信息放在Package对象中
}
}
阶段3:BOOT_PROGRESS_PMS_DATA_SCAN_START
scanDirTracedLI()
扫描data目录的应用信息,及时更新,祛除不必要的数据;应用apk的安装
阶段4:BOOT_PROGRESS_PMS_SCAN_END
sdk版本变更,更新权限;OTA升级后首次启动,清除不必要的缓存数据;权限等默认项更新完后,清理相关数据;更新package.xml
阶段5:BOOT_PROGRESS_PMS_READY
安装流程
安装时
1、把apk文件复制到data/app/包名目录下,
2、开辟应用程序的数据文件夹(so库,缓存)
3、将apk的dex安装到data/dalvik-cache目录下