1伞租、PMS Overview
PackageManagerService在Android系統(tǒng)中是一個非常重要的系統(tǒng)服務,提供了應用安裝包的掃描灶挟、安裝坏逢、卸載以及應用權(quán)限管理等比較敏感的功能。由于涉及的功能比較多妙黍,進而其代碼邏輯也是非常復雜悴侵,這里對PackageManagerService的邏輯進行梳理、以便日后功能的開發(fā)以及日常Debug
關(guān)于PackageManagerService中有很多API是以LP拭嫁、LI等后綴結(jié)尾可免,我們對這些后綴做如下解釋說明:
1.1、PMS中的兩個鎖
要想弄明白方法名中的LI噩凹、LIF巴元、LPw、LPr的含義驮宴,需要先了解PackageManagerService內(nèi)部使用的兩個鎖逮刨。因為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同步鎖用來保護內(nèi)存中已經(jīng)解析的包信息和其他相關(guān)狀態(tài)。mPackages同步鎖是細粒度的鎖卒茬,只能短時間持有這個鎖船老,因為爭搶mPackages鎖的請求很多,短時間持有mPackages鎖圃酵,可以讓其他請求等待的時間短些柳畔。
mInstallLock同步鎖,是指安裝App的時候郭赐,對安裝的處理要用synchronized (mInstaller) {}保護起來荸镊。mInstallLock同步鎖,用來保護所有對installd的訪問。installd通常包含對應用數(shù)據(jù)的繁重操作躬存。
由于installd是單線程的张惹,并且installd的操作通常很慢,所以在已經(jīng)持有mPackages同步鎖的時候岭洲,千萬不要再請求mInstallLock同步鎖宛逗。反之,在已經(jīng)持有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實現(xiàn)應用的安裝與卸載
3挤巡、Initialization
3.1、ApplicationPackageManager
上面提到酷麦、PackageManager是一個抽象類矿卑,實際的實現(xiàn)類是ApplicationPackageManager。當客戶端利用Context的getPacakgeManager函數(shù)獲取PackageManger時沃饶,獲取的就是ApplicationPacakgeManager母廷、看它的構(gòu)造函數(shù):
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); ......... //根據(jù)系統(tǒng)屬性,決定是否為加密設(shè)備加密 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; } //調(diào)用PKMS的main函數(shù) 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); }...... } }}