1、PMS Overview
PackageManagerService在Android系统中是一个非常重要的系统服务,提供了应用安装包的扫描、安装、卸载以及应用权限管理等比较敏感的功能。由于涉及的功能比较多,进而其代码逻辑也是非常复杂,这里对PackageManagerService的逻辑进行梳理、以便日后功能的开发以及日常Debug
关于PackageManagerService中有很多API是以LP、LI等后缀结尾,我们对这些后缀做如下解释说明:
1.1、PMS中的两个锁
要想弄明白方法名中的LI、LIF、LPw、LPr的含义,需要先了解PackageManagerService内部使用的两个锁。因为LI、LIF、LPw、LPr中的L,指的是Lock,而后面跟的I和P指的是两个锁,I表示mInstallLock同步锁。P表示mPackages同步锁。LPw、LPr中的w表示writing,r表示reading。LIF中的F表示Freeze。
mPackages同步锁,是指操作mPackages时,用synchronized (mPackages) {}保护起来。mPackages同步锁用来保护内存中已经解析的包信息和其他相关状态。mPackages同步锁是细粒度的锁,只能短时间持有这个锁,因为争抢mPackages锁的请求很多,短时间持有mPackages锁,可以让其他请求等待的时间短些。
mInstallLock同步锁,是指安装App的时候,对安装的处理要用synchronized (mInstaller) {}保护起来。mInstallLock同步锁,用来保护所有对installd的访问。installd通常包含对应用数据的繁重操作。
由于installd是单线程的,并且installd的操作通常很慢,所以在已经持有mPackages同步锁的时候,千万不要再请求mInstallLock同步锁。反之,在已经持有mInstallLock同步锁的时候,可以去请求mPackages同步锁。
1.2、方法名中的LI、LIF、LPw、LPr的含义
2、Main Class Diagram
PackageManagerService开机由SystemServer启动、后面会详细描述
PackageManager是PackageManagerService的代理,是一个抽象类,具体的Implement者是ApplicationPackageManager
ApplicationPackageManager通过AIDL IPackageManager获取到PackageManagerService的服务API
PackageManagerService通过Installer实现应用的安装与卸载
3、Initialization
3.1、ApplicationPackageManager
上面提到、PackageManager是一个抽象类,实际的实现类是ApplicationPackageManager。当客户端利用Context的getPacakgeManager函数获取PackageManger时,获取的就是ApplicationPacakgeManager、看它的构造函数:
protected ApplicationPackageManager(ContextImpl context,IPackageManager pm) { mContext = context; mPM = pm;
}
容易看出,ApplicationPackageManger中持有了IPackageManger对象。
3.2、SystemServer
private void startBootstrapServices() { // Wait for installd to finish starting up so that it has a chance to // create critical directories such as /data/user with the appropriate // permissions. We need this to complete before we initialize other services.//启动Installer服务 Installer installer = mSystemServiceManager.startService(Installer.class); ......... //根据系统属性,决定是否为加密设备加密 String cryptState = SystemProperties.get("vold.decrypt"); if (ENCRYPTING_STATE.equals(cryptState)) { Slog.w(TAG, "Detected encryption in progress - only parsing core apps"); mOnlyCore = true; } else if (ENCRYPTED_STATE.equals(cryptState)) { Slog.w(TAG, "Device encrypted - only parsing core apps"); mOnlyCore = true; } //调用PKMS的main函数 mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); //判断是否为初次启动 mFirstBoot = mPackageManagerService.isFirstBoot(); mPackageManager = mSystemContext.getPackageManager(); .......... // Manages A/B OTA dexopting. This is a bootstrap service as we need it to rename // A/B artifacts after boot, before anything else might touch/need them. // Note: this isn't needed during decryption (we don't have /data anyways). if (!mOnlyCore) { boolean disableOtaDexopt = SystemProperties.getBoolean("config.disable_otadexopt", false); if (!disableOtaDexopt) { try { //启动OtaDexoptService也需要PackageMangerService的参与 OtaDexoptService.main(mSystemContext, mPackageManagerService); }...... } }}
网友评论