Add a checked exception to legacy dexopt code paths.

This exception helps us identify the code in PackageManager that will
eventually be replaced by ART Service. It's propagated to the point
where the code paths need to fork to call ArtManagerLocal instead.

This CL only adds TODO comments at those locations, unless supporting
ART Service is trivial.

This also fixes some cases where Exception is caught. It's an
antipattern because it matches both checked and unchecked exceptions,
so it hides unintended catching of checked exceptions. Instead catch
RuntimeException, along with a specific list of checked exceptions, if
any.

Test: Boot without dalvik.vm.useartservice=true
Test: m FrameworksMockingServicesTests
Bug: 251903639
Change-Id: I53758b8e4b135287e34b412ad1257e5f5fa58273
diff --git a/core/java/com/android/internal/util/FunctionalUtils.java b/core/java/com/android/internal/util/FunctionalUtils.java
index 91cc4b9..d64a474 100644
--- a/core/java/com/android/internal/util/FunctionalUtils.java
+++ b/core/java/com/android/internal/util/FunctionalUtils.java
@@ -248,6 +248,19 @@
     }
 
     /**
+     * A {@link Supplier} that allows the caller to specify a custom checked {@link Exception} that
+     * can be thrown by the implementer. This is usually used when proxying/wrapping calls between
+     * different classes.
+     *
+     * @param <Output> Method return type
+     * @param <ExceptionType> Checked exception type
+     */
+    @FunctionalInterface
+    public interface ThrowingCheckedSupplier<Output, ExceptionType extends Exception> {
+        Output get() throws ExceptionType;
+    }
+
+    /**
      * A {@link Consumer} that allows the caller to specify a custom checked {@link Exception} that
      * can be thrown by the implementer. This is usually used when proxying/wrapping calls between
      * different classes.
diff --git a/services/core/java/com/android/server/pm/AppDataHelper.java b/services/core/java/com/android/server/pm/AppDataHelper.java
index ed31187..abaff4a 100644
--- a/services/core/java/com/android/server/pm/AppDataHelper.java
+++ b/services/core/java/com/android/server/pm/AppDataHelper.java
@@ -18,6 +18,7 @@
 
 import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
 
+import static com.android.server.pm.DexOptHelper.useArtService;
 import static com.android.server.pm.PackageManagerService.TAG;
 import static com.android.server.pm.PackageManagerServiceUtils.logCriticalInfo;
 
@@ -45,6 +46,7 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.util.Preconditions;
 import com.android.server.SystemServerInitThreadPool;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.ArtManagerService;
 import com.android.server.pm.parsing.pkg.AndroidPackageUtils;
 import com.android.server.pm.pkg.AndroidPackage;
@@ -242,33 +244,39 @@
                 }
             }
 
-            // Prepare the application profiles only for upgrades and
-            // first boot (so that we don't repeat the same operation at
-            // each boot).
-            //
-            // We only have to cover the upgrade and first boot here
-            // because for app installs we prepare the profiles before
-            // invoking dexopt (in installPackageLI).
-            //
-            // We also have to cover non system users because we do not
-            // call the usual install package methods for them.
-            //
-            // NOTE: in order to speed up first boot time we only create
-            // the current profile and do not update the content of the
-            // reference profile. A system image should already be
-            // configured with the right profile keys and the profiles
-            // for the speed-profile prebuilds should already be copied.
-            // That's done in #performDexOptUpgrade.
-            //
-            // TODO(calin, mathieuc): We should use .dm files for
-            // prebuilds profiles instead of manually copying them in
-            // #performDexOptUpgrade. When we do that we should have a
-            // more granular check here and only update the existing
-            // profiles.
-            if (mPm.isDeviceUpgrading() || mPm.isFirstBoot()
-                    || (userId != UserHandle.USER_SYSTEM)) {
-                mArtManagerService.prepareAppProfiles(pkg, userId,
-                        /* updateReferenceProfileContent= */ false);
+            if (!useArtService()) { // ART Service handles this on demand instead.
+                // Prepare the application profiles only for upgrades and
+                // first boot (so that we don't repeat the same operation at
+                // each boot).
+                //
+                // We only have to cover the upgrade and first boot here
+                // because for app installs we prepare the profiles before
+                // invoking dexopt (in installPackageLI).
+                //
+                // We also have to cover non system users because we do not
+                // call the usual install package methods for them.
+                //
+                // NOTE: in order to speed up first boot time we only create
+                // the current profile and do not update the content of the
+                // reference profile. A system image should already be
+                // configured with the right profile keys and the profiles
+                // for the speed-profile prebuilds should already be copied.
+                // That's done in #performDexOptUpgrade.
+                //
+                // TODO(calin, mathieuc): We should use .dm files for
+                // prebuilds profiles instead of manually copying them in
+                // #performDexOptUpgrade. When we do that we should have a
+                // more granular check here and only update the existing
+                // profiles.
+                if (mPm.isDeviceUpgrading() || mPm.isFirstBoot()
+                        || (userId != UserHandle.USER_SYSTEM)) {
+                    try {
+                        mArtManagerService.prepareAppProfiles(pkg, userId,
+                                /* updateReferenceProfileContent= */ false);
+                    } catch (LegacyDexoptDisabledException e2) {
+                        throw new RuntimeException(e2);
+                    }
+                }
             }
 
             if ((flags & StorageManager.FLAG_STORAGE_CE) != 0 && ceDataInode != -1) {
@@ -578,7 +586,12 @@
             Slog.wtf(TAG, "Package was null!", new Throwable());
             return;
         }
-        mArtManagerService.clearAppProfiles(pkg);
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            mArtManagerService.clearAppProfiles(pkg);
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     public void destroyAppDataLIF(AndroidPackage pkg, int userId, int flags) {
@@ -615,8 +628,11 @@
     }
 
     private void destroyAppProfilesLeafLIF(AndroidPackage pkg) {
+        // TODO(b/251903639): Call into ART Service.
         try {
             mInstaller.destroyAppProfiles(pkg.getPackageName());
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
         } catch (Installer.InstallerException e) {
             Slog.w(TAG, String.valueOf(e));
         }
diff --git a/services/core/java/com/android/server/pm/BackgroundDexOptService.java b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
index fe122f8..9785d47 100644
--- a/services/core/java/com/android/server/pm/BackgroundDexOptService.java
+++ b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
@@ -55,9 +55,11 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FrameworkStatsLog;
+import com.android.internal.util.FunctionalUtils.ThrowingCheckedSupplier;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.LocalServices;
 import com.android.server.PinnerService;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.PackageDexOptimizer.DexOptResult;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.dex.DexoptOptions;
@@ -71,7 +73,6 @@
 import java.util.List;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Supplier;
 
 /**
  * Controls background dex optimization run as idle job or command line.
@@ -202,7 +203,8 @@
     }
 
     /** Start scheduling job after boot completion */
-    public void systemReady() {
+    public void systemReady() throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         if (mInjector.isBackgroundDexOptDisabled()) {
             return;
         }
@@ -272,7 +274,8 @@
      * @return true if dex optimization is complete. false if the task is cancelled or if there was
      *         an error.
      */
-    public boolean runBackgroundDexoptJob(@Nullable List<String> packageNames) {
+    public boolean runBackgroundDexoptJob(@Nullable List<String> packageNames)
+            throws LegacyDexoptDisabledException {
         enforceRootOrShell();
         long identity = Binder.clearCallingIdentity();
         try {
@@ -301,7 +304,8 @@
      *
      * <p>This is only for shell command and only root or shell user can use this.
      */
-    public void cancelBackgroundDexoptJob() {
+    public void cancelBackgroundDexoptJob() throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         enforceRootOrShell();
         Binder.withCleanCallingIdentity(() -> cancelDexOptAndWaitForCompletion());
     }
@@ -315,7 +319,8 @@
      *
      * @param disable True if JobScheduler invocations should be disabled, false otherwise.
      */
-    public void setDisableJobSchedulerJobs(boolean disable) {
+    public void setDisableJobSchedulerJobs(boolean disable) throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         enforceRootOrShell();
         synchronized (mLock) {
             mDisableJobSchedulerJobs = disable;
@@ -323,14 +328,18 @@
     }
 
     /** Adds listener for package update */
-    public void addPackagesUpdatedListener(PackagesUpdatedListener listener) {
+    public void addPackagesUpdatedListener(PackagesUpdatedListener listener)
+            throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         synchronized (mLock) {
             mPackagesUpdatedListeners.add(listener);
         }
     }
 
     /** Removes package update listener */
-    public void removePackagesUpdatedListener(PackagesUpdatedListener listener) {
+    public void removePackagesUpdatedListener(PackagesUpdatedListener listener)
+            throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         synchronized (mLock) {
             mPackagesUpdatedListeners.remove(listener);
         }
@@ -340,7 +349,8 @@
      * Notifies package change and removes the package from the failed package list so that
      * the package can run dexopt again.
      */
-    public void notifyPackageChanged(String packageName) {
+    public void notifyPackageChanged(String packageName) throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         // The idle maintenance job skips packages which previously failed to
         // compile. The given package has changed and may successfully compile
         // now. Remove it from the list of known failing packages.
@@ -387,37 +397,43 @@
                 // Post boot job not finished yet. Run post boot job first.
                 return false;
             }
-            resetStatesForNewDexOptRunLocked(mInjector.createAndStartThread(
-                    "BackgroundDexOptService_" + (isPostBootUpdateJob ? "PostBoot" : "Idle"),
-                    () -> {
-                        TimingsTraceAndSlog tr =
-                                new TimingsTraceAndSlog(TAG, Trace.TRACE_TAG_PACKAGE_MANAGER);
-                        tr.traceBegin("jobExecution");
-                        boolean completed = false;
-                        try {
-                            completed = runIdleOptimization(
-                                    pm, pkgs, params.getJobId() == JOB_POST_BOOT_UPDATE);
-                        } finally { // Those cleanup should be done always.
-                            tr.traceEnd();
-                            Slog.i(TAG,
-                                    "dexopt finishing. jobid:" + params.getJobId()
-                                            + " completed:" + completed);
+            try {
+                resetStatesForNewDexOptRunLocked(mInjector.createAndStartThread(
+                        "BackgroundDexOptService_" + (isPostBootUpdateJob ? "PostBoot" : "Idle"),
+                        () -> {
+                            TimingsTraceAndSlog tr =
+                                    new TimingsTraceAndSlog(TAG, Trace.TRACE_TAG_PACKAGE_MANAGER);
+                            tr.traceBegin("jobExecution");
+                            boolean completed = false;
+                            try {
+                                completed = runIdleOptimization(
+                                        pm, pkgs, params.getJobId() == JOB_POST_BOOT_UPDATE);
+                            } catch (LegacyDexoptDisabledException e) {
+                                Slog.wtf(TAG, e);
+                            } finally { // Those cleanup should be done always.
+                                tr.traceEnd();
+                                Slog.i(TAG,
+                                        "dexopt finishing. jobid:" + params.getJobId()
+                                                + " completed:" + completed);
 
-                            writeStatsLog(params);
+                                writeStatsLog(params);
 
-                            if (params.getJobId() == JOB_POST_BOOT_UPDATE) {
-                                if (completed) {
-                                    markPostBootUpdateCompleted(params);
+                                if (params.getJobId() == JOB_POST_BOOT_UPDATE) {
+                                    if (completed) {
+                                        markPostBootUpdateCompleted(params);
+                                    }
+                                    // Reschedule when cancelled
+                                    job.jobFinished(params, !completed);
+                                } else {
+                                    // Periodic job
+                                    job.jobFinished(params, false /* reschedule */);
                                 }
-                                // Reschedule when cancelled
-                                job.jobFinished(params, !completed);
-                            } else {
-                                // Periodic job
-                                job.jobFinished(params, false /* reschedule */);
+                                markDexOptCompleted();
                             }
-                            markDexOptCompleted();
-                        }
-                    }));
+                        }));
+            } catch (LegacyDexoptDisabledException e) {
+                Slog.wtf(TAG, e);
+            }
         }
         return true;
     }
@@ -425,11 +441,16 @@
     /** For BackgroundDexOptJobService to dispatch onStopJob event */
     /* package */ boolean onStopJob(BackgroundDexOptJobService job, JobParameters params) {
         Slog.i(TAG, "onStopJob:" + params.getJobId());
-        // This cannot block as it is in main thread, thus dispatch to a newly created thread and
-        // cancel it from there.
-        // As this event does not happen often, creating a new thread is justified rather than
-        // having one thread kept permanently.
-        mInjector.createAndStartThread("DexOptCancel", this::cancelDexOptAndWaitForCompletion);
+        // This cannot block as it is in main thread, thus dispatch to a newly created thread
+        // and cancel it from there. As this event does not happen often, creating a new thread
+        // is justified rather than having one thread kept permanently.
+        mInjector.createAndStartThread("DexOptCancel", () -> {
+            try {
+                cancelDexOptAndWaitForCompletion();
+            } catch (LegacyDexoptDisabledException e) {
+                Slog.wtf(TAG, e);
+            }
+        });
         // Always reschedule for cancellation.
         return true;
     }
@@ -438,7 +459,7 @@
      * Cancels pending dexopt and wait for completion of the cancellation. This can block the caller
      * until cancellation is done.
      */
-    private void cancelDexOptAndWaitForCompletion() {
+    private void cancelDexOptAndWaitForCompletion() throws LegacyDexoptDisabledException {
         synchronized (mLock) {
             if (mDexOptThread == null) {
                 return;
@@ -496,7 +517,8 @@
     }
 
     @GuardedBy("mLock")
-    private void resetStatesForNewDexOptRunLocked(Thread thread) {
+    private void resetStatesForNewDexOptRunLocked(Thread thread)
+            throws LegacyDexoptDisabledException {
         mDexOptThread = thread;
         mLastCancelledPackages.clear();
         controlDexOptBlockingLocked(false);
@@ -510,7 +532,7 @@
     }
 
     @GuardedBy("mLock")
-    private void controlDexOptBlockingLocked(boolean block) {
+    private void controlDexOptBlockingLocked(boolean block) throws LegacyDexoptDisabledException {
         PackageManagerService pm = mInjector.getPackageManagerService();
         mDexOptHelper.controlDexOptBlocking(block);
     }
@@ -564,8 +586,8 @@
      * Returns whether we've successfully run the job. Note that it will return true even if some
      * packages may have failed compiling.
      */
-    private boolean runIdleOptimization(
-            PackageManagerService pm, List<String> pkgs, boolean isPostBootUpdate) {
+    private boolean runIdleOptimization(PackageManagerService pm, List<String> pkgs,
+            boolean isPostBootUpdate) throws LegacyDexoptDisabledException {
         synchronized (mLock) {
             mLastExecutionStatus = STATUS_UNSPECIFIED;
             mLastExecutionStartUptimeMs = SystemClock.uptimeMillis();
@@ -631,7 +653,8 @@
 
     @Status
     private int idleOptimizePackages(PackageManagerService pm, List<String> pkgs,
-            long lowStorageThreshold, boolean isPostBootUpdate) {
+            long lowStorageThreshold, boolean isPostBootUpdate)
+            throws LegacyDexoptDisabledException {
         ArraySet<String> updatedPackages = new ArraySet<>();
 
         try {
@@ -707,7 +730,8 @@
 
     @Status
     private int optimizePackages(List<String> pkgs, long lowStorageThreshold,
-            ArraySet<String> updatedPackages, boolean isPostBootUpdate) {
+            ArraySet<String> updatedPackages, boolean isPostBootUpdate)
+            throws LegacyDexoptDisabledException {
         boolean supportSecondaryDex = mInjector.supportSecondaryDex();
 
         // Keep the error if there is any error from any package.
@@ -760,7 +784,8 @@
      */
     @DexOptResult
     private int downgradePackage(@NonNull Computer snapshot, PackageManagerService pm, String pkg,
-            boolean isForPrimaryDex, boolean isPostBootUpdate) {
+            boolean isForPrimaryDex, boolean isPostBootUpdate)
+            throws LegacyDexoptDisabledException {
         if (DEBUG) {
             Slog.d(TAG, "Downgrading " + pkg);
         }
@@ -808,7 +833,7 @@
     }
 
     @Status
-    private int reconcileSecondaryDexFiles() {
+    private int reconcileSecondaryDexFiles() throws LegacyDexoptDisabledException {
         // TODO(calin): should we denylist packages for which we fail to reconcile?
         for (String p : mInjector.getDexManager().getAllPackagesWithSecondaryDexFiles()) {
             if (isCancelling()) {
@@ -829,7 +854,8 @@
      * @return PackageDexOptimizer#DEX_OPT_*
      */
     @DexOptResult
-    private int optimizePackage(String pkg, boolean isForPrimaryDex, boolean isPostBootUpdate) {
+    private int optimizePackage(String pkg, boolean isForPrimaryDex, boolean isPostBootUpdate)
+            throws LegacyDexoptDisabledException {
         int reason = isPostBootUpdate ? PackageManagerService.REASON_POST_BOOT
                                       : PackageManagerService.REASON_BACKGROUND_DEXOPT;
         String filter = getCompilerFilterForReason(reason);
@@ -857,7 +883,8 @@
     }
 
     @DexOptResult
-    private int performDexOptPrimary(String pkg, int reason, String filter, int dexoptFlags) {
+    private int performDexOptPrimary(String pkg, int reason, String filter, int dexoptFlags)
+            throws LegacyDexoptDisabledException {
         DexoptOptions dexoptOptions =
                 new DexoptOptions(pkg, reason, filter, /*splitName=*/null, dexoptFlags);
         return trackPerformDexOpt(pkg, /*isForPrimaryDex=*/true,
@@ -865,7 +892,8 @@
     }
 
     @DexOptResult
-    private int performDexOptSecondary(String pkg, int reason, String filter, int dexoptFlags) {
+    private int performDexOptSecondary(String pkg, int reason, String filter, int dexoptFlags)
+            throws LegacyDexoptDisabledException {
         DexoptOptions dexoptOptions = new DexoptOptions(pkg, reason, filter, /*splitName=*/null,
                 dexoptFlags | DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX);
         return trackPerformDexOpt(pkg, /*isForPrimaryDex=*/false,
@@ -885,8 +913,9 @@
      *  {@link PackageDexOptimizer#DEX_OPT_FAILED}
      */
     @DexOptResult
-    private int trackPerformDexOpt(
-            String pkg, boolean isForPrimaryDex, Supplier<Integer> performDexOptWrapper) {
+    private int trackPerformDexOpt(String pkg, boolean isForPrimaryDex,
+            ThrowingCheckedSupplier<Integer, LegacyDexoptDisabledException> performDexOptWrapper)
+            throws LegacyDexoptDisabledException {
         ArraySet<String> failedPackageNames;
         synchronized (mLock) {
             failedPackageNames =
diff --git a/services/core/java/com/android/server/pm/ComputerEngine.java b/services/core/java/com/android/server/pm/ComputerEngine.java
index 0a59c19..6d3366c 100644
--- a/services/core/java/com/android/server/pm/ComputerEngine.java
+++ b/services/core/java/com/android/server/pm/ComputerEngine.java
@@ -124,6 +124,7 @@
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
 import com.android.modules.utils.TypedXmlSerializer;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.dex.PackageDexUsage;
 import com.android.server.pm.parsing.PackageInfoUtils;
@@ -3009,8 +3010,13 @@
                     ipw.println("[" + pkgName + "]");
                     ipw.increaseIndent();
 
-                    mPackageDexOptimizer.dumpDexoptState(ipw, pkg, pkgSetting,
-                            mDexManager.getPackageUseInfoOrDefault(pkgName));
+                    // TODO(b/251903639): Call into ART Service.
+                    try {
+                        mPackageDexOptimizer.dumpDexoptState(ipw, pkg, pkgSetting,
+                                mDexManager.getPackageUseInfoOrDefault(pkgName));
+                    } catch (LegacyDexoptDisabledException e) {
+                        throw new RuntimeException(e);
+                    }
                     ipw.decreaseIndent();
                 }
                 ipw.println("BgDexopt state:");
diff --git a/services/core/java/com/android/server/pm/DexOptHelper.java b/services/core/java/com/android/server/pm/DexOptHelper.java
index 69aaa0d..39aa9c1 100644
--- a/services/core/java/com/android/server/pm/DexOptHelper.java
+++ b/services/core/java/com/android/server/pm/DexOptHelper.java
@@ -66,6 +66,8 @@
 import com.android.server.art.model.ArtFlags;
 import com.android.server.art.model.OptimizeParams;
 import com.android.server.art.model.OptimizeResult;
+import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.PackageDexOptimizer.DexOptResult;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.dex.DexoptOptions;
@@ -125,8 +127,9 @@
      * and {@code numberOfPackagesFailed}.
      */
     public int[] performDexOptUpgrade(List<PackageStateInternal> packageStates, boolean showDialog,
-            final int compilationReason, boolean bootComplete) {
-
+            final int compilationReason, boolean bootComplete)
+            throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         int numberOfPackagesVisited = 0;
         int numberOfPackagesOptimized = 0;
         int numberOfPackagesSkipped = 0;
@@ -158,7 +161,7 @@
                             // even if things are already compiled.
                             // useProfileForDexopt = true;
                         }
-                    } catch (Exception e) {
+                    } catch (InstallerException | RuntimeException e) {
                         Log.e(TAG, "Failed to copy profile " + profileFile.getAbsolutePath() + " ",
                                 e);
                     }
@@ -193,7 +196,7 @@
                                 } else {
                                     useProfileForDexopt = true;
                                 }
-                            } catch (Exception e) {
+                            } catch (InstallerException | RuntimeException e) {
                                 Log.e(TAG, "Failed to copy profile "
                                         + profileFile.getAbsolutePath() + " ", e);
                             }
@@ -284,7 +287,8 @@
      * Checks if system UI package (typically "com.android.systemui") needs to be re-compiled, and
      * compiles it if needed.
      */
-    private void checkAndDexOptSystemUi() {
+    private void checkAndDexOptSystemUi() throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         Computer snapshot = mPm.snapshotComputer();
         String sysUiPackageName =
                 mPm.mContext.getString(com.android.internal.R.string.config_systemUi);
@@ -320,7 +324,7 @@
                             Log.e(TAG, "Failed to copy profile " + profileFile.getAbsolutePath());
                         }
                     }
-                } catch (Exception e) {
+                } catch (InstallerException | RuntimeException e) {
                     Log.e(TAG, "Failed to copy profile " + profileFile.getAbsolutePath(), e);
                 }
             }
@@ -341,7 +345,7 @@
     }
 
     @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG)
-    public void performPackageDexOptUpgradeIfNeeded() {
+    public void performPackageDexOptUpgradeIfNeeded() throws LegacyDexoptDisabledException {
         PackageManagerServiceUtils.enforceSystemOrRoot(
                 "Only the system can request package update");
 
@@ -413,7 +417,12 @@
         }
 
         if (options.isDexoptOnlySecondaryDex()) {
-            return mPm.getDexManager().dexoptSecondaryDex(options);
+            // TODO(b/251903639): Call into ART Service.
+            try {
+                return mPm.getDexManager().dexoptSecondaryDex(options);
+            } catch (LegacyDexoptDisabledException e) {
+                throw new RuntimeException(e);
+            }
         } else {
             int dexoptStatus = performDexOptWithStatus(options);
             return dexoptStatus != PackageDexOptimizer.DEX_OPT_FAILED;
@@ -470,6 +479,8 @@
         final long callingId = Binder.clearCallingIdentity();
         try {
             return performDexOptInternalWithDependenciesLI(p, pkgSetting, options);
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
         } finally {
             Binder.restoreCallingIdentity(callingId);
         }
@@ -542,7 +553,8 @@
 
     @DexOptResult
     private int performDexOptInternalWithDependenciesLI(
-            AndroidPackage p, @NonNull PackageStateInternal pkgSetting, DexoptOptions options) {
+            AndroidPackage p, @NonNull PackageStateInternal pkgSetting, DexoptOptions options)
+            throws LegacyDexoptDisabledException {
         // System server gets a special path.
         if (PLATFORM_PACKAGE_NAME.equals(p.getPackageName())) {
             return mPm.getDexManager().dexoptSystemServer(options);
@@ -621,7 +633,11 @@
         if (artSrvRes.isPresent()) {
             res = artSrvRes.get();
         } else {
-            res = performDexOptInternalWithDependenciesLI(pkg, packageState, options);
+            try {
+                res = performDexOptInternalWithDependenciesLI(pkg, packageState, options);
+            } catch (LegacyDexoptDisabledException e) {
+                throw new RuntimeException(e);
+            }
         }
 
         Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
@@ -666,8 +682,8 @@
         return installerPkg.getUid() == Binder.getCallingUid();
     }
 
-    public boolean performDexOptSecondary(String packageName, String compilerFilter,
-            boolean force) {
+    public boolean performDexOptSecondary(
+            String packageName, String compilerFilter, boolean force) {
         int flags = DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX
                 | DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES
                 | DexoptOptions.DEXOPT_BOOT_COMPLETE
@@ -869,7 +885,7 @@
         }
     }
 
-    /*package*/ void controlDexOptBlocking(boolean block) {
+    /*package*/ void controlDexOptBlocking(boolean block) throws LegacyDexoptDisabledException {
         mPm.mPackageDexOptimizer.controlDexOptBlocking(block);
     }
 
@@ -929,10 +945,17 @@
     }
 
     /**
+     * Returns true if ART Service should be used for package optimization.
+     */
+    public static boolean useArtService() {
+        return SystemProperties.getBoolean("dalvik.vm.useartservice", false);
+    }
+
+    /**
      * Returns {@link DexUseManagerLocal} if ART Service should be used for package optimization.
      */
     public static @Nullable DexUseManagerLocal getDexUseManagerLocal() {
-        if (!"true".equals(SystemProperties.get("dalvik.vm.useartservice", ""))) {
+        if (!useArtService()) {
             return null;
         }
         try {
@@ -946,7 +969,7 @@
      * Returns {@link ArtManagerLocal} if ART Service should be used for package optimization.
      */
     private static @Nullable ArtManagerLocal getArtManagerLocal() {
-        if (!"true".equals(SystemProperties.get("dalvik.vm.useartservice", ""))) {
+        if (!useArtService()) {
             return null;
         }
         try {
diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java
index 16bf0fe..6f7484e 100644
--- a/services/core/java/com/android/server/pm/InstallPackageHelper.java
+++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java
@@ -46,6 +46,7 @@
 import static android.os.storage.StorageManager.FLAG_STORAGE_DE;
 import static android.os.storage.StorageManager.FLAG_STORAGE_EXTERNAL;
 
+import static com.android.server.pm.DexOptHelper.useArtService;
 import static com.android.server.pm.InstructionSets.getAppDexInstructionSets;
 import static com.android.server.pm.InstructionSets.getDexCodeInstructionSet;
 import static com.android.server.pm.InstructionSets.getPreferredInstructionSet;
@@ -157,6 +158,7 @@
 import com.android.internal.util.CollectionUtils;
 import com.android.internal.util.FrameworkStatsLog;
 import com.android.server.EventLogTags;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.ArtManagerService;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.dex.DexoptOptions;
@@ -261,7 +263,8 @@
         final PackageSetting oldPkgSetting = request.getScanRequestOldPackageSetting();
         final PackageSetting originalPkgSetting = request.getScanRequestOriginalPackageSetting();
         final String realPkgName = request.getRealPackageName();
-        final List<String> changedAbiCodePath = request.getChangedAbiCodePath();
+        final List<String> changedAbiCodePath =
+                useArtService() ? null : request.getChangedAbiCodePath();
         final PackageSetting pkgSetting;
         if (request.getScanRequestPackageSetting() != null) {
             SharedUserSetting requestSharedUserSetting = mPm.mSettings.getSharedUserSettingLPr(
@@ -362,6 +365,8 @@
         }
         pkgSetting.setSigningDetails(reconciledPkg.mSigningDetails);
 
+        // The conditional on useArtService() for changedAbiCodePath above means this is skipped
+        // when ART Service is in use, since it has its own dex file GC.
         if (changedAbiCodePath != null && changedAbiCodePath.size() > 0) {
             for (int i = changedAbiCodePath.size() - 1; i >= 0; --i) {
                 final String codePathString = changedAbiCodePath.get(i);
@@ -370,6 +375,8 @@
                         mPm.mInstaller.rmdex(codePathString,
                                 getDexCodeInstructionSet(getPreferredInstructionSet()));
                     }
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
                 } catch (Installer.InstallerException ignored) {
                 }
             }
@@ -2282,12 +2289,18 @@
                         pkg.getBaseApkPath(), pkg.getSplitCodePaths());
             }
 
-            // Prepare the application profiles for the new code paths.
-            // This needs to be done before invoking dexopt so that any install-time profile
-            // can be used for optimizations.
-            mArtManagerService.prepareAppProfiles(
-                    pkg, mPm.resolveUserIds(installRequest.getUserId()),
-                    /* updateReferenceProfileContent= */ true);
+            if (!useArtService()) { // ART Service handles this on demand instead.
+                // Prepare the application profiles for the new code paths.
+                // This needs to be done before invoking dexopt so that any install-time profile
+                // can be used for optimizations.
+                try {
+                    mArtManagerService.prepareAppProfiles(pkg,
+                            mPm.resolveUserIds(installRequest.getUserId()),
+                            /* updateReferenceProfileContent= */ true);
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
+            }
 
             // Compute the compilation reason from the installation scenario.
             final int compilationReason =
@@ -2365,19 +2378,30 @@
 
                 realPkgSetting.getPkgState().setUpdatedSystemApp(isUpdatedSystemApp);
 
-                mPackageDexOptimizer.performDexOpt(pkg, realPkgSetting,
-                        null /* instructionSets */,
-                        mPm.getOrCreateCompilerPackageStats(pkg),
-                        mDexManager.getPackageUseInfoOrDefault(packageName),
-                        dexoptOptions);
+                // TODO(b/251903639): Call into ART Service.
+                try {
+                    mPackageDexOptimizer.performDexOpt(pkg, realPkgSetting,
+                            null /* instructionSets */, mPm.getOrCreateCompilerPackageStats(pkg),
+                            mDexManager.getPackageUseInfoOrDefault(packageName), dexoptOptions);
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
                 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
             }
 
-            // Notify BackgroundDexOptService that the package has been changed.
-            // If this is an update of a package which used to fail to compile,
-            // BackgroundDexOptService will remove it from its denylist.
-            // TODO: Layering violation
-            BackgroundDexOptService.getService().notifyPackageChanged(packageName);
+            if (!useArtService()) {
+                // Notify BackgroundDexOptService that the package has been changed.
+                // If this is an update of a package which used to fail to compile,
+                // BackgroundDexOptService will remove it from its denylist.
+                // ART Service currently doesn't support this and will retry packages in every
+                // background dexopt.
+                // TODO: Layering violation
+                try {
+                    BackgroundDexOptService.getService().notifyPackageChanged(packageName);
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
+            }
         }
         PackageManagerServiceUtils.waitForNativeBinariesExtractionForIncremental(
                 incrementalStorages);
diff --git a/services/core/java/com/android/server/pm/Installer.java b/services/core/java/com/android/server/pm/Installer.java
index a078b5d..9329f06 100644
--- a/services/core/java/com/android/server/pm/Installer.java
+++ b/services/core/java/com/android/server/pm/Installer.java
@@ -16,6 +16,8 @@
 
 package com.android.server.pm;
 
+import static com.android.server.pm.DexOptHelper.useArtService;
+
 import android.annotation.AppIdInt;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
@@ -609,11 +611,13 @@
      * @throws InstallerException if {@code dexopt} fails.
      */
     public boolean dexopt(String apkPath, int uid, String pkgName, String instructionSet,
-            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
-            String compilerFilter, @Nullable String volumeUuid, @Nullable String classLoaderContext,
+            int dexoptNeeded, @Nullable String outputPath, int dexFlags, String compilerFilter,
+            @Nullable String volumeUuid, @Nullable String classLoaderContext,
             @Nullable String seInfo, boolean downgrade, int targetSdkVersion,
             @Nullable String profileName, @Nullable String dexMetadataPath,
-            @Nullable String compilationReason) throws InstallerException {
+            @Nullable String compilationReason)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         assertValidInstructionSet(instructionSet);
         BlockGuard.getVmPolicy().onPathAccess(apkPath);
         BlockGuard.getVmPolicy().onPathAccess(outputPath);
@@ -637,7 +641,8 @@
      *
      * @param block set to true to enable blocking / false to disable blocking.
      */
-    public void controlDexOptBlocking(boolean block) {
+    public void controlDexOptBlocking(boolean block) throws LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         try {
             mInstalld.controlDexOptBlocking(block);
         } catch (Exception e) {
@@ -655,7 +660,8 @@
      *         {@link #PROFILE_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES}
      */
     public int mergeProfiles(int uid, String packageName, String profileName)
-            throws InstallerException {
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return PROFILE_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA;
         try {
             return mInstalld.mergeProfiles(uid, packageName, profileName);
@@ -668,8 +674,9 @@
      * Dumps profiles associated with a package in a human readable format.
      */
     public boolean dumpProfiles(int uid, String packageName, String profileName, String codePath,
-                                boolean dumpClassesAndMethods)
-            throws InstallerException {
+            boolean dumpClassesAndMethods)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return false;
         BlockGuard.getVmPolicy().onPathAccess(codePath);
         try {
@@ -681,7 +688,8 @@
     }
 
     public boolean copySystemProfile(String systemProfile, int uid, String packageName,
-                String profileName) throws InstallerException {
+            String profileName) throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return false;
         try {
             return mInstalld.copySystemProfile(systemProfile, uid, packageName, profileName);
@@ -690,7 +698,9 @@
         }
     }
 
-    public void rmdex(String codePath, String instructionSet) throws InstallerException {
+    public void rmdex(String codePath, String instructionSet)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         assertValidInstructionSet(instructionSet);
         if (!checkBeforeRemote()) return;
         BlockGuard.getVmPolicy().onPathAccess(codePath);
@@ -714,7 +724,9 @@
         }
     }
 
-    public void clearAppProfiles(String packageName, String profileName) throws InstallerException {
+    public void clearAppProfiles(String packageName, String profileName)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return;
         try {
             mInstalld.clearAppProfiles(packageName, profileName);
@@ -723,7 +735,9 @@
         }
     }
 
-    public void destroyAppProfiles(String packageName) throws InstallerException {
+    public void destroyAppProfiles(String packageName)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return;
         try {
             mInstalld.destroyAppProfiles(packageName);
@@ -737,7 +751,8 @@
      * @throws InstallerException if the deletion fails.
      */
     public void deleteReferenceProfile(String packageName, String profileName)
-            throws InstallerException {
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return;
         try {
             mInstalld.deleteReferenceProfile(packageName, profileName);
@@ -799,7 +814,8 @@
      * Creates an oat dir for given package and instruction set.
      */
     public void createOatDir(String packageName, String oatDir, String dexInstructionSet)
-            throws InstallerException {
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return;
         try {
             mInstalld.createOatDir(packageName, oatDir, dexInstructionSet);
@@ -843,7 +859,8 @@
      * of freed bytes.
      */
     public long deleteOdex(String packageName, String apkPath, String instructionSet,
-            String outputPath) throws InstallerException {
+            String outputPath) throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return -1;
         BlockGuard.getVmPolicy().onPathAccess(apkPath);
         BlockGuard.getVmPolicy().onPathAccess(outputPath);
@@ -855,7 +872,9 @@
     }
 
     public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,
-            String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException {
+            String[] isas, @Nullable String volumeUuid, int flags)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         for (int i = 0; i < isas.length; i++) {
             assertValidInstructionSet(isas[i]);
         }
@@ -881,7 +900,8 @@
     }
 
     public boolean createProfileSnapshot(int appId, String packageName, String profileName,
-            String classpath) throws InstallerException {
+            String classpath) throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return false;
         try {
             return mInstalld.createProfileSnapshot(appId, packageName, profileName, classpath);
@@ -891,7 +911,8 @@
     }
 
     public void destroyProfileSnapshot(String packageName, String profileName)
-            throws InstallerException {
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return;
         try {
             mInstalld.destroyProfileSnapshot(packageName, profileName);
@@ -952,7 +973,9 @@
      * </ul>
      */
     public boolean prepareAppProfile(String pkg, @UserIdInt int userId, @AppIdInt int appId,
-            String profileName, String codePath, String dexMetadataPath) throws InstallerException {
+            String profileName, String codePath, String dexMetadataPath)
+            throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return false;
         BlockGuard.getVmPolicy().onPathAccess(codePath);
         BlockGuard.getVmPolicy().onPathAccess(dexMetadataPath);
@@ -1117,7 +1140,8 @@
      * @throws InstallerException if failed to get the visibility of the optimized artifacts.
      */
     public int getOdexVisibility(String packageName, String apkPath, String instructionSet,
-            String outputPath) throws InstallerException {
+            String outputPath) throws InstallerException, LegacyDexoptDisabledException {
+        checkLegacyDexoptDisabled();
         if (!checkBeforeRemote()) return -1;
         BlockGuard.getVmPolicy().onPathAccess(apkPath);
         BlockGuard.getVmPolicy().onPathAccess(outputPath);
@@ -1137,4 +1161,26 @@
             throw new InstallerException(e.toString());
         }
     }
+
+    /**
+     * A checked exception that is thrown in legacy dexopt code paths when ART Service should be
+     * used instead.
+     */
+    public static class LegacyDexoptDisabledException extends Exception {
+        // TODO(b/260124949): Remove the legacy dexopt code paths, i.e. this exception and all code
+        // that may throw it.
+        public LegacyDexoptDisabledException() {
+            super("Invalid call to legacy dexopt installd method while ART Service is in use.");
+        }
+    }
+
+    /**
+     * Throws LegacyDexoptDisabledException if ART Service should be used instead of the
+     * {@link android.os.IInstalld} method that follows this method call.
+     */
+    public static void checkLegacyDexoptDisabled() throws LegacyDexoptDisabledException {
+        if (useArtService()) {
+            throw new LegacyDexoptDisabledException();
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/pm/OtaDexoptService.java b/services/core/java/com/android/server/pm/OtaDexoptService.java
index 56f2493..490b2a9 100644
--- a/services/core/java/com/android/server/pm/OtaDexoptService.java
+++ b/services/core/java/com/android/server/pm/OtaDexoptService.java
@@ -37,6 +37,7 @@
 import com.android.internal.logging.MetricsLogger;
 import com.android.server.LocalServices;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.DexoptOptions;
 import com.android.server.pm.parsing.pkg.AndroidPackageUtils;
 import com.android.server.pm.pkg.AndroidPackage;
@@ -167,7 +168,13 @@
             Log.i(TAG, "Low on space, deleting oat files in an attempt to free up space: "
                     + DexOptHelper.packagesToString(others));
             for (PackageStateInternal pkg : others) {
-                mPackageManagerService.deleteOatArtifactsOfPackage(snapshot, pkg.getPackageName());
+                // TODO(b/251903639): Call into ART Service.
+                try {
+                    mPackageManagerService.deleteOatArtifactsOfPackage(
+                            snapshot, pkg.getPackageName());
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
             }
         }
         long spaceAvailableNow = getAvailableSpace();
@@ -188,7 +195,7 @@
                             + pkgSetting.getTransientState()
                             .getLatestForegroundPackageUseTimeInMills());
                 }
-            } catch (Exception ignored) {
+            } catch (RuntimeException ignored) {
             }
         }
     }
@@ -352,13 +359,17 @@
         PackageDexOptimizer optimizer = new OTADexoptPackageDexOptimizer(
                 collectingInstaller, mPackageManagerService.mInstallLock, mContext);
 
-        optimizer.performDexOpt(pkg, pkgSetting,
-                null /* ISAs */,
-                null /* CompilerStats.PackageStats */,
-                mPackageManagerService.getDexManager().getPackageUseInfoOrDefault(
-                        pkg.getPackageName()),
-                new DexoptOptions(pkg.getPackageName(), compilationReason,
-                        DexoptOptions.DEXOPT_BOOT_COMPLETE));
+        // TODO(b/251903639): Allow this use of legacy dexopt code even when ART Service is enabled.
+        try {
+            optimizer.performDexOpt(pkg, pkgSetting, null /* ISAs */,
+                    null /* CompilerStats.PackageStats */,
+                    mPackageManagerService.getDexManager().getPackageUseInfoOrDefault(
+                            pkg.getPackageName()),
+                    new DexoptOptions(pkg.getPackageName(), compilationReason,
+                            DexoptOptions.DEXOPT_BOOT_COMPLETE));
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
 
         return commands;
     }
diff --git a/services/core/java/com/android/server/pm/PackageDexOptimizer.java b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
index aaf8755..0a71892 100644
--- a/services/core/java/com/android/server/pm/PackageDexOptimizer.java
+++ b/services/core/java/com/android/server/pm/PackageDexOptimizer.java
@@ -71,6 +71,7 @@
 import com.android.server.LocalServices;
 import com.android.server.apphibernation.AppHibernationManagerInternal;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.ArtManagerService;
 import com.android.server.pm.dex.ArtStatsLogUtils;
 import com.android.server.pm.dex.ArtStatsLogUtils.ArtStatsLogger;
@@ -219,7 +220,8 @@
     @DexOptResult
     int performDexOpt(AndroidPackage pkg, @NonNull PackageStateInternal pkgSetting,
             String[] instructionSets, CompilerStats.PackageStats packageStats,
-            PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
+            PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options)
+            throws LegacyDexoptDisabledException {
         if (PLATFORM_PACKAGE_NAME.equals(pkg.getPackageName())) {
             throw new IllegalArgumentException("System server dexopting should be done via "
                     + " DexManager and PackageDexOptimizer#dexoptSystemServerPath");
@@ -245,7 +247,7 @@
     /**
      * Cancels currently running dex optimization.
      */
-    void controlDexOptBlocking(boolean block) {
+    void controlDexOptBlocking(boolean block) throws LegacyDexoptDisabledException {
         // This method should not hold mInstallLock as cancelling should be possible while
         // the lock is held by other thread running performDexOpt.
         getInstallerWithoutLock().controlDexOptBlocking(block);
@@ -259,7 +261,8 @@
     @DexOptResult
     private int performDexOptLI(AndroidPackage pkg, @NonNull PackageStateInternal pkgSetting,
             String[] targetInstructionSets, CompilerStats.PackageStats packageStats,
-            PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options) {
+            PackageDexUsage.PackageUseInfo packageUseInfo, DexoptOptions options)
+            throws LegacyDexoptDisabledException {
         // ClassLoader only refers non-native (jar) shared libraries and must ignore
         // native (so) shared libraries. See also LoadedApk#createSharedLibraryLoader().
         final List<SharedLibraryInfo> sharedLibraries = pkgSetting.getTransientState()
@@ -439,7 +442,7 @@
      */
     @GuardedBy("mInstallLock")
     private boolean prepareCloudProfile(AndroidPackage pkg, String profileName, String path,
-            @Nullable String dexMetadataPath) {
+            @Nullable String dexMetadataPath) throws LegacyDexoptDisabledException {
         if (dexMetadataPath != null) {
             try {
                 // Make sure we don't keep any existing contents.
@@ -472,7 +475,7 @@
             String path, String isa, String compilerFilter, int profileAnalysisResult,
             String classLoaderContext, int dexoptFlags, int uid,
             CompilerStats.PackageStats packageStats, boolean downgrade, String profileName,
-            String dexMetadataPath, int compilationReason) {
+            String dexMetadataPath, int compilationReason) throws LegacyDexoptDisabledException {
         String oatDir = getPackageOatDirIfSupported(pkgSetting, pkg);
 
         int dexoptNeeded = getDexoptNeeded(pkg.getPackageName(), path, isa, compilerFilter,
@@ -525,8 +528,8 @@
      */
     @GuardedBy("mInstallLock")
     @DexOptResult
-    public int dexoptSystemServerPath(
-            String dexPath, PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
+    public int dexoptSystemServerPath(String dexPath, PackageDexUsage.DexUseInfo dexUseInfo,
+            DexoptOptions options) throws LegacyDexoptDisabledException {
         int dexoptFlags = DEXOPT_PUBLIC
                 | (options.isBootComplete() ? DEXOPT_BOOTCOMPLETE : 0)
                 | (options.isDexoptIdleBackgroundJob() ? DEXOPT_IDLE_BACKGROUND_JOB : 0);
@@ -601,7 +604,8 @@
      */
     @DexOptResult
     public int dexOptSecondaryDexPath(ApplicationInfo info, String path,
-            PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
+            PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options)
+            throws LegacyDexoptDisabledException {
         if (info.uid == -1) {
             throw new IllegalArgumentException("Dexopt for path " + path + " has invalid uid.");
         }
@@ -643,7 +647,7 @@
                         + " time out. Operation took " + duration + " ms. Thread: "
                         + Thread.currentThread().getName());
             }
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             Slog.wtf(TAG, "Error while releasing " + mDexoptWakeLock.getTag() + " lock", e);
         }
     }
@@ -651,7 +655,8 @@
     @GuardedBy("mInstallLock")
     @DexOptResult
     private int dexOptSecondaryDexPathLI(ApplicationInfo info, String path,
-            PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options) {
+            PackageDexUsage.DexUseInfo dexUseInfo, DexoptOptions options)
+            throws LegacyDexoptDisabledException {
         String compilerFilter = getRealCompilerFilter(info, options.getCompilerFilter(),
                 dexUseInfo.isUsedByOtherApps());
         // Get the dexopt flags after getRealCompilerFilter to make sure we get the correct flags.
@@ -729,7 +734,8 @@
      * Dumps the dexopt state of the given package {@code pkg} to the given {@code PrintWriter}.
      */
     void dumpDexoptState(IndentingPrintWriter pw, AndroidPackage pkg,
-            PackageStateInternal pkgSetting, PackageDexUsage.PackageUseInfo useInfo) {
+            PackageStateInternal pkgSetting, PackageDexUsage.PackageUseInfo useInfo)
+            throws LegacyDexoptDisabledException {
         final String[] instructionSets = getAppDexInstructionSets(pkgSetting.getPrimaryCpuAbi(),
                 pkgSetting.getSecondaryCpuAbi());
         final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
@@ -929,7 +935,8 @@
     @GuardedBy("mInstallLock")
     private int getDexoptNeeded(String packageName, String path, String isa, String compilerFilter,
             String classLoaderContext, int profileAnalysisResult, boolean downgrade,
-            int dexoptFlags, String oatDir) {
+            int dexoptFlags, String oatDir) throws LegacyDexoptDisabledException {
+        Installer.checkLegacyDexoptDisabled();
         final boolean shouldBePublic = (dexoptFlags & DEXOPT_PUBLIC) != 0;
         final boolean isProfileGuidedFilter = (dexoptFlags & DEXOPT_PROFILE_GUIDED) != 0;
         boolean newProfile = profileAnalysisResult == PROFILE_ANALYSIS_OPTIMIZE;
@@ -962,7 +969,7 @@
         } catch (IOException ioe) {
             Slog.w(TAG, "IOException reading apk: " + path, ioe);
             return DEX_OPT_FAILED;
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             Slog.wtf(TAG, "Unexpected exception when calling dexoptNeeded on " + path, e);
             return DEX_OPT_FAILED;
         }
@@ -976,11 +983,12 @@
 
     /** Returns true if the current artifacts of the app are private to the app itself. */
     @GuardedBy("mInstallLock")
-    private boolean isOdexPrivate(String packageName, String path, String isa, String oatDir) {
+    private boolean isOdexPrivate(String packageName, String path, String isa, String oatDir)
+            throws LegacyDexoptDisabledException {
         try {
             return mInstaller.getOdexVisibility(packageName, path, isa, oatDir)
                     == Installer.ODEX_IS_PRIVATE;
-        } catch (Exception e) {
+        } catch (InstallerException e) {
             Slog.w(TAG, "Failed to get odex visibility for " + path, e);
             return false;
         }
@@ -996,7 +1004,7 @@
      * may return a different result.
      */
     private int analyseProfiles(AndroidPackage pkg, int uid, String profileName,
-            String compilerFilter) {
+            String compilerFilter) throws LegacyDexoptDisabledException {
         // Check if we are allowed to merge and if the compiler filter is profile guided.
         if (!isProfileGuidedCompilerFilter(compilerFilter)) {
             return PROFILE_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA;
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index afcd9d1..0556c3b 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -48,6 +48,7 @@
 import static com.android.internal.util.XmlUtils.writeByteArrayAttribute;
 import static com.android.internal.util.XmlUtils.writeStringAttribute;
 import static com.android.internal.util.XmlUtils.writeUriAttribute;
+import static com.android.server.pm.DexOptHelper.useArtService;
 import static com.android.server.pm.PackageInstallerService.prepareStageDir;
 import static com.android.server.pm.PackageManagerService.APP_METADATA_FILE_NAME;
 
@@ -164,6 +165,7 @@
 import com.android.modules.utils.TypedXmlSerializer;
 import com.android.server.LocalServices;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.pkg.AndroidPackage;
 import com.android.server.pm.pkg.PackageStateInternal;
@@ -2382,9 +2384,15 @@
                 }
 
                 if (isLinkPossible(fromFiles, toDir)) {
-                    if (!mResolvedInstructionSets.isEmpty()) {
-                        final File oatDir = new File(toDir, "oat");
-                        createOatDirs(tempPackageName, mResolvedInstructionSets, oatDir);
+                    if (!useArtService()) { // ART Service creates oat dirs on demand instead.
+                        if (!mResolvedInstructionSets.isEmpty()) {
+                            final File oatDir = new File(toDir, "oat");
+                            try {
+                                createOatDirs(tempPackageName, mResolvedInstructionSets, oatDir);
+                            } catch (LegacyDexoptDisabledException e) {
+                                throw new RuntimeException(e);
+                            }
+                        }
                     }
                     // pre-create lib dirs for linking if necessary
                     if (!mResolvedNativeLibPaths.isEmpty()) {
@@ -3629,7 +3637,7 @@
     }
 
     private void createOatDirs(String packageName, List<String> instructionSets, File fromDir)
-            throws PackageManagerException {
+            throws PackageManagerException, LegacyDexoptDisabledException {
         for (String instructionSet : instructionSets) {
             try {
                 mInstaller.createOatDir(packageName, fromDir.getAbsolutePath(), instructionSet);
diff --git a/services/core/java/com/android/server/pm/PackageManagerInternalBase.java b/services/core/java/com/android/server/pm/PackageManagerInternalBase.java
index cc9c1e0..c44818e 100644
--- a/services/core/java/com/android/server/pm/PackageManagerInternalBase.java
+++ b/services/core/java/com/android/server/pm/PackageManagerInternalBase.java
@@ -47,6 +47,7 @@
 import android.util.ArraySet;
 import android.util.SparseArray;
 
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.dex.DexManager;
 import com.android.server.pm.dex.DynamicCodeLogger;
 import com.android.server.pm.permission.PermissionManagerServiceInternal;
@@ -708,7 +709,12 @@
     @Override
     @Deprecated
     public final long deleteOatArtifactsOfPackage(String packageName) {
-        return mService.deleteOatArtifactsOfPackage(snapshot(), packageName);
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            return mService.deleteOatArtifactsOfPackage(snapshot(), packageName);
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Override
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index c1298ff..5ae0df4 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -39,6 +39,7 @@
 
 import static com.android.internal.annotations.VisibleForTesting.Visibility;
 import static com.android.internal.util.FrameworkStatsLog.BOOT_TIME_EVENT_DURATION__EVENT__OTA_PACKAGE_MANAGER_INIT_TIME;
+import static com.android.server.pm.DexOptHelper.useArtService;
 import static com.android.server.pm.InstructionSets.getDexCodeInstructionSet;
 import static com.android.server.pm.InstructionSets.getPreferredInstructionSet;
 import static com.android.server.pm.PackageManagerServiceUtils.compareSignatures;
@@ -198,6 +199,7 @@
 import com.android.server.compat.CompatChange;
 import com.android.server.compat.PlatformCompat;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.Settings.VersionInfo;
 import com.android.server.pm.dex.ArtManagerService;
 import com.android.server.pm.dex.ArtUtils;
@@ -2136,16 +2138,19 @@
                 // the rest of the commands above) because there's precious little we
                 // can do about it. A settings error is reported, though.
                 final List<String> changedAbiCodePath =
-                        ScanPackageUtils.applyAdjustedAbiToSharedUser(
-                                setting, null /*scannedPackage*/,
+                        ScanPackageUtils.applyAdjustedAbiToSharedUser(setting,
+                                null /*scannedPackage*/,
                                 mInjector.getAbiHelper().getAdjustedAbiForSharedUser(
                                         setting.getPackageStates(), null /*scannedPackage*/));
-                if (changedAbiCodePath != null && changedAbiCodePath.size() > 0) {
+                if (!useArtService() && // Skip for ART Service since it has its own dex file GC.
+                        changedAbiCodePath != null && changedAbiCodePath.size() > 0) {
                     for (int i = changedAbiCodePath.size() - 1; i >= 0; --i) {
                         final String codePathString = changedAbiCodePath.get(i);
                         try {
                             mInstaller.rmdex(codePathString,
                                     getDexCodeInstructionSet(getPreferredInstructionSet()));
+                        } catch (LegacyDexoptDisabledException e) {
+                            throw new RuntimeException(e);
                         } catch (InstallerException ignored) {
                         }
                     }
@@ -2952,7 +2957,12 @@
     }
 
     public void updatePackagesIfNeeded() {
-        mDexOptHelper.performPackageDexOptUpgradeIfNeeded();
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            mDexOptHelper.performPackageDexOptUpgradeIfNeeded();
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private void notifyPackageUseInternal(String packageName, int reason) {
@@ -4266,7 +4276,12 @@
                     }
                 });
 
-        mBackgroundDexOptService.systemReady();
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            mBackgroundDexOptService.systemReady();
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
 
         // Prune unused static shared libraries which have been cached a period of time
         schedulePruneUnusedStaticSharedLibraries(false /* delay */);
@@ -4615,7 +4630,8 @@
 
             final Computer snapshot = snapshotComputer();
             final AndroidPackage pkg = snapshot.getPackage(packageName);
-            try (PackageFreezer ignored = freezePackage(packageName, "clearApplicationProfileData")) {
+            try (PackageFreezer ignored =
+                            freezePackage(packageName, "clearApplicationProfileData")) {
                 synchronized (mInstallLock) {
                     mAppDataHelper.clearAppProfilesLIF(pkg);
                 }
@@ -4797,9 +4813,14 @@
                 throw new IllegalArgumentException("Unknown package: " + packageName);
             }
 
+            // TODO(b/251903639): Call into ART Service.
             synchronized (mInstallLock) {
                 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "dump profiles");
-                mArtManagerService.dumpProfiles(pkg, dumpClassesAndMethods);
+                try {
+                    mArtManagerService.dumpProfiles(pkg, dumpClassesAndMethods);
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
                 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
             }
         }
@@ -5497,20 +5518,40 @@
          */
         @Override
         public void reconcileSecondaryDexFiles(String packageName) {
+            if (useArtService()) {
+                // ART Service currently relies on a GC to find stale oat files, including secondary
+                // dex files. Hence it doesn't use this call for anything.
+                return;
+            }
+
             final Computer snapshot = snapshotComputer();
             if (snapshot.getInstantAppPackageName(Binder.getCallingUid()) != null) {
                 return;
             } else if (snapshot.isInstantAppInternal(
-                    packageName, UserHandle.getCallingUserId(), Process.SYSTEM_UID)) {
+                               packageName, UserHandle.getCallingUserId(), Process.SYSTEM_UID)) {
                 return;
             }
-            mDexManager.reconcileSecondaryDexFiles(packageName);
+            try {
+                mDexManager.reconcileSecondaryDexFiles(packageName);
+            } catch (LegacyDexoptDisabledException e) {
+                throw new RuntimeException(e);
+            }
         }
 
         @Override
         public void registerDexModule(String packageName, String dexModulePath,
                 boolean isSharedModule,
                 IDexModuleRegisterCallback callback) {
+            if (useArtService()) {
+                // ART Service currently doesn't support this explicit dexopting and instead relies
+                // on background dexopt for secondary dex files. This API is problematic since it
+                // doesn't provide the correct classloader context.
+                Slog.i(TAG,
+                        "Ignored unsupported registerDexModule call for " + dexModulePath + " in "
+                                + packageName);
+                return;
+            }
+
             int userId = UserHandle.getCallingUserId();
             ApplicationInfo ai = snapshot().getApplicationInfo(packageName, /*flags*/ 0, userId);
             DexManager.RegisterDexModuleResult result;
@@ -5520,7 +5561,12 @@
                                 " calling user. package=" + packageName + ", user=" + userId);
                 result = new DexManager.RegisterDexModuleResult(false, "Package not installed");
             } else {
-                result = mDexManager.registerDexModule(ai, dexModulePath, isSharedModule, userId);
+                try {
+                    result = mDexManager.registerDexModule(
+                            ai, dexModulePath, isSharedModule, userId);
+                } catch (LegacyDexoptDisabledException e) {
+                    throw new RuntimeException(e);
+                }
             }
 
             if (callback != null) {
@@ -6994,7 +7040,8 @@
         return AndroidPackageUtils.canHaveOatDir(packageState, packageState.getPkg());
     }
 
-    long deleteOatArtifactsOfPackage(@NonNull Computer snapshot, String packageName) {
+    long deleteOatArtifactsOfPackage(@NonNull Computer snapshot, String packageName)
+            throws LegacyDexoptDisabledException {
         PackageManagerServiceUtils.enforceSystemOrRootOrShell(
                 "Only the system or shell can delete oat artifacts");
 
diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
index 2138c20..6af1d0f 100644
--- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
+++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@@ -113,6 +113,7 @@
 import com.android.server.LocalServices;
 import com.android.server.SystemConfig;
 import com.android.server.art.ArtManagerLocal;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.PackageManagerShellCommandDataLoader.Metadata;
 import com.android.server.pm.permission.LegacyPermissionManagerInternal;
 import com.android.server.pm.permission.PermissionAllowlist;
@@ -1953,52 +1954,62 @@
     }
 
     private int runBgDexOpt() throws RemoteException {
-        String opt = getNextOption();
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            String opt = getNextOption();
 
-        if (opt == null) {
-            List<String> packageNames = new ArrayList<>();
-            String arg;
-            while ((arg = getNextArg()) != null) {
-                packageNames.add(arg);
-            }
-            if (!BackgroundDexOptService.getService().runBackgroundDexoptJob(
-                    packageNames.isEmpty() ? null : packageNames)) {
-                getOutPrintWriter().println("Failure");
-                return -1;
-            }
-        } else {
-            String extraArg = getNextArg();
-            if (extraArg != null) {
-                getErrPrintWriter().println("Invalid argument: " + extraArg);
-                return -1;
-            }
-
-            switch (opt) {
-                case "--cancel":
-                    return cancelBgDexOptJob();
-
-                case "--disable":
-                    BackgroundDexOptService.getService().setDisableJobSchedulerJobs(true);
-                    break;
-
-                case "--enable":
-                    BackgroundDexOptService.getService().setDisableJobSchedulerJobs(false);
-                    break;
-
-                default:
-                    getErrPrintWriter().println("Unknown option: " + opt);
+            if (opt == null) {
+                List<String> packageNames = new ArrayList<>();
+                String arg;
+                while ((arg = getNextArg()) != null) {
+                    packageNames.add(arg);
+                }
+                if (!BackgroundDexOptService.getService().runBackgroundDexoptJob(
+                            packageNames.isEmpty() ? null : packageNames)) {
+                    getOutPrintWriter().println("Failure");
                     return -1;
-            }
-        }
+                }
+            } else {
+                String extraArg = getNextArg();
+                if (extraArg != null) {
+                    getErrPrintWriter().println("Invalid argument: " + extraArg);
+                    return -1;
+                }
 
-        getOutPrintWriter().println("Success");
-        return 0;
+                switch (opt) {
+                    case "--cancel":
+                        return cancelBgDexOptJob();
+
+                    case "--disable":
+                        BackgroundDexOptService.getService().setDisableJobSchedulerJobs(true);
+                        break;
+
+                    case "--enable":
+                        BackgroundDexOptService.getService().setDisableJobSchedulerJobs(false);
+                        break;
+
+                    default:
+                        getErrPrintWriter().println("Unknown option: " + opt);
+                        return -1;
+                }
+            }
+
+            getOutPrintWriter().println("Success");
+            return 0;
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private int cancelBgDexOptJob() throws RemoteException {
-        BackgroundDexOptService.getService().cancelBackgroundDexoptJob();
-        getOutPrintWriter().println("Success");
-        return 0;
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            BackgroundDexOptService.getService().cancelBackgroundDexoptJob();
+            getOutPrintWriter().println("Success");
+            return 0;
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private int runDeleteDexOpt() throws RemoteException {
@@ -2008,8 +2019,8 @@
             pw.println("Error: no package name");
             return 1;
         }
-        long freedBytes = LocalServices.getService(
-                PackageManagerInternal.class).deleteOatArtifactsOfPackage(packageName);
+        long freedBytes = LocalServices.getService(PackageManagerInternal.class)
+                                  .deleteOatArtifactsOfPackage(packageName);
         if (freedBytes < 0) {
             pw.println("Error: delete failed");
             return 1;
@@ -2273,7 +2284,7 @@
             if (abandonSession) {
                 try {
                     doAbandonSession(sessionId, false /*logSuccess*/);
-                } catch (Exception ignore) {
+                } catch (RuntimeException ignore) {
                 }
             }
         }
diff --git a/services/core/java/com/android/server/pm/RemovePackageHelper.java b/services/core/java/com/android/server/pm/RemovePackageHelper.java
index eb99536..e5aaddb 100644
--- a/services/core/java/com/android/server/pm/RemovePackageHelper.java
+++ b/services/core/java/com/android/server/pm/RemovePackageHelper.java
@@ -45,6 +45,7 @@
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.util.ArrayUtils;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.parsing.PackageCacher;
 import com.android.server.pm.parsing.pkg.AndroidPackageUtils;
 import com.android.server.pm.parsing.pkg.PackageImpl;
@@ -424,8 +425,11 @@
             String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
             for (String codePath : allCodePaths) {
                 for (String dexCodeInstructionSet : dexCodeInstructionSets) {
+                    // TODO(b/251903639): Call into ART Service.
                     try {
                         mPm.mInstaller.rmdex(codePath, dexCodeInstructionSet);
+                    } catch (LegacyDexoptDisabledException e) {
+                        throw new RuntimeException(e);
                     } catch (Installer.InstallerException ignored) {
                     }
                 }
diff --git a/services/core/java/com/android/server/pm/ScanResult.java b/services/core/java/com/android/server/pm/ScanResult.java
index 750e893..2af01d6 100644
--- a/services/core/java/com/android/server/pm/ScanResult.java
+++ b/services/core/java/com/android/server/pm/ScanResult.java
@@ -47,6 +47,9 @@
      */
     @Nullable
     public final PackageSetting mPkgSetting;
+
+    // TODO(b/260124949): Check if this can be dropped when the legacy PackageManager dexopt code is
+    // cleaned up.
     /** ABI code paths that have changed in the package scan */
     @Nullable public final List<String> mChangedAbiCodePath;
 
diff --git a/services/core/java/com/android/server/pm/dex/ArtManagerService.java b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
index e19ebce..b5b6347 100644
--- a/services/core/java/com/android/server/pm/dex/ArtManagerService.java
+++ b/services/core/java/com/android/server/pm/dex/ArtManagerService.java
@@ -52,6 +52,7 @@
 import com.android.server.LocalServices;
 import com.android.server.pm.Installer;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.PackageManagerService;
 import com.android.server.pm.PackageManagerServiceCompilerMapping;
 import com.android.server.pm.parsing.PackageInfoUtils;
@@ -177,7 +178,6 @@
                 == ApplicationInfo.FLAG_DEBUGGABLE;
     }
 
-
     @Override
     public void snapshotRuntimeProfile(@ProfileType int profileType, @Nullable String packageName,
             @Nullable String codePath, @NonNull ISnapshotRuntimeProfileCallback callback,
@@ -210,15 +210,20 @@
             Slog.d(TAG, "Requested snapshot for " + packageName + ":" + codePath);
         }
 
-        if (bootImageProfile) {
-            snapshotBootImageProfile(callback);
-        } else {
-            snapshotAppProfile(packageName, codePath, callback);
+        // TODO(b/251903639): Call into ART Service.
+        try {
+            if (bootImageProfile) {
+                snapshotBootImageProfile(callback);
+            } else {
+                snapshotAppProfile(packageName, codePath, callback);
+            }
+        } catch (LegacyDexoptDisabledException e) {
+            throw new RuntimeException(e);
         }
     }
 
     private void snapshotAppProfile(String packageName, String codePath,
-            ISnapshotRuntimeProfileCallback callback) {
+            ISnapshotRuntimeProfileCallback callback) throws LegacyDexoptDisabledException {
         PackageInfo info = null;
         try {
             // Note that we use the default user 0 to retrieve the package info.
@@ -269,7 +274,8 @@
     }
 
     private void createProfileSnapshot(String packageName, String profileName, String classpath,
-            int appId, ISnapshotRuntimeProfileCallback callback) {
+            int appId, ISnapshotRuntimeProfileCallback callback)
+            throws LegacyDexoptDisabledException {
         // Ask the installer to snapshot the profile.
         try {
             if (!mInstaller.createProfileSnapshot(appId, packageName, profileName, classpath)) {
@@ -299,7 +305,8 @@
         }
     }
 
-    private void destroyProfileSnapshot(String packageName, String profileName) {
+    private void destroyProfileSnapshot(String packageName, String profileName)
+            throws LegacyDexoptDisabledException {
         if (DEBUG) {
             Slog.d(TAG, "Destroying profile snapshot for" + packageName + ":" + profileName);
         }
@@ -333,7 +340,8 @@
         }
     }
 
-    private void snapshotBootImageProfile(ISnapshotRuntimeProfileCallback callback) {
+    private void snapshotBootImageProfile(ISnapshotRuntimeProfileCallback callback)
+            throws LegacyDexoptDisabledException {
         // Combine the profiles for boot classpath and system server classpath.
         // This avoids having yet another type of profiles and simplifies the processing.
         String classpath = String.join(":", Os.getenv("BOOTCLASSPATH"),
@@ -364,7 +372,7 @@
         mHandler.post(() -> {
             try {
                 callback.onError(errCode);
-            } catch (Exception e) {
+            } catch (RemoteException | RuntimeException e) {
                 Slog.w(TAG, "Failed to callback after profile snapshot for " + packageName, e);
             }
         });
@@ -387,7 +395,7 @@
                             + packageName);
                     callback.onError(ArtManager.SNAPSHOT_FAILED_INTERNAL_ERROR);
                 }
-            } catch (Exception e) {
+            } catch (RemoteException | RuntimeException e) {
                 Slog.w(TAG,
                         "Failed to call onSuccess after profile snapshot for " + packageName, e);
             } finally {
@@ -402,9 +410,8 @@
      *   - create the current primary profile to save time at app startup time.
      *   - copy the profiles from the associated dex metadata file to the reference profile.
      */
-    public void prepareAppProfiles(
-            AndroidPackage pkg, @UserIdInt int user,
-            boolean updateReferenceProfileContent) {
+    public void prepareAppProfiles(AndroidPackage pkg, @UserIdInt int user,
+            boolean updateReferenceProfileContent) throws LegacyDexoptDisabledException {
         final int appId = UserHandle.getAppId(pkg.getUid());
         if (user < 0) {
             Slog.wtf(TAG, "Invalid user id: " + user);
@@ -444,9 +451,8 @@
     /**
      * Prepares the app profiles for a set of users. {@see ArtManagerService#prepareAppProfiles}.
      */
-    public void prepareAppProfiles(
-            AndroidPackage pkg, int[] user,
-            boolean updateReferenceProfileContent) {
+    public void prepareAppProfiles(AndroidPackage pkg, int[] user,
+            boolean updateReferenceProfileContent) throws LegacyDexoptDisabledException {
         for (int i = 0; i < user.length; i++) {
             prepareAppProfiles(pkg, user[i], updateReferenceProfileContent);
         }
@@ -455,7 +461,7 @@
     /**
      * Clear the profiles for the given package.
      */
-    public void clearAppProfiles(AndroidPackage pkg) {
+    public void clearAppProfiles(AndroidPackage pkg) throws LegacyDexoptDisabledException {
         try {
             ArrayMap<String, String> packageProfileNames = getPackageProfileNames(pkg);
             for (int i = packageProfileNames.size() - 1; i >= 0; i--) {
@@ -470,7 +476,8 @@
     /**
      * Dumps the profiles for the given package.
      */
-    public void dumpProfiles(AndroidPackage pkg, boolean dumpClassesAndMethods) {
+    public void dumpProfiles(AndroidPackage pkg, boolean dumpClassesAndMethods)
+            throws LegacyDexoptDisabledException {
         final int sharedGid = UserHandle.getSharedAppGid(pkg.getUid());
         try {
             ArrayMap<String, String> packageProfileNames = getPackageProfileNames(pkg);
diff --git a/services/core/java/com/android/server/pm/dex/DexManager.java b/services/core/java/com/android/server/pm/dex/DexManager.java
index 8c2da45..fbf7409 100644
--- a/services/core/java/com/android/server/pm/dex/DexManager.java
+++ b/services/core/java/com/android/server/pm/dex/DexManager.java
@@ -46,6 +46,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.pm.Installer;
 import com.android.server.pm.Installer.InstallerException;
+import com.android.server.pm.Installer.LegacyDexoptDisabledException;
 import com.android.server.pm.PackageDexOptimizer;
 import com.android.server.pm.PackageManagerService;
 import com.android.server.pm.PackageManagerServiceUtils;
@@ -190,7 +191,7 @@
         try {
             notifyDexLoadInternal(loadingAppInfo, classLoaderContextMap, loaderIsa,
                     loaderUserId, loaderIsIsolatedProcess);
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             Slog.w(TAG, "Exception while notifying dex load for package " +
                     loadingAppInfo.packageName, e);
         }
@@ -325,7 +326,7 @@
     public void load(Map<Integer, List<PackageInfo>> existingPackages) {
         try {
             loadInternal(existingPackages);
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             mPackageDexUsage.clear();
             mDynamicCodeLogger.clear();
             Slog.w(TAG, "Exception while loading. Starting with a fresh state.", e);
@@ -457,7 +458,7 @@
             List<String> packagesToKeepDataAbout = new ArrayList<>();
             mPackageDexUsage.syncData(
                     packageToUsersMap, packageToCodePaths, packagesToKeepDataAbout);
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             mPackageDexUsage.clear();
             Slog.w(TAG, "Exception while loading package dex usage. "
                     + "Starting with a fresh state.", e);
@@ -465,7 +466,7 @@
 
         try {
             mDynamicCodeLogger.readAndSync(packageToUsersMap);
-        } catch (Exception e) {
+        } catch (RuntimeException e) {
             mDynamicCodeLogger.clear();
             Slog.w(TAG, "Exception while loading package dynamic code usage. "
                     + "Starting with a fresh state.", e);
@@ -514,7 +515,7 @@
      * @return true if all secondary dex files were processed successfully (compiled or skipped
      *         because they don't need to be compiled)..
      */
-    public boolean dexoptSecondaryDex(DexoptOptions options) {
+    public boolean dexoptSecondaryDex(DexoptOptions options) throws LegacyDexoptDisabledException {
         if (isPlatformPackage(options.getPackageName())) {
             // We could easily redirect to #dexoptSystemServer in this case. But there should be
             // no-one calling this method directly for system server.
@@ -574,7 +575,8 @@
      * <p>PackageDexOptimizer.DEX_OPT_FAILED if any dexopt operation failed.
      * <p>PackageDexOptimizer.DEX_OPT_PERFORMED if all dexopt operations succeeded.
      */
-    public int dexoptSystemServer(DexoptOptions options) {
+    public int dexoptSystemServer(DexoptOptions options) throws LegacyDexoptDisabledException {
+        // TODO(b/254043366): Clean this up.
         if (!isPlatformPackage(options.getPackageName())) {
             Slog.wtf(TAG, "Non system server package used when trying to dexopt system server:"
                     + options.getPackageName());
@@ -664,7 +666,8 @@
      * {@code packagName} and the actual dex files. For all dex files that were
      * deleted, update the internal records and delete any generated oat files.
      */
-    public void reconcileSecondaryDexFiles(String packageName) {
+    public void reconcileSecondaryDexFiles(String packageName)
+            throws LegacyDexoptDisabledException {
         PackageUseInfo useInfo = getPackageUseInfoOrDefault(packageName);
         if (useInfo.getDexUseInfoMap().isEmpty()) {
             if (DEBUG) {
@@ -756,7 +759,7 @@
     // TODO(calin): questionable API in the presence of class loaders context. Needs amends as the
     // compilation happening here will use a pessimistic context.
     public RegisterDexModuleResult registerDexModule(ApplicationInfo info, String dexPath,
-            boolean isSharedModule, int userId) {
+            boolean isSharedModule, int userId) throws LegacyDexoptDisabledException {
         // Find the owning package record.
         DexSearchResult searchResult = getDexPackage(info, dexPath, userId);
 
@@ -1012,7 +1015,8 @@
      * @param packageInfo the package information.
      * @return the number of freed bytes or -1 if there was an error in the process.
      */
-    public long deleteOptimizedFiles(ArtPackageInfo packageInfo) {
+    public long deleteOptimizedFiles(ArtPackageInfo packageInfo)
+            throws LegacyDexoptDisabledException {
         long freedBytes = 0;
         boolean hadErrors = false;
         final String packageName = packageInfo.getPackageName();
diff --git a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
index 01674bb..8b36da5 100644
--- a/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/pm/BackgroundDexOptServiceUnitTest.java
@@ -169,19 +169,19 @@
     }
 
     @Test
-    public void testBootCompleted() {
+    public void testBootCompleted() throws Exception {
         initUntilBootCompleted();
     }
 
     @Test
-    public void testNoExecutionForIdleJobBeforePostBootUpdate() {
+    public void testNoExecutionForIdleJobBeforePostBootUpdate() throws Exception {
         initUntilBootCompleted();
 
         assertThat(mService.onStartJob(mJobServiceForIdle, mJobParametersForIdle)).isFalse();
     }
 
     @Test
-    public void testNoExecutionForLowStorage() {
+    public void testNoExecutionForLowStorage() throws Exception {
         initUntilBootCompleted();
         when(mPackageManager.isStorageLow()).thenReturn(true);
 
@@ -191,7 +191,7 @@
     }
 
     @Test
-    public void testNoExecutionForNoOptimizablePackages() {
+    public void testNoExecutionForNoOptimizablePackages() throws Exception {
         initUntilBootCompleted();
         when(mDexOptHelper.getOptimizablePackages(any())).thenReturn(Collections.emptyList());
 
@@ -201,7 +201,7 @@
     }
 
     @Test
-    public void testPostBootUpdateFullRun() {
+    public void testPostBootUpdateFullRun() throws Exception {
         initUntilBootCompleted();
 
         runFullJob(mJobServiceForPostBoot, mJobParametersForPostBoot,
@@ -210,7 +210,7 @@
     }
 
     @Test
-    public void testPostBootUpdateFullRunWithPackageFailure() {
+    public void testPostBootUpdateFullRunWithPackageFailure() throws Exception {
         mDexOptResultForPackageAAA = PackageDexOptimizer.DEX_OPT_FAILED;
 
         initUntilBootCompleted();
@@ -224,7 +224,7 @@
     }
 
     @Test
-    public void testIdleJobFullRun() {
+    public void testIdleJobFullRun() throws Exception {
         initUntilBootCompleted();
         runFullJob(mJobServiceForPostBoot, mJobParametersForPostBoot,
                 /* expectedReschedule= */ false, /* expectedStatus= */ STATUS_OK,
@@ -235,7 +235,7 @@
     }
 
     @Test
-    public void testIdleJobFullRunWithFailureOnceAndSuccessAfterUpdate() {
+    public void testIdleJobFullRunWithFailureOnceAndSuccessAfterUpdate() throws Exception {
         mDexOptResultForPackageAAA = PackageDexOptimizer.DEX_OPT_FAILED;
 
         initUntilBootCompleted();
@@ -271,7 +271,7 @@
     }
 
     @Test
-    public void testIdleJobFullRunWithFatalError() {
+    public void testIdleJobFullRunWithFatalError() throws Exception {
         initUntilBootCompleted();
         runFullJob(mJobServiceForPostBoot, mJobParametersForPostBoot,
                 /* expectedReschedule= */ false, /* expectedStatus= */ STATUS_OK,
@@ -285,7 +285,7 @@
     }
 
     @Test
-    public void testSystemReadyWhenDisabled() {
+    public void testSystemReadyWhenDisabled() throws Exception {
         when(mInjector.isBackgroundDexOptDisabled()).thenReturn(true);
 
         mService.systemReady();
@@ -294,7 +294,7 @@
     }
 
     @Test
-    public void testStopByCancelFlag() {
+    public void testStopByCancelFlag() throws Exception {
         when(mInjector.createAndStartThread(any(), any())).thenReturn(Thread.currentThread());
         initUntilBootCompleted();
 
@@ -447,7 +447,7 @@
     }
 
     @Test
-    public void testStopByThermal() {
+    public void testStopByThermal() throws Exception {
         when(mInjector.createAndStartThread(any(), any())).thenReturn(Thread.currentThread());
         initUntilBootCompleted();
 
@@ -478,7 +478,7 @@
     }
 
     @Test
-    public void testDisableJobSchedulerJobs() {
+    public void testDisableJobSchedulerJobs() throws Exception {
         when(mInjector.getCallingUid()).thenReturn(Process.SHELL_UID);
         mService.setDisableJobSchedulerJobs(true);
         assertThat(mService.onStartJob(mJobServiceForIdle, mJobParametersForIdle)).isFalse();
@@ -492,7 +492,7 @@
         assertThrows(SecurityException.class, () -> mService.setDisableJobSchedulerJobs(true));
     }
 
-    private void initUntilBootCompleted() {
+    private void initUntilBootCompleted() throws Exception {
         ArgumentCaptor<BroadcastReceiver> argReceiver = ArgumentCaptor.forClass(
                 BroadcastReceiver.class);
         ArgumentCaptor<IntentFilter> argIntentFilter = ArgumentCaptor.forClass(IntentFilter.class);
@@ -515,7 +515,7 @@
         assertThat(jobIds).containsExactlyElementsIn(expectedJobIds);
     }
 
-    private void verifyLastControlDexOptBlockingCall(boolean expected) {
+    private void verifyLastControlDexOptBlockingCall(boolean expected) throws Exception {
         ArgumentCaptor<Boolean> argDexOptBlock = ArgumentCaptor.forClass(Boolean.class);
         verify(mDexOptHelper, atLeastOnce()).controlDexOptBlocking(argDexOptBlock.capture());
         assertThat(argDexOptBlock.getValue()).isEqualTo(expected);
@@ -523,7 +523,7 @@
 
     private void runFullJob(BackgroundDexOptJobService jobService, JobParameters params,
             boolean expectedReschedule, int expectedStatus, int totalJobFinishedWithParams,
-            @Nullable String expectedSkippedPackage) {
+            @Nullable String expectedSkippedPackage) throws Exception {
         when(mInjector.createAndStartThread(any(), any())).thenReturn(Thread.currentThread());
         addFullRunSequence(expectedSkippedPackage);
         assertThat(mService.onStartJob(jobService, params)).isTrue();