PackageManagerService
PMS也是SystemServer中一个非常重要的服务,主要有以下功能:
1.管理apk 安装包、apk安装、删除等
2.解析AndroidManifest.xml文件,主要包括节点信息解析target-name的分析和提炼,与AMS和WMS联系紧密
3.扫描本地文件,主要针对apk,主要是系统应用、本地安装应用等等
1.PMS的启动
xref: /frameworks/base/services/java/com/android/server/SystemServer.java
SystemServer进程创建执行中,会调用 startBootstrapServices(); 此方法内进行PMS的初始化调用
private void startBootstrapServices() {
...
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();
...
}
xref: /frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
public static PackageManagerService main(Context context, Installer installer,
boolean factoryTest, boolean onlyCore) {
// Self-check for initial settings.
PackageManagerServiceCompilerMapping.checkProperties();
PackageManagerService m = new PackageManagerService(context, installer,
factoryTest, onlyCore);
m.enableSystemUserPackages();
ServiceManager.addService("package", m);
final PackageManagerNative pmn = m.new PackageManagerNative();
ServiceManager.addService("package_native", pmn);
return m;
}
构造函数中,有传一个installer
public class Installer extends SystemService {
...
}
Installer是一个负责package安装、删除、迁移、更新的系统服务,PMS中需要管理package,那么就需要installer实例的协助
接下来,我们来看PackageManagerService的构造方法
1.1 初始化package解析和管理的对象
1.PackageDexOptimizer
2.DexManager
3.ArtManagerService
4.ProtectedPackages
创建PackageHandler处理package管理中消息
扫描系统app和系统目录下的jar
扫描应用app和应用目录下的lib
// Find base frameworks (resource packages without code).
scanDirTracedLI(frameworkDir, mDefParseFlags
| PackageParser.PARSE_IS_SYSTEM
| PackageParser.PARSE_IS_SYSTEM_DIR
| PackageParser.PARSE_IS_PRIVILEGED,
scanFlags | SCAN_NO_DEX, 0);
// Collected privileged system packages.
final File privilegedAppDir = new File(Environment.getRootDirectory(), "priv-app");
scanDirTracedLI(privilegedAppDir, mDefParseFlags
| PackageParser.PARSE_IS_SYSTEM
| PackageParser.PARSE_IS_SYSTEM_DIR
| PackageParser.PARSE_IS_PRIVILEGED, scanFlags, 0);
// Collect ordinary system packages.
final File systemAppDir = new File(Environment.getRootDirectory(), "app");
scanDirTracedLI(systemAppDir, mDefParseFlags
| PackageParser.PARSE_IS_SYSTEM
| PackageParser.PARSE_IS_SYSTEM_DIR, scanFlags, 0);
都是调用scanDirTracedLI,然后检索的目录不同
而 Environment.getRootDirectory(),获取的就是系统目录路径
private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system");
private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data");
public static File getRootDirectory() {
return DIR_ANDROID_ROOT;
}
我们来看看 scanDirTracedLI(...)的具体调用流程
scanDirTracedLI——>scanDirLl——> new ParallelPackageParser()构造——> submit()——> ParsePackage()
——> PackageParser()构造——>parseClusterPackage()——>ParseBaseApk——>ParseBaseApkCommon()
做的事情比较简单,总结起来就是解析/system/app 目录下面的所有apk,然后解析其中的文件,例如解析AndroidManifest.xml,
解析assets,解析res等等,分别使用存储结构存储起来,这些数据在后面都会用到
网友评论