/**
   * Performs dex-opt on the elements of {@code classPath}, if needed. We choose the instruction set
   * of the current runtime.
   */
  private static void performSystemServerDexOpt(String classPath) {
    final String[] classPathElements = classPath.split(":");
    final InstallerConnection installer = new InstallerConnection();
    installer.waitForConnection();
    final String instructionSet = VMRuntime.getRuntime().vmInstructionSet();

    try {
      for (String classPathElement : classPathElements) {
        final int dexoptNeeded =
            DexFile.getDexOptNeeded(classPathElement, "*", instructionSet, false /* defer */);
        if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
          installer.dexopt(
              classPathElement, Process.SYSTEM_UID, instructionSet, dexoptNeeded, 0 /*dexFlags*/);
        }
      }
    } catch (IOException ioe) {
      throw new RuntimeException("Error starting system_server", ioe);
    } finally {
      installer.disconnect();
    }
  }
  private int performDexOptLI(
      PackageParser.Package pkg,
      String[] targetInstructionSets,
      boolean forceDex,
      boolean defer,
      ArraySet<String> done) {
    final String[] instructionSets =
        targetInstructionSets != null
            ? targetInstructionSets
            : getAppDexInstructionSets(pkg.applicationInfo);

    if (done != null) {
      done.add(pkg.packageName);
      if (pkg.usesLibraries != null) {
        performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer, done);
      }
      if (pkg.usesOptionalLibraries != null) {
        performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer, done);
      }
    }

    if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) == 0) {
      return DEX_OPT_SKIPPED;
    }

    final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0;
    final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;

    final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
    boolean performedDexOpt = false;
    // There are three basic cases here:
    // 1.) we need to dexopt, either because we are forced or it is needed
    // 2.) we are deferring a needed dexopt
    // 3.) we are skipping an unneeded dexopt
    final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
    for (String dexCodeInstructionSet : dexCodeInstructionSets) {
      if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {
        continue;
      }

      for (String path : paths) {
        final int dexoptNeeded;
        if (forceDex) {
          dexoptNeeded = DexFile.DEX2OAT_NEEDED;
        } else {
          try {
            dexoptNeeded =
                DexFile.getDexOptNeeded(path, pkg.packageName, dexCodeInstructionSet, defer);
          } catch (IOException ioe) {
            Slog.w(TAG, "IOException reading apk: " + path, ioe);
            return DEX_OPT_FAILED;
          }
        }

        if (!forceDex && defer && dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
          // We're deciding to defer a needed dexopt. Don't bother dexopting for other
          // paths and instruction sets. We'll deal with them all together when we process
          // our list of deferred dexopts.
          addPackageForDeferredDexopt(pkg);
          return DEX_OPT_DEFERRED;
        }

        if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
          final String dexoptType;
          String oatDir = null;
          if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
            dexoptType = "dex2oat";
            try {
              oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);
            } catch (IOException ioe) {
              Slog.w(TAG, "Unable to create oatDir for package: " + pkg.packageName);
              return DEX_OPT_FAILED;
            }
          } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
            dexoptType = "patchoat";
          } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
            dexoptType = "self patchoat";
          } else {
            throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
          }

          Log.i(
              TAG,
              "Running dexopt ("
                  + dexoptType
                  + ") on: "
                  + path
                  + " pkg="
                  + pkg.applicationInfo.packageName
                  + " isa="
                  + dexCodeInstructionSet
                  + " vmSafeMode="
                  + vmSafeMode
                  + " debuggable="
                  + debuggable
                  + " oatDir = "
                  + oatDir);
          final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
          final int ret =
              mPackageManagerService.mInstaller.dexopt(
                  path,
                  sharedGid,
                  !pkg.isForwardLocked(),
                  pkg.packageName,
                  dexCodeInstructionSet,
                  dexoptNeeded,
                  vmSafeMode,
                  debuggable,
                  oatDir);

          // Dex2oat might fail due to compiler / verifier errors. We soldier on
          // regardless, and attempt to interpret the app as a safety net.
          if (ret == 0) {
            performedDexOpt = true;
          }
        }
      }

      // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
      // either have either succeeded dexopt, or have had getDexOptNeeded tell us
      // it isn't required. We therefore mark that this package doesn't need dexopt unless
      // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
      // it.
      pkg.mDexOptPerformed.add(dexCodeInstructionSet);
    }

    // If we've gotten here, we're sure that no error occurred and that we haven't
    // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
    // we've skipped all of them because they are up to date. In both cases this
    // package doesn't need dexopt any longer.
    return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;
  }