Android PMS(三)-Installd執(zhí)行dexopt流程

原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處,多謝配合。

APK經(jīng)過(guò)復(fù)制、創(chuàng)建對(duì)應(yīng)包文件夾、安裝之后,還剩一個(gè)比較重要的點(diǎn)需要分析,那就是dex編譯。

上篇在installPackageLI中:

private void installPackageLI(InstallArgs args, PackageInstalledInfo res) {
           ...
       mPackageDexOptimizer.performDexOpt(pkg, pkg.usesLibraryFiles,
                               null /* instructionSets */, false /* checkProfiles */,
                               getCompilerFilterForReason(REASON_INSTALL),
                              getOrCreateCompilerPackageStats(pkg),
       mDexManager.isUsedByOtherApps(pkg.packageName));
              ...
}

執(zhí)行dex編譯優(yōu)化

frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java

int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries,
       String[] instructionSets, boolean checkProfiles, String targetCompilationFilter,
       CompilerStats.PackageStats packageStats, boolean isUsedByOtherApps) {
    ...
            return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles,
      ...
}

繼續(xù)看performDexOptLI

/**
* Performs dexopt on all code paths and libraries of the specified package for specified
* instruction sets.
*
* <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are
* synchronized on {@link #mInstallLock}.
*/
private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries,
       String[] targetInstructionSets, boolean checkForProfileUpdates,
       String targetCompilerFilter, CompilerStats.PackageStats packageStats,
       boolean isUsedByOtherApps) {
  ...
        for (String dexCodeIsa : dexCodeInstructionSets) {
            int newResult = dexOptPath(pkg, path, dexCodeIsa, compilerFilter, profileUpdated,
                   sharedLibrariesPathWithSplits, dexoptFlags, sharedGid, packageStats);
           // The end result is:
           //  - FAILED if any path failed,
           //  - PERFORMED if at least one path needed compilation,
           //  - SKIPPED when all paths are up to date
           if ((result != DEX_OPT_FAILED) && (newResult != DEX_OPT_SKIPPED)) {
                result = newResult;
           }
        }
    }
    return result;
}

循環(huán)執(zhí)行dexOptPath

/**
* Performs dexopt on the {@code path} belonging to the package {@code pkg}.
*
* @return
*      DEX_OPT_FAILED if there was any exception during dexopt
*      DEX_OPT_PERFORMED if dexopt was performed successfully on the given path.
*      DEX_OPT_SKIPPED if the path does not need to be deopt-ed.
*/
@GuardedBy("mInstallLock")
private int dexOptPath(PackageParser.Package pkg, String path, String isa,
       String compilerFilter, boolean profileUpdated, String sharedLibrariesPath,
       int dexoptFlags, int uid, CompilerStats.PackageStats packageStats) {
...
       mInstaller.dexopt(path, uid, pkg.packageName, isa, dexoptNeeded, oatDir, dexoptFlags,
               compilerFilter, pkg.volumeUuid, sharedLibrariesPath, pkg.applicationInfo.seInfo);
...
}
frameworks/base/services/core/java/com/android/server/pm/Installer.java

private volatile IInstalld mInstalld;
...
IBinder binder = ServiceManager.getService("installd”);
...
mInstalld = IInstalld.Stub.asInterface(binder);
…
public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
       int dexoptNeeded, @Nullable String outputPath, int dexFlags,
       String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
       @Nullable String seInfo)
        throws InstallerException {
    assertValidInstructionSet(instructionSet);
   if (!checkBeforeRemote()) return;
   try {
        mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
               dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo);
   } catch (Exception e) {
        throw InstallerException.from(e);
   }
}

這里installd是init啟動(dòng)的native進(jìn)程,7.0及之前它與Installer是進(jìn)行socket通信,8.0之后換成了binder,如上代碼也明顯能看出。

frameworks/native/cmds/installd/installd.cpp

8.0之后也不像7.0的時(shí)候在installd.cpp中通過(guò)cmds命令對(duì)應(yīng)depot了 { "dexopt", , do_dexopt },現(xiàn)在操作是在dexopt.cpp中進(jìn)行。

frameworks/native/cmds/installd/dexopt.cpp
int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* instruction_set,
        int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
        const char* volume_uuid, const char* shared_libraries, const char* se_info) {
...
        run_dex2oat(input_fd.get(),
                    out_oat_fd.get(),
                    in_vdex_fd.get(),
                    out_vdex_fd.get(),
                    image_fd.get(),
                    dex_path,
                    out_oat_path,
                    swap_fd.get(),
                    instruction_set,
                    compiler_filter,
                    debuggable,
                    boot_complete,
                    reference_profile_fd.get(),
                    shared_libraries);
...
    return 0;
}

最終調(diào)用dex2oat進(jìn)行編譯操作。

好了,3篇文章對(duì)PMS的安裝過(guò)程掃了個(gè)盲,非常多的細(xì)節(jié)沒(méi)有分析,只簡(jiǎn)單捋了個(gè)框架。想深入學(xué)習(xí)的可以看看這個(gè)系列,總結(jié)的還挺全面的:APK安裝流程詳解

最后站在巨人的肩膀上盜兩張圖來(lái)總結(jié)下:


安裝流程圖
整體架構(gòu)圖(與installd通信 8.0之前是socket,之后是binder)

參考:
https://blog.csdn.net/shuttlecheng/article/details/79018014

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容