Merge "Revert "Delay init of classes of pre-allocated exceptions.""
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 9f42727..7d1115e 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -668,7 +668,7 @@
   dependencies := $$(ART_TEST_$(2)_GTEST$(3)_RULES)
 
 .PHONY: $$(rule_name)
-$$(rule_name): $$(dependencies) dx d8-compat-dx desugar
+$$(rule_name): $$(dependencies) d8 d8-compat-dx
 	$(hide) $$(call ART_TEST_PREREQ_FINISHED,$$@)
 
   # Clear locally defined variables.
diff --git a/compiler/common_compiler_test.cc b/compiler/common_compiler_test.cc
index 7491173..52c767f 100644
--- a/compiler/common_compiler_test.cc
+++ b/compiler/common_compiler_test.cc
@@ -40,6 +40,7 @@
 #include "oat_quick_method_header.h"
 #include "scoped_thread_state_change-inl.h"
 #include "thread-current-inl.h"
+#include "utils/atomic_dex_ref_map-inl.h"
 
 namespace art {
 
@@ -332,4 +333,10 @@
   image_reservation_.reset();
 }
 
+void CommonCompilerTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
+  compiler_options_->dex_files_for_oat_file_ = dex_files;
+  compiler_driver_->compiled_classes_.AddDexFiles(dex_files);
+  compiler_driver_->dex_to_dex_compiler_.SetDexFiles(dex_files);
+}
+
 }  // namespace art
diff --git a/compiler/common_compiler_test.h b/compiler/common_compiler_test.h
index 46b59a3..f070bbb 100644
--- a/compiler/common_compiler_test.h
+++ b/compiler/common_compiler_test.h
@@ -33,6 +33,7 @@
 class CompilerDriver;
 class CompilerOptions;
 class CumulativeLogger;
+class DexFile;
 class ProfileCompilationInfo;
 class VerificationResults;
 
@@ -93,6 +94,8 @@
 
   void UnreserveImageSpace();
 
+  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files);
+
   Compiler::Kind compiler_kind_ = Compiler::kOptimizing;
   std::unique_ptr<CompilerOptions> compiler_options_;
   std::unique_ptr<VerificationResults> verification_results_;
diff --git a/compiler/compiler.h b/compiler/compiler.h
index f2ec3a9..ef3d87f 100644
--- a/compiler/compiler.h
+++ b/compiler/compiler.h
@@ -39,12 +39,6 @@
 class OatWriter;
 class Thread;
 
-enum class CopyOption {
-  kNever,
-  kAlways,
-  kOnlyIfCompressed
-};
-
 class Compiler {
  public:
   enum Kind {
diff --git a/compiler/dex/dex_to_dex_decompiler_test.cc b/compiler/dex/dex_to_dex_decompiler_test.cc
index 76250d2..4f83d60 100644
--- a/compiler/dex/dex_to_dex_decompiler_test.cc
+++ b/compiler/dex/dex_to_dex_decompiler_test.cc
@@ -47,7 +47,7 @@
     // the results for all the dex files, not just the results for the current dex file.
     down_cast<QuickCompilerCallbacks*>(Runtime::Current()->GetCompilerCallbacks())->SetVerifierDeps(
         new verifier::VerifierDeps(GetDexFiles(class_loader)));
-    compiler_driver_->SetDexFilesForOatFile(GetDexFiles(class_loader));
+    SetDexFilesForOatFile(GetDexFiles(class_loader));
     compiler_driver_->CompileAll(class_loader, GetDexFiles(class_loader), &timings);
   }
 
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index ed4fb6f..66a8a57 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -885,8 +885,12 @@
                                 const std::vector<const DexFile*>& dex_files,
                                 TimingLogger* timings) {
   CheckThreadPools();
+
   VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
 
+  compiled_classes_.AddDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
+  dex_to_dex_compiler_.SetDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
+
   // Precompile:
   // 1) Load image classes.
   // 2) Resolve all classes.
@@ -1948,7 +1952,8 @@
     // Create per-thread VerifierDeps to avoid contention on the main one.
     // We will merge them after verification.
     for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
-      worker->GetThread()->SetVerifierDeps(new verifier::VerifierDeps(dex_files_for_oat_file_));
+      worker->GetThread()->SetVerifierDeps(
+          new verifier::VerifierDeps(GetCompilerOptions().GetDexFilesForOatFile()));
     }
   }
 
@@ -1973,7 +1978,7 @@
     for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
       verifier::VerifierDeps* thread_deps = worker->GetThread()->GetVerifierDeps();
       worker->GetThread()->SetVerifierDeps(nullptr);
-      verifier_deps->MergeWith(*thread_deps, dex_files_for_oat_file_);
+      verifier_deps->MergeWith(*thread_deps, GetCompilerOptions().GetDexFilesForOatFile());
       delete thread_deps;
     }
     Thread::Current()->SetVerifierDeps(nullptr);
@@ -2846,7 +2851,7 @@
       if (kIsDebugBuild) {
         // Check to make sure it's not a dex file for an oat file we are compiling since these
         // should always succeed. These do not include classes in for used libraries.
-        for (const DexFile* dex_file : GetDexFilesForOatFile()) {
+        for (const DexFile* dex_file : GetCompilerOptions().GetDexFilesForOatFile()) {
           CHECK_NE(ref.dex_file, dex_file) << ref.dex_file->GetLocation();
         }
       }
@@ -2945,17 +2950,6 @@
   return oss.str();
 }
 
-bool CompilerDriver::MayInlineInternal(const DexFile* inlined_from,
-                                       const DexFile* inlined_into) const {
-  // We're not allowed to inline across dex files if we're the no-inline-from dex file.
-  if (inlined_from != inlined_into &&
-      ContainsElement(compiler_options_->GetNoInlineFromDexFile(), inlined_from)) {
-    return false;
-  }
-
-  return true;
-}
-
 void CompilerDriver::InitializeThreadPools() {
   size_t parallel_count = parallel_thread_count_ > 0 ? parallel_thread_count_ - 1 : 0;
   parallel_thread_pool_.reset(
@@ -2968,12 +2962,6 @@
   single_thread_pool_.reset();
 }
 
-void CompilerDriver::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
-  dex_files_for_oat_file_ = dex_files;
-  compiled_classes_.AddDexFiles(dex_files);
-  dex_to_dex_compiler_.SetDexFiles(dex_files);
-}
-
 void CompilerDriver::SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files) {
   classpath_classes_.AddDexFiles(dex_files);
 }
diff --git a/compiler/driver/compiler_driver.h b/compiler/driver/compiler_driver.h
index 36e93a8..54e1f37 100644
--- a/compiler/driver/compiler_driver.h
+++ b/compiler/driver/compiler_driver.h
@@ -106,17 +106,9 @@
 
   ~CompilerDriver();
 
-  // Set dex files associated with the oat file being compiled.
-  void SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files);
-
   // Set dex files classpath.
   void SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files);
 
-  // Get dex files associated with the the oat file being compiled.
-  ArrayRef<const DexFile* const> GetDexFilesForOatFile() const {
-    return ArrayRef<const DexFile* const>(dex_files_for_oat_file_);
-  }
-
   void CompileAll(jobject class_loader,
                   const std::vector<const DexFile*>& dex_files,
                   TimingLogger* timings)
@@ -354,13 +346,6 @@
   bool CanAssumeClassIsLoaded(mirror::Class* klass)
       REQUIRES_SHARED(Locks::mutator_lock_);
 
-  bool MayInline(const DexFile* inlined_from, const DexFile* inlined_into) const {
-    if (!kIsTargetBuild) {
-      return MayInlineInternal(inlined_from, inlined_into);
-    }
-    return true;
-  }
-
   const ProfileCompilationInfo* GetProfileCompilationInfo() const {
     return profile_compilation_info_;
   }
@@ -454,8 +439,6 @@
                const std::vector<const DexFile*>& dex_files,
                TimingLogger* timings);
 
-  bool MayInlineInternal(const DexFile* inlined_from, const DexFile* inlined_into) const;
-
   void InitializeThreadPools();
   void FreeThreadPools();
   void CheckThreadPools();
@@ -525,9 +508,6 @@
 
   bool support_boot_image_fixup_;
 
-  // List of dex files associates with the oat file.
-  std::vector<const DexFile*> dex_files_for_oat_file_;
-
   CompiledMethodStorage compiled_method_storage_;
 
   // Info for profile guided compilation.
diff --git a/compiler/driver/compiler_driver_test.cc b/compiler/driver/compiler_driver_test.cc
index 491e61f..2eeb439 100644
--- a/compiler/driver/compiler_driver_test.cc
+++ b/compiler/driver/compiler_driver_test.cc
@@ -46,7 +46,7 @@
     TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
     TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
     dex_files_ = GetDexFiles(class_loader);
-    compiler_driver_->SetDexFilesForOatFile(dex_files_);;
+    SetDexFilesForOatFile(dex_files_);
     compiler_driver_->CompileAll(class_loader, dex_files_, &timings);
     t.NewTiming("MakeAllExecutable");
     MakeAllExecutable(class_loader);
@@ -331,7 +331,7 @@
     ASSERT_GT(dex_files.size(), 0u);
     dex_file = dex_files.front();
   }
-  compiler_driver_->SetDexFilesForOatFile(dex_files);
+  SetDexFilesForOatFile(dex_files);
   callbacks_->SetDoesClassUnloading(true, compiler_driver_.get());
   ClassReference ref(dex_file, 0u);
   // Test that the status is read from the compiler driver as expected.
diff --git a/compiler/driver/compiler_options.cc b/compiler/driver/compiler_options.cc
index 3d37b68..cc1af3e 100644
--- a/compiler/driver/compiler_options.cc
+++ b/compiler/driver/compiler_options.cc
@@ -38,6 +38,7 @@
       num_dex_methods_threshold_(kDefaultNumDexMethodsThreshold),
       inline_max_code_units_(kUnsetInlineMaxCodeUnits),
       no_inline_from_(),
+      dex_files_for_oat_file_(),
       image_classes_(),
       boot_image_(false),
       core_image_(false),
diff --git a/compiler/driver/compiler_options.h b/compiler/driver/compiler_options.h
index 0709faf..908ff33 100644
--- a/compiler/driver/compiler_options.h
+++ b/compiler/driver/compiler_options.h
@@ -235,6 +235,10 @@
     return no_inline_from_;
   }
 
+  const std::vector<const DexFile*>& GetDexFilesForOatFile() const {
+    return dex_files_for_oat_file_;
+  }
+
   const HashSet<std::string>& GetImageClasses() const {
     return image_classes_;
   }
@@ -313,6 +317,9 @@
   // prefer vector<> over a lookup-oriented container, such as set<>.
   std::vector<const DexFile*> no_inline_from_;
 
+  // List of dex files associated with the oat file, empty for JIT.
+  std::vector<const DexFile*> dex_files_for_oat_file_;
+
   // Image classes, specifies the classes that will be included in the image if creating an image.
   // Must not be empty for real boot image, only for tests pretending to compile boot image.
   HashSet<std::string> image_classes_;
diff --git a/compiler/optimizing/inliner.cc b/compiler/optimizing/inliner.cc
index 7dd756e..72d53d2 100644
--- a/compiler/optimizing/inliner.cc
+++ b/compiler/optimizing/inliner.cc
@@ -126,7 +126,7 @@
 }
 
 bool HInliner::Run() {
-  if (compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits() == 0) {
+  if (codegen_->GetCompilerOptions().GetInlineMaxCodeUnits() == 0) {
     // Inlining effectively disabled.
     return false;
   } else if (graph_->IsDebuggable()) {
@@ -731,7 +731,7 @@
         offline_profile.dex_references.size());
   for (size_t i = 0; i < offline_profile.dex_references.size(); i++) {
     bool found = false;
-    for (const DexFile* dex_file : compiler_driver_->GetDexFilesForOatFile()) {
+    for (const DexFile* dex_file : codegen_->GetCompilerOptions().GetDexFilesForOatFile()) {
       if (offline_profile.dex_references[i].MatchesDex(dex_file)) {
         dex_profile_index_to_dex_cache[i] =
             caller_compilation_unit_.GetClassLinker()->FindDexCache(self, *dex_file);
@@ -1418,6 +1418,22 @@
   return count;
 }
 
+static inline bool MayInline(const CompilerOptions& compiler_options,
+                             const DexFile& inlined_from,
+                             const DexFile& inlined_into) {
+  if (kIsTargetBuild) {
+    return true;
+  }
+
+  // We're not allowed to inline across dex files if we're the no-inline-from dex file.
+  if (!IsSameDexFile(inlined_from, inlined_into) &&
+      ContainsElement(compiler_options.GetNoInlineFromDexFile(), &inlined_from)) {
+    return false;
+  }
+
+  return true;
+}
+
 bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction,
                                  ArtMethod* method,
                                  ReferenceTypeInfo receiver_type,
@@ -1439,8 +1455,9 @@
 
   // Check whether we're allowed to inline. The outermost compilation unit is the relevant
   // dex file here (though the transitivity of an inline chain would allow checking the calller).
-  if (!compiler_driver_->MayInline(method->GetDexFile(),
-                                   outer_compilation_unit_.GetDexFile())) {
+  if (!MayInline(codegen_->GetCompilerOptions(),
+                 *method->GetDexFile(),
+                 *outer_compilation_unit_.GetDexFile())) {
     if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) {
       LOG_SUCCESS() << "Successfully replaced pattern of invoke "
                     << method->PrettyMethod();
@@ -1465,7 +1482,7 @@
     return false;
   }
 
-  size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits();
+  size_t inline_max_code_units = codegen_->GetCompilerOptions().GetInlineMaxCodeUnits();
   if (accessor.InsnsSizeInCodeUnits() > inline_max_code_units) {
     LOG_FAIL(stats_, MethodCompilationStat::kNotInlinedCodeItem)
         << "Method " << method->PrettyMethod()
diff --git a/compiler/optimizing/sharpening.cc b/compiler/optimizing/sharpening.cc
index ebac3f6..1e29c21 100644
--- a/compiler/optimizing/sharpening.cc
+++ b/compiler/optimizing/sharpening.cc
@@ -191,14 +191,14 @@
         desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
       } else {
         // Not a boot image class.
-        DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
+        DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
       }
     } else {
       is_in_boot_image = (klass != nullptr) &&
           runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
       if (runtime->UseJitCompilation()) {
-        DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
+        DCHECK(!compiler_options.GetCompilePic());
         if (is_in_boot_image) {
           // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
           desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
@@ -331,14 +331,15 @@
         : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
     ObjPtr<mirror::String> string = nullptr;
 
-    if (codegen->GetCompilerOptions().IsBootImage()) {
+    const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
+    if (compiler_options.IsBootImage()) {
       // Compiling boot image. Resolve the string and allocate it if needed, to ensure
       // the string will be added to the boot image.
       DCHECK(!runtime->UseJitCompilation());
       string = class_linker->ResolveString(string_index, dex_cache);
       CHECK(string != nullptr);
       if (compiler_driver->GetSupportBootImageFixup()) {
-        DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
+        DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
         desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
       } else {
         // compiler_driver_test. Do not sharpen.
diff --git a/compiler/verifier_deps_test.cc b/compiler/verifier_deps_test.cc
index 3fe2ec0..c223549 100644
--- a/compiler/verifier_deps_test.cc
+++ b/compiler/verifier_deps_test.cc
@@ -129,7 +129,7 @@
     for (const DexFile* dex_file : dex_files_) {
       compiler_driver_->GetVerificationResults()->AddDexFile(dex_file);
     }
-    compiler_driver_->SetDexFilesForOatFile(dex_files_);
+    SetDexFilesForOatFile(dex_files_);
   }
 
   void LoadDexFile(ScopedObjectAccess& soa) REQUIRES_SHARED(Locks::mutator_lock_) {
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 779128f..be3f922 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -696,7 +696,7 @@
   }
 
   bool VerifyProfileData() {
-    return profile_compilation_info_->VerifyProfileData(dex_files_);
+    return profile_compilation_info_->VerifyProfileData(compiler_options_->dex_files_for_oat_file_);
   }
 
   void ParseInstructionSetVariant(const std::string& option, ParserOptions* parser_options) {
@@ -1491,8 +1491,8 @@
     if (profile_compilation_info_ != nullptr) {
       // TODO: The following comment looks outdated or misplaced.
       // Filter out class path classes since we don't want to include these in the image.
-      HashSet<std::string> image_classes =
-          profile_compilation_info_->GetClassDescriptors(dex_files_);
+      HashSet<std::string> image_classes = profile_compilation_info_->GetClassDescriptors(
+          compiler_options_->dex_files_for_oat_file_);
       VLOG(compiler) << "Loaded " << image_classes.size()
                      << " image class descriptors from profile";
       if (VLOG_IS_ON(compiler)) {
@@ -1653,10 +1653,11 @@
       }
     }
 
-    dex_files_ = MakeNonOwningPointerVector(opened_dex_files_);
+    compiler_options_->dex_files_for_oat_file_ = MakeNonOwningPointerVector(opened_dex_files_);
+    const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
 
     // If we need to downgrade the compiler-filter for size reasons.
-    if (!IsBootImage() && IsVeryLarge(dex_files_)) {
+    if (!IsBootImage() && IsVeryLarge(dex_files)) {
       // Disable app image to make sure dex2oat unloading is enabled.
       compiler_options_->DisableAppImage();
 
@@ -1689,7 +1690,7 @@
     CHECK(driver_ == nullptr);
     // If we use a swap file, ensure we are above the threshold to make it necessary.
     if (swap_fd_ != -1) {
-      if (!UseSwap(IsBootImage(), dex_files_)) {
+      if (!UseSwap(IsBootImage(), dex_files)) {
         close(swap_fd_);
         swap_fd_ = -1;
         VLOG(compiler) << "Decided to run without swap.";
@@ -1732,7 +1733,7 @@
     // Verification results are only required for modes that have any compilation. Avoid
     // adding the dex files if possible to prevent allocating large arrays.
     if (verification_results_ != nullptr) {
-      for (const auto& dex_file : dex_files_) {
+      for (const auto& dex_file : dex_files) {
         // Pre-register dex files so that we can access verification results without locks during
         // compilation and verification.
         verification_results_->AddDexFile(dex_file);
@@ -1750,7 +1751,7 @@
   // Doesn't return the class loader since it's not meant to be used for image compilation.
   void CompileDexFilesIndividually() {
     CHECK(!IsImage()) << "Not supported with image";
-    for (const DexFile* dex_file : dex_files_) {
+    for (const DexFile* dex_file : compiler_options_->dex_files_for_oat_file_) {
       std::vector<const DexFile*> dex_files(1u, dex_file);
       VLOG(compiler) << "Compiling " << dex_file->GetLocation();
       jobject class_loader = CompileDexFiles(dex_files);
@@ -1781,7 +1782,7 @@
     // mode (to reduce RAM used by the compiler).
     return !IsImage() &&
         !update_input_vdex_ &&
-        dex_files_.size() > 1 &&
+        compiler_options_->dex_files_for_oat_file_.size() > 1 &&
         !CompilerFilter::IsAotCompilationEnabled(compiler_options_->GetCompilerFilter());
   }
 
@@ -1808,11 +1809,12 @@
         class_path_files = class_loader_context_->FlattenOpenedDexFiles();
       }
 
+      const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
       std::vector<const DexFile*> no_inline_from_dex_files;
-      std::vector<const std::vector<const DexFile*>*> dex_file_vectors = {
+      const std::vector<const DexFile*>* dex_file_vectors[] = {
           &class_linker->GetBootClassPath(),
           &class_path_files,
-          &dex_files_
+          &dex_files
       };
       for (const std::vector<const DexFile*>* dex_file_vector : dex_file_vectors) {
         for (const DexFile* dex_file : *dex_file_vector) {
@@ -1851,7 +1853,6 @@
                                      thread_count_,
                                      swap_fd_,
                                      profile_compilation_info_.get()));
-    driver_->SetDexFilesForOatFile(dex_files_);
     if (!IsBootImage()) {
       driver_->SetClasspathDexFiles(class_loader_context_->FlattenOpenedDexFiles());
     }
@@ -1867,9 +1868,10 @@
     }
 
     // Setup vdex for compilation.
+    const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
     if (!DoEagerUnquickeningOfVdex() && input_vdex_file_ != nullptr) {
       callbacks_->SetVerifierDeps(
-          new verifier::VerifierDeps(dex_files_, input_vdex_file_->GetVerifierDepsData()));
+          new verifier::VerifierDeps(dex_files, input_vdex_file_->GetVerifierDepsData()));
 
       // TODO: we unquicken unconditionally, as we don't know
       // if the boot image has changed. How exactly we'll know is under
@@ -1879,11 +1881,11 @@
       // We do not decompile a RETURN_VOID_NO_BARRIER into a RETURN_VOID, as the quickening
       // optimization does not depend on the boot image (the optimization relies on not
       // having final fields in a class, which does not change for an app).
-      input_vdex_file_->Unquicken(dex_files_, /* decompile_return_instruction */ false);
+      input_vdex_file_->Unquicken(dex_files, /* decompile_return_instruction */ false);
     } else {
       // Create the main VerifierDeps, here instead of in the compiler since we want to aggregate
       // the results for all the dex files, not just the results for the current dex file.
-      callbacks_->SetVerifierDeps(new verifier::VerifierDeps(dex_files_));
+      callbacks_->SetVerifierDeps(new verifier::VerifierDeps(dex_files));
     }
     // Invoke the compilation.
     if (compile_individually) {
@@ -1891,7 +1893,7 @@
       // Return a null classloader since we already freed released it.
       return nullptr;
     }
-    return CompileDexFiles(dex_files_);
+    return CompileDexFiles(dex_files);
   }
 
   // Create the class loader, use it to compile, and return.
@@ -1900,7 +1902,8 @@
 
     jobject class_loader = nullptr;
     if (!IsBootImage()) {
-      class_loader = class_loader_context_->CreateClassLoader(dex_files_);
+      class_loader =
+          class_loader_context_->CreateClassLoader(compiler_options_->dex_files_for_oat_file_);
       callbacks_->SetDexFiles(&dex_files);
     }
 
@@ -2370,7 +2373,7 @@
     return dex_files_size >= min_dex_file_cumulative_size_for_swap_;
   }
 
-  bool IsVeryLarge(std::vector<const DexFile*>& dex_files) {
+  bool IsVeryLarge(const std::vector<const DexFile*>& dex_files) {
     size_t dex_files_size = 0;
     for (const auto* dex_file : dex_files) {
       dex_files_size += dex_file->GetHeader().file_size_;
@@ -2501,8 +2504,9 @@
   }
 
   void SaveDexInput() {
-    for (size_t i = 0; i < dex_files_.size(); ++i) {
-      const DexFile* dex_file = dex_files_[i];
+    const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
+    for (size_t i = 0, size = dex_files.size(); i != size; ++i) {
+      const DexFile* dex_file = dex_files[i];
       std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex",
                                              getpid(), i));
       std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str()));
@@ -2854,8 +2858,6 @@
   bool multi_image_;
   bool is_host_;
   std::string android_root_;
-  // Dex files we are compiling, does not include the class path dex files.
-  std::vector<const DexFile*> dex_files_;
   std::string no_inline_from_string_;
   CompactDexLevel compact_dex_level_ = kDefaultCompactDexLevel;
 
@@ -2898,7 +2900,7 @@
 
   // By default, copy the dex to the vdex file only if dex files are
   // compressed in APK.
-  CopyOption copy_dex_files_ = CopyOption::kOnlyIfCompressed;
+  linker::CopyOption copy_dex_files_ = linker::CopyOption::kOnlyIfCompressed;
 
   // The reason for invoking the compiler.
   std::string compilation_reason_;
diff --git a/dex2oat/dex2oat_options.cc b/dex2oat/dex2oat_options.cc
index dbb00c2..bf9edf7 100644
--- a/dex2oat/dex2oat_options.cc
+++ b/dex2oat/dex2oat_options.cc
@@ -222,10 +222,10 @@
       .Define("--force-determinism")
           .IntoKey(M::ForceDeterminism)
       .Define("--copy-dex-files=_")
-          .WithType<CopyOption>()
-          .WithValueMap({{"true", CopyOption::kOnlyIfCompressed},
-                         {"false", CopyOption::kNever},
-                         {"always", CopyOption::kAlways}})
+          .WithType<linker::CopyOption>()
+          .WithValueMap({{"true", linker::CopyOption::kOnlyIfCompressed},
+                         {"false", linker::CopyOption::kNever},
+                         {"always", linker::CopyOption::kAlways}})
           .IntoKey(M::CopyDexFiles)
       .Define("--classpath-dir=_")
           .WithType<std::string>()
diff --git a/dex2oat/dex2oat_options.def b/dex2oat/dex2oat_options.def
index 7be8e56..fe5c4e6 100644
--- a/dex2oat/dex2oat_options.def
+++ b/dex2oat/dex2oat_options.def
@@ -70,7 +70,7 @@
 DEX2OAT_OPTIONS_KEY (Unit,                           DumpTiming)
 DEX2OAT_OPTIONS_KEY (Unit,                           DumpPasses)
 DEX2OAT_OPTIONS_KEY (Unit,                           DumpStats)
-DEX2OAT_OPTIONS_KEY (CopyOption,                     CopyDexFiles)
+DEX2OAT_OPTIONS_KEY (linker::CopyOption,             CopyDexFiles)
 DEX2OAT_OPTIONS_KEY (Unit,                           AvoidStoringInvocation)
 DEX2OAT_OPTIONS_KEY (std::string,                    SwapFile)
 DEX2OAT_OPTIONS_KEY (int,                            SwapFileFd)
diff --git a/dex2oat/dex2oat_options.h b/dex2oat/dex2oat_options.h
index cc124c1..27d3d25 100644
--- a/dex2oat/dex2oat_options.h
+++ b/dex2oat/dex2oat_options.h
@@ -28,6 +28,7 @@
 #include "dex/compact_dex_level.h"
 #include "driver/compiler_options_map.h"
 #include "image.h"
+#include "linker/oat_writer.h"
 
 namespace art {
 
diff --git a/dex2oat/linker/image_test.h b/dex2oat/linker/image_test.h
index 2acdf25..66b37fb 100644
--- a/dex2oat/linker/image_test.h
+++ b/dex2oat/linker/image_test.h
@@ -63,9 +63,6 @@
   std::vector<ScratchFile> vdex_files;
   std::string image_dir;
 
-  void Compile(CompilerDriver* driver,
-               ImageHeader::StorageMode storage_mode);
-
   std::vector<size_t> GetImageObjectSectionSizes();
 
   ~CompilationHelper();
@@ -81,7 +78,7 @@
   void TestWriteRead(ImageHeader::StorageMode storage_mode);
 
   void Compile(ImageHeader::StorageMode storage_mode,
-               CompilationHelper& out_helper,
+               /*out*/ CompilationHelper& out_helper,
                const std::string& extra_dex = "",
                const std::initializer_list<std::string>& image_classes = {});
 
@@ -111,6 +108,8 @@
   }
 
  private:
+  void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
+
   HashSet<std::string> image_classes_;
 };
 
@@ -141,12 +140,13 @@
   return ret;
 }
 
-inline void CompilationHelper::Compile(CompilerDriver* driver,
-                                       ImageHeader::StorageMode storage_mode) {
+inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
+                                 /*out*/ CompilationHelper& out_helper) {
+  CompilerDriver* driver = compiler_driver_.get();
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
   std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
 
-  for (const std::unique_ptr<const DexFile>& dex_file : extra_dex_files) {
+  for (const std::unique_ptr<const DexFile>& dex_file : out_helper.extra_dex_files) {
     {
       ScopedObjectAccess soa(Thread::Current());
       // Inject in boot class path so that the compiler driver can see it.
@@ -157,7 +157,7 @@
 
   // Enable write for dex2dex.
   for (const DexFile* dex_file : class_path) {
-    dex_file_locations.push_back(dex_file->GetLocation());
+    out_helper.dex_file_locations.push_back(dex_file->GetLocation());
     if (dex_file->IsReadOnly()) {
       dex_file->EnableWrite();
     }
@@ -168,31 +168,31 @@
     for (int i = 0; i < static_cast<int>(class_path.size()); ++i) {
       std::string cur_location =
           android::base::StringPrintf("%s-%d.art", location.GetFilename().c_str(), i);
-      image_locations.push_back(ScratchFile(cur_location));
+      out_helper.image_locations.push_back(ScratchFile(cur_location));
     }
   }
   std::vector<std::string> image_filenames;
-  for (ScratchFile& file : image_locations) {
+  for (ScratchFile& file : out_helper.image_locations) {
     std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
     image_filenames.push_back(image_filename);
     size_t pos = image_filename.rfind('/');
     CHECK_NE(pos, std::string::npos) << image_filename;
-    if (image_dir.empty()) {
-      image_dir = image_filename.substr(0, pos);
-      int mkdir_result = mkdir(image_dir.c_str(), 0700);
-      CHECK_EQ(0, mkdir_result) << image_dir;
+    if (out_helper.image_dir.empty()) {
+      out_helper.image_dir = image_filename.substr(0, pos);
+      int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
+      CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
     }
-    image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
+    out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
   }
 
   std::vector<std::string> oat_filenames;
   std::vector<std::string> vdex_filenames;
   for (const std::string& image_filename : image_filenames) {
     std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
-    oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
+    out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
     oat_filenames.push_back(oat_filename);
     std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
-    vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
+    out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
     vdex_filenames.push_back(vdex_filename);
   }
 
@@ -224,7 +224,7 @@
       jobject class_loader = nullptr;
       TimingLogger timings("ImageTest::WriteRead", false, false);
       TimingLogger::ScopedTiming t("CompileAll", &timings);
-      driver->SetDexFilesForOatFile(class_path);
+      SetDexFilesForOatFile(class_path);
       driver->CompileAll(class_loader, class_path, &timings);
 
       t.NewTiming("WriteElf");
@@ -241,7 +241,7 @@
 
       std::vector<std::unique_ptr<ElfWriter>> elf_writers;
       std::vector<std::unique_ptr<OatWriter>> oat_writers;
-      for (ScratchFile& oat_file : oat_files) {
+      for (ScratchFile& oat_file : out_helper.oat_files) {
         elf_writers.emplace_back(CreateElfWriterQuick(driver->GetInstructionSet(),
                                                       driver->GetInstructionSetFeatures(),
                                                       &driver->GetCompilerOptions(),
@@ -270,7 +270,7 @@
         std::vector<std::unique_ptr<MemMap>> cur_opened_dex_files_maps;
         std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
         bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
-            vdex_files[i].GetFile(),
+            out_helper.vdex_files[i].GetFile(),
             rodata.back(),
             driver->GetInstructionSet(),
             driver->GetInstructionSetFeatures(),
@@ -297,8 +297,8 @@
       bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
       ASSERT_TRUE(image_space_ok);
 
-      DCHECK_EQ(vdex_files.size(), oat_files.size());
-      for (size_t i = 0, size = oat_files.size(); i != size; ++i) {
+      DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
+      for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
         MultiOatRelativePatcher patcher(driver->GetInstructionSet(),
                                         driver->GetInstructionSetFeatures(),
                                         driver->GetCompiledMethodStorage());
@@ -309,7 +309,7 @@
 
         std::unique_ptr<BufferedOutputStream> vdex_out =
             std::make_unique<BufferedOutputStream>(
-                std::make_unique<FileOutputStream>(vdex_files[i].GetFile()));
+                std::make_unique<FileOutputStream>(out_helper.vdex_files[i].GetFile()));
         oat_writer->WriteVerifierDeps(vdex_out.get(), nullptr);
         oat_writer->WriteQuickeningInfo(vdex_out.get());
         oat_writer->WriteChecksumsAndVdexHeader(vdex_out.get());
@@ -366,8 +366,7 @@
       const char* oat_filename = oat_filenames[i].c_str();
       std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename));
       ASSERT_TRUE(oat_file != nullptr);
-      bool success_fixup = ElfWriter::Fixup(oat_file.get(),
-                                            writer->GetOatDataBegin(i));
+      bool success_fixup = ElfWriter::Fixup(oat_file.get(), writer->GetOatDataBegin(i));
       ASSERT_TRUE(success_fixup);
       ASSERT_EQ(oat_file->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
                                                   << oat_filename;
@@ -389,7 +388,7 @@
   if (!extra_dex.empty()) {
     helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
   }
-  helper.Compile(compiler_driver_.get(), storage_mode);
+  DoCompile(storage_mode, helper);
   if (image_classes.begin() != image_classes.end()) {
     // Make sure the class got initialized.
     ScopedObjectAccess soa(Thread::Current());
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index b98dc68..bb730d3 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -442,7 +442,7 @@
   // Prepare dex cache array starts based on the ordering specified in the CompilerDriver.
   // Set the slot size early to avoid DCHECK() failures in IsImageBinSlotAssigned()
   // when AssignImageBinSlot() assigns their indexes out or order.
-  for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) {
+  for (const DexFile* dex_file : compiler_driver_.GetCompilerOptions().GetDexFilesForOatFile()) {
     auto it = dex_file_oat_index_map_.find(dex_file);
     DCHECK(it != dex_file_oat_index_map_.end()) << dex_file->GetLocation();
     ImageInfo& image_info = GetImageInfo(it->second);
@@ -1738,7 +1738,7 @@
   WorkStack work_stack;
 
   // Special case interned strings to put them in the image they are likely to be resolved from.
-  for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) {
+  for (const DexFile* dex_file : compiler_driver_.GetCompilerOptions().GetDexFilesForOatFile()) {
     auto it = dex_file_oat_index_map_.find(dex_file);
     DCHECK(it != dex_file_oat_index_map_.end()) << dex_file->GetLocation();
     const size_t oat_index = it->second;
diff --git a/dex2oat/linker/oat_writer.h b/dex2oat/linker/oat_writer.h
index ccafe05..e8eee88 100644
--- a/dex2oat/linker/oat_writer.h
+++ b/dex2oat/linker/oat_writer.h
@@ -27,7 +27,6 @@
 #include "base/os.h"
 #include "base/mem_map.h"
 #include "base/safe_map.h"
-#include "compiler.h"
 #include "debug/debug_info.h"
 #include "dex/compact_dex_level.h"
 #include "dex/method_reference.h"
@@ -64,6 +63,12 @@
 class MultiOatRelativePatcher;
 class OutputStream;
 
+enum class CopyOption {
+  kNever,
+  kAlways,
+  kOnlyIfCompressed
+};
+
 // OatHeader         variable length with count of D OatDexFiles
 //
 // TypeLookupTable[0] one descriptor to class def index hash table for each OatDexFile.
diff --git a/dex2oat/linker/oat_writer_test.cc b/dex2oat/linker/oat_writer_test.cc
index 2bc286a..e43a7f3 100644
--- a/dex2oat/linker/oat_writer_test.cc
+++ b/dex2oat/linker/oat_writer_test.cc
@@ -409,7 +409,7 @@
   jobject class_loader = nullptr;
   if (kCompile) {
     TimingLogger timings2("OatTest::WriteRead", false, false);
-    compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
+    SetDexFilesForOatFile(class_linker->GetBootClassPath());
     compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
   }
 
@@ -547,7 +547,7 @@
     ScopedObjectAccess soa(Thread::Current());
     class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader>(class_loader));
   }
-  compiler_driver_->SetDexFilesForOatFile(dex_files);
+  SetDexFilesForOatFile(dex_files);
   compiler_driver_->CompileAll(class_loader, dex_files, &timings);
 
   ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
diff --git a/libdexfile/dex/dex_file-inl.h b/libdexfile/dex/dex_file-inl.h
index f5dd374..0966859 100644
--- a/libdexfile/dex/dex_file-inl.h
+++ b/libdexfile/dex/dex_file-inl.h
@@ -17,12 +17,14 @@
 #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
 #define ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
 
+#include "dex_file.h"
+
 #include "base/casts.h"
 #include "base/leb128.h"
 #include "base/stringpiece.h"
 #include "class_iterator.h"
 #include "compact_dex_file.h"
-#include "dex_file.h"
+#include "dex_instruction_iterator.h"
 #include "invoke_type.h"
 #include "standard_dex_file.h"
 
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index a8cd187..67abdca 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -29,7 +29,6 @@
 #include "base/value_object.h"
 #include "class_iterator.h"
 #include "dex_file_types.h"
-#include "dex_instruction_iterator.h"
 #include "hidden_api_access_flags.h"
 #include "jni.h"
 #include "modifiers.h"
@@ -38,6 +37,7 @@
 
 class ClassDataItemIterator;
 class CompactDexFile;
+class DexInstructionIterator;
 enum InvokeType : uint32_t;
 class MemMap;
 class OatDexFile;
diff --git a/runtime/Android.bp b/runtime/Android.bp
index b347019..0345c2f 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -338,14 +338,10 @@
                 "thread_android.cc",
             ],
             shared_libs: [
-                // For android::FileMap used by libziparchive.
-                "libutils",
                 "libtombstoned_client",
             ],
             static_libs: [
-                // ZipArchive support, the order matters here to get all symbols.
-                "libziparchive",
-                "libz",
+                "libz",  // For adler32.
             ],
         },
         android_arm: {
@@ -367,8 +363,7 @@
                 "thread_linux.cc",
             ],
             shared_libs: [
-                "libziparchive",
-                "libz",
+                "libz",  // For adler32.
             ],
         },
     },
@@ -600,7 +595,6 @@
     ],
     shared_libs: [
         "libbacktrace",
-        "libziparchive",
     ],
     header_libs: [
         "art_cmdlineparser_headers", // For parsed_options_test.
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 8e3bbde..58becb1 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -37,7 +37,6 @@
 #include "base/systrace.h"
 #include "base/time_utils.h"
 #include "common_throws.h"
-#include "cutils/sched_policy.h"
 #include "debugger.h"
 #include "dex/dex_file-inl.h"
 #include "entrypoints/quick/quick_alloc_entrypoints.h"
diff --git a/runtime/hprof/hprof.cc b/runtime/hprof/hprof.cc
index 7bd5a6a..c9e8426 100644
--- a/runtime/hprof/hprof.cc
+++ b/runtime/hprof/hprof.cc
@@ -24,7 +24,6 @@
 
 #include "hprof.h"
 
-#include <cutils/open_memstream.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
diff --git a/runtime/jit/profiling_info_test.cc b/runtime/jit/profiling_info_test.cc
index 106a80a..8424610 100644
--- a/runtime/jit/profiling_info_test.cc
+++ b/runtime/jit/profiling_info_test.cc
@@ -31,7 +31,6 @@
 #include "mirror/class_loader.h"
 #include "profile/profile_compilation_info.h"
 #include "scoped_thread_state_change-inl.h"
-#include "ziparchive/zip_writer.h"
 
 namespace art {
 
diff --git a/test/003-omnibus-opcodes/build b/test/003-omnibus-opcodes/build
index 4d3fb37..c2e6112 100644
--- a/test/003-omnibus-opcodes/build
+++ b/test/003-omnibus-opcodes/build
@@ -17,12 +17,20 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-rm classes/UnresClass.class
-${JAVAC} -d classes `find src2 -name '*.java'`
+export ORIGINAL_JAVAC="$JAVAC"
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
+
+  $ORIGINAL_JAVAC "$@"
+  rm -f classes/UnresClass.class
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/004-JniTest/build b/test/004-JniTest/build
index e563d73..a786b8b 100755
--- a/test/004-JniTest/build
+++ b/test/004-JniTest/build
@@ -23,16 +23,18 @@
 # This enables the test to compile with vanilla RI javac and work on either ART or RI.
 #
 
+# Stop on failure.
+set -e
+
 export ORIGINAL_JAVAC="$JAVAC"
 
-# Delete CriticalNative.java, FastNative.java annotations after building the .class files.
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
 function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
   $ORIGINAL_JAVAC "$@"
-  local stat=$?
-
-  [[ -d classes ]] && (find classes/dalvik -name '*.class' | xargs rm -rf)
-
-  return $stat
+  # Delete CriticalNative.java, FastNative.java annotations after building the .class files.
+  find classes/dalvik -name '*.class' -exec rm {} \;
 }
 
 export -f javac_wrapper
@@ -40,28 +42,6 @@
 
 ######################################################################
 
-# Use the original dx with no extra magic or pessimizing flags.
-# This ensures that any default optimizations that dx do would not break JNI.
-
-export ORIGINAL_DX="$DX"
-
-# Filter out --debug flag from dx.
-function dx_wrapper {
-  local args=("$@")
-  local args_filtered=()
-  for i in "${args[@]}"; do
-    case "$i" in
-      --debug)
-        ;;
-      *)
-        args_filtered+=("$i")
-        ;;
-    esac
-  done
-  "$ORIGINAL_DX" "${args_filtered[@]}"
-}
-
-export -f dx_wrapper
-export DX=dx_wrapper
-
+# Use release mode to check optimizations do not break JNI.
+export D8_FLAGS=--release
 ./default-build "$@"
diff --git a/test/004-ReferenceMap/build b/test/004-ReferenceMap/build
index 3bb63ca..d928cd7 100644
--- a/test/004-ReferenceMap/build
+++ b/test/004-ReferenceMap/build
@@ -17,9 +17,26 @@
 # Stop if something fails.
 set -e
 
-# The test relies on DEX file produced by javac+dx so keep building with them for now
-# (see b/19467889)
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-${DX} -JXmx256m --debug --dex --output=classes.dex ${DX_FLAGS} classes
-zip $TEST_NAME.jar classes.dex
+# This test depends on the exact format of the DEX file. Since dx is deprecated,
+# the classes.dex file is packaged as a test input. It was created with:
+#
+# $ javac -g -Xlint:-options -source 1.7 -target 1.7 -d classes src/Main.java
+# $ dx --debug --dex --output=classes.dex classes
+
+# Wrapper function for javac which for this test does nothing as the
+# test uses a pre-built DEX file.
+function javac_wrapper {
+  # Nothing to compile, using dx generated classes.dex.
+  return 0
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+# Do not invoke D8 for this test.
+export D8=':'
+
+######################################################################
+
+jar -cf classes.jar classes.dex
+./default-build "$@"
diff --git a/test/004-ReferenceMap/classes.dex b/test/004-ReferenceMap/classes.dex
new file mode 100644
index 0000000..993c077
--- /dev/null
+++ b/test/004-ReferenceMap/classes.dex
Binary files differ
diff --git a/test/004-StackWalk/build b/test/004-StackWalk/build
index 3bb63ca..eeecbfc 100644
--- a/test/004-StackWalk/build
+++ b/test/004-StackWalk/build
@@ -17,9 +17,25 @@
 # Stop if something fails.
 set -e
 
-# The test relies on DEX file produced by javac+dx so keep building with them for now
-# (see b/19467889)
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-${DX} -JXmx256m --debug --dex --output=classes.dex ${DX_FLAGS} classes
-zip $TEST_NAME.jar classes.dex
+# This test depends on the exact format of the DEX file. Since dx is deprecated,
+# the classes.dex file is packaged as a test input. It was created with:
+#
+# $ javac -g -Xlint:-options -source 1.7 -target 1.7 -d classes src/Main.java
+# $ dx --debug --dex --output=classes.dex classes
+
+# Wrapper function for javac which for this test does nothing as the
+# test uses a pre-built DEX file.
+function javac_wrapper {
+  return 0
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+# Do not invoke D8 for this test.
+export D8=':'
+
+######################################################################
+
+jar -cf classes.jar classes.dex
+./default-build "$@"
diff --git a/test/004-StackWalk/classes.dex b/test/004-StackWalk/classes.dex
new file mode 100644
index 0000000..ad45296
--- /dev/null
+++ b/test/004-StackWalk/classes.dex
Binary files differ
diff --git a/test/005-annotations/build b/test/005-annotations/build
index 8eb07a9..5342eea 100644
--- a/test/005-annotations/build
+++ b/test/005-annotations/build
@@ -17,18 +17,28 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
+export ORIGINAL_JAVAC="$JAVAC"
 
-# android.test.anno.MissingAnnotation is available at compile time...
-${JAVAC} -d classes `find src -name '*.java'`
-# overwrite RenamedEnum
-${JAVAC} -d classes `find src2 -name '*.java'`
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-# ...but not at run time.
-rm 'classes/android/test/anno/MissingAnnotation.class'
-rm 'classes/android/test/anno/ClassWithInnerAnnotationClass$MissingInnerAnnotationClass.class'
+  $ORIGINAL_JAVAC "$@"
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+  # Classes available at compile time, but not at runtime.
+  rm -f classes/android/test/anno/MissingAnnotation.class
+  rm -f 'classes/android/test/anno/ClassWithInnerAnnotationClass$MissingInnerAnnotationClass.class'
+
+  # overwrite RenamedEnum in classes
+  if [ -f classes2/android/test/anno/RenamedEnumClass.java ] ; then
+    mv classes2/android/test/anno/RenamedEnumClass.java classes/android/test/anno/RenamedEnumClass.java
+  fi
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/022-interface/build b/test/022-interface/build
deleted file mode 100644
index ab1c822..0000000
--- a/test/022-interface/build
+++ /dev/null
@@ -1,22 +0,0 @@
-#!/bin/bash
-#
-# Copyright (C) 2012 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Stop if something fails.
-set -e
-
-${DX} --debug --dex --output=classes.dex classes
-
-zip $TEST_NAME.jar classes.dex
diff --git a/test/056-const-string-jumbo/build b/test/056-const-string-jumbo/build
index 47641d5..c1d711b 100644
--- a/test/056-const-string-jumbo/build
+++ b/test/056-const-string-jumbo/build
@@ -39,10 +39,4 @@
     printf("}\n") > fileName;
 }'
 
-mkdir classes
-${JAVAC} -d classes src/*.java
-
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx500m --debug --dex --no-optimize --positions=none --no-locals --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+./default-build "$@"
diff --git a/test/442-checker-constant-folding/build b/test/066-mismatched-super/build
old mode 100755
new mode 100644
similarity index 81%
rename from test/442-checker-constant-folding/build
rename to test/066-mismatched-super/build
index 42b99ad..c1c9ed3
--- a/test/442-checker-constant-folding/build
+++ b/test/066-mismatched-super/build
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Copyright 2017 The Android Open Source Project
+# Copyright 2018 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -14,7 +14,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# See b/65168732
-export DX=$ANDROID_HOST_OUT/bin/dx
-
-./default-build "$@"
+DESUGAR=false ./default-build "$@"
diff --git a/test/089-many-methods/build b/test/089-many-methods/build
index ff77c60..5b4cda8 100644
--- a/test/089-many-methods/build
+++ b/test/089-many-methods/build
@@ -43,8 +43,9 @@
     printf("}\n") > fileName;
 }'
 
-# The test relies on the error message produced by dx, not jack, so keep building with dx for now
-# (b/19467889).
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-${DX} -JXmx1024m --dex --no-optimize classes
+# Force DEX generation so test also passes with --jvm.
+export NEED_DEX=true
+
+# Specify old API level as d8 automagically produces a multidex file
+# when the API level is above 20. Failing the build here is deliberate.
+./default-build --api-level 20 "$@"
diff --git a/test/089-many-methods/check b/test/089-many-methods/check
index 65b7139..1f71e8e 100755
--- a/test/089-many-methods/check
+++ b/test/089-many-methods/check
@@ -14,7 +14,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Strip build error debug messages, as they are environment-specific.
-sed -e '/^Failed to build/d' -e '/^Non-canonical tmpdir/d' -e '/^Args:/d' -e '/^Max filename/d' -e '/^Max pathlength/d' "$2" > "$2.tmp"
-
-diff --strip-trailing-cr -q "$1" "$2.tmp" >/dev/null
\ No newline at end of file
+grep Error "$2" > "$2.tmp"
+diff --strip-trailing-cr -q "$1" "$2.tmp" >/dev/null
diff --git a/test/089-many-methods/expected.txt b/test/089-many-methods/expected.txt
index 786df7c..bb6ba3c 100644
--- a/test/089-many-methods/expected.txt
+++ b/test/089-many-methods/expected.txt
@@ -1,6 +1 @@
-
-trouble writing output: Too many field references to fit in one dex file: 131000; max is 65536.
-You may try using multi-dex. If multi-dex is enabled then the list of classes for the main dex list is too large.
-References by package:
-131000 default
-build exit status: 2
+Error: Cannot fit requested classes in a single dex file (# fields: 131000 > 65536)
diff --git a/test/091-override-package-private-method/build b/test/091-override-package-private-method/build
index e5fa669..8257d92 100755
--- a/test/091-override-package-private-method/build
+++ b/test/091-override-package-private-method/build
@@ -17,15 +17,20 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
+export ORIGINAL_JAVAC="$JAVAC"
 
-mkdir classes-ex
-mv classes/OverridePackagePrivateMethodSuper.class classes-ex
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
+  $ORIGINAL_JAVAC "$@"
+  mkdir -p classes-ex
+  mv classes/OverridePackagePrivateMethodSuper.class classes-ex
+}
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes-ex
-  zip ${TEST_NAME}-ex.jar classes.dex
-fi
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/111-unresolvable-exception/build b/test/111-unresolvable-exception/build
index f24c5b2..1c275aa 100644
--- a/test/111-unresolvable-exception/build
+++ b/test/111-unresolvable-exception/build
@@ -17,11 +17,22 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-rm classes/TestException.class
+export ORIGINAL_JAVAC="$JAVAC"
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
+
+  $ORIGINAL_JAVAC "$@"
+
+  # Remove class available at compile time but not at run time.
+  rm classes/TestException.class
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/113-multidex/build b/test/113-multidex/build
deleted file mode 100644
index 4ad7cb9..0000000
--- a/test/113-multidex/build
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/bin/bash
-#
-# Copyright (C) 2014 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-# Stop if something fails.
-set -e
-
-# All except Main
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-rm classes/Main.class
-
-# Only Main
-mkdir classes2
-${JAVAC} -d classes2 `find src -name '*.java'`
-rm classes2/Second.class classes2/FillerA.class classes2/FillerB.class classes2/Inf*.class
-
-if [ ${NEED_DEX} = "true" ]; then
-  # All except Main
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-
-  # Only Main
-  ${DX} -JXmx256m --debug --dex --output=classes2.dex classes2
-  zip $TEST_NAME.jar classes.dex classes2.dex
-fi
diff --git a/test/113-multidex/src/Main.java b/test/113-multidex/src-multidex/Main.java
similarity index 100%
rename from test/113-multidex/src/Main.java
rename to test/113-multidex/src-multidex/Main.java
diff --git a/test/124-missing-classes/build b/test/124-missing-classes/build
index b13aa6e..ec4ec84 100644
--- a/test/124-missing-classes/build
+++ b/test/124-missing-classes/build
@@ -17,16 +17,24 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
+export ORIGINAL_JAVAC="$JAVAC"
 
-# Some classes are available at compile time...
-${JAVAC} -d classes `find src -name '*.java'`
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
 
-# ...but not at run time.
-rm 'classes/MissingClass.class'
-rm 'classes/Main$MissingInnerClass.class'
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+  # Some classes are available at compile time...
+  $ORIGINAL_JAVAC "$@"
+
+  # ...but not at run time.
+  rm 'classes/MissingClass.class' 'classes/Main$MissingInnerClass.class'
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/126-miranda-multidex/build b/test/126-miranda-multidex/build
index b4bb88d..7b44863 100644
--- a/test/126-miranda-multidex/build
+++ b/test/126-miranda-multidex/build
@@ -17,21 +17,30 @@
 # Stop if something fails.
 set -e
 
-# All except MirandaInterface
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
-rm classes/MirandaInterface.class
+export ORIGINAL_JAVAC="$JAVAC"
 
-# Only MirandaInterface
-mkdir classes2
-${JAVAC} -d classes2 `find src -name '*.java'`
-rm classes2/Main.class classes2/MirandaAbstract.class classes2/MirandaClass*.class classes2/MirandaInterface2*.class
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-if [ ${NEED_DEX} = "true" ]; then
-  # All except Main
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
+  if [[ "$*" != *"classes2"* ]]; then
+    # First invocation: compile src/ files.
+    $ORIGINAL_JAVAC "$@"
+  else
+    # Second invocation: move MirandaInterface.class for placement in
+    # a secondary dex file. There are no other source files for the
+    # secondary DEX so no compilation required.
+    mv classes/MirandaInterface.class classes2
+  fi
+  return $?
+}
 
-  # Only Main
-  ${DX} -JXmx256m --debug --dex --output=classes2.dex classes2
-  zip $TEST_NAME.jar classes.dex classes2.dex
-fi
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+# Signal to default-build that this is a multidex test.
+mkdir src-multidex
+./default-build "$@"
diff --git a/test/127-checker-secondarydex/build b/test/127-checker-secondarydex/build
index c23b761..3135681 100755
--- a/test/127-checker-secondarydex/build
+++ b/test/127-checker-secondarydex/build
@@ -17,15 +17,22 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
+export ORIGINAL_JAVAC="$JAVAC"
 
-mkdir classes-ex
-mv classes/Super.class classes-ex
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes-ex
-  zip ${TEST_NAME}-ex.jar classes.dex
-fi
+  $ORIGINAL_JAVAC "$@"
+
+  mkdir classes-ex
+  mv classes/Super.class classes-ex
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/138-duplicate-classes-check2/build b/test/138-duplicate-classes-check2/build
index 3ff15aa..4ab7320 100755
--- a/test/138-duplicate-classes-check2/build
+++ b/test/138-duplicate-classes-check2/build
@@ -17,16 +17,22 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
-${JAVAC} -d classes `find src -name '*.java'`
+export ORIGINAL_JAVAC="$JAVAC"
 
-mkdir classes-ex
-${JAVAC} -d classes-ex `find src-ex -name '*.java'`
-rm classes-ex/A.class
+# Wrapper function for javac which invokes the compiler and applies
+# additional setup steps for the test.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes
-  zip ${TEST_NAME}.jar classes.dex
-  ${DX} -JXmx256m --debug --dex --output=classes.dex classes-ex
-  zip ${TEST_NAME}-ex.jar classes.dex
-fi
+  $ORIGINAL_JAVAC "$@"
+
+  # Remove one A.class from classes-ex
+  rm -f classes-ex/A.class
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+./default-build "$@"
diff --git a/test/1948-obsolete-const-method-handle/build b/test/1948-obsolete-const-method-handle/build
index ac0dcd9..d0e7a8c 100644
--- a/test/1948-obsolete-const-method-handle/build
+++ b/test/1948-obsolete-const-method-handle/build
@@ -20,6 +20,4 @@
 mkdir classes
 ./util-src/build-classes $PWD/classes
 
-${DX} --dex --min-sdk-version=28 --output=classes.dex classes
-
-zip $TEST_NAME.jar classes.dex
+./default-build --api-level 28 "$@"
diff --git a/test/303-verification-stress/build b/test/303-verification-stress/build
index ba79541..87a4a85 100644
--- a/test/303-verification-stress/build
+++ b/test/303-verification-stress/build
@@ -21,11 +21,4 @@
 gcc -Wall -Werror -o classes-gen classes-gen.c
 ./classes-gen
 
-mkdir classes
-${JAVAC} -d classes src/*.java
-
-# dx needs more memory for that test so do not pass Xmx option here.
-if [ ${NEED_DEX} = "true" ]; then
-  ${DX} --debug --dex --output=classes.dex classes
-  zip $TEST_NAME.jar classes.dex
-fi
+./default-build "$@"
diff --git a/test/638-no-line-number/build b/test/638-no-line-number/build
index 7eaf50e..9cd1955 100644
--- a/test/638-no-line-number/build
+++ b/test/638-no-line-number/build
@@ -17,9 +17,6 @@
 # Stop if something fails.
 set -e
 
-mkdir classes
 # Only keep the source name, to make sure we do remove it in the stack trace
 # when there is no line number mapping.
-${JAVAC} -g:source -source 7 -target 7 -d classes `find src -name '*.java'`
-${DX} --dex --output=classes.dex classes
-zip $TEST_NAME.jar classes.dex
+JAVAC_ARGS="$JAVAC_ARGS -g:source" ./default-build "$@"
diff --git a/test/638-no-line-number/expected.txt b/test/638-no-line-number/expected.txt
index ffde153..4b351f4 100644
--- a/test/638-no-line-number/expected.txt
+++ b/test/638-no-line-number/expected.txt
@@ -2,4 +2,4 @@
 	at Main.main(Unknown Source:2)
 java.lang.NullPointerException: throw with null exception
 	at Main.doThrow(Unknown Source:0)
-	at Main.main(Unknown Source:9)
+	at Main.main(Unknown Source:16)
diff --git a/test/712-varhandle-invocations/build b/test/712-varhandle-invocations/build
index 253765b..9a6e96e 100755
--- a/test/712-varhandle-invocations/build
+++ b/test/712-varhandle-invocations/build
@@ -32,8 +32,4 @@
 # Generate tests and Main that covers both the generated tests and manual tests
 python3 ./util-src/generate_java.py "${GENERATED_SRC}" ${MANUAL_TESTS}
 
-# Desugar is not happy with our Java 9 byte code, it shouldn't be necessary here anyway.
-export USE_DESUGAR=false
-
-# Invoke default build with increased heap size for dx
-./default-build "$@" --experimental var-handles --dx-vm-option -JXmx384m
+./default-build "$@" --experimental var-handles
diff --git a/test/715-clinit-implicit-parameter-annotations/build b/test/715-clinit-implicit-parameter-annotations/build
index 4753c8c..2b5f92c 100644
--- a/test/715-clinit-implicit-parameter-annotations/build
+++ b/test/715-clinit-implicit-parameter-annotations/build
@@ -17,8 +17,4 @@
 # Make us exit on a failure
 set -e
 
-# Always use D8 as DX does not support propagating parameter name and
-# access_flag information.
-export USE_D8=true
-
 ./default-build "$@" --experimental parameter-annotations
diff --git a/test/442-checker-constant-folding/build b/test/804-class-extends-itself/build
old mode 100755
new mode 100644
similarity index 66%
copy from test/442-checker-constant-folding/build
copy to test/804-class-extends-itself/build
index 42b99ad..71cb3ca
--- a/test/442-checker-constant-folding/build
+++ b/test/804-class-extends-itself/build
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Copyright 2017 The Android Open Source Project
+# Copyright 2018 The Android Open Source Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# See b/65168732
-export DX=$ANDROID_HOST_OUT/bin/dx
-
-./default-build "$@"
+# Use old API level to create DEX file with 035 version. Stricter
+# checking introduced with DEX file version 37 rejects class
+# otherwise (see DexFile::kClassDefinitionOrderEnforcedVersion).
+./default-build "$@" --api-level 13
diff --git a/test/952-invoke-custom/build b/test/952-invoke-custom/build
index 53d8228..a70fc20 100755
--- a/test/952-invoke-custom/build
+++ b/test/952-invoke-custom/build
@@ -14,34 +14,60 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# make us exit on a failure
+# Stop on failure.
 set -e
 
-ASM_JAR="${ANDROID_BUILD_TOP}/prebuilts/misc/common/asm/asm-6.0.jar"
-INTERMEDIATE_CLASSES=classes-intermediate
-CLASSES=classes
+export ASM_JAR="${ANDROID_BUILD_TOP}/prebuilts/misc/common/asm/asm-6.0.jar"
 
-# Create directory for intermediate classes
-rm -rf "${INTERMEDIATE_CLASSES}"
-mkdir "${INTERMEDIATE_CLASSES}"
+export ORIGINAL_JAVAC="$JAVAC"
 
-# Generate intermediate classes that will allow transform to be applied to test classes
-JAVAC_ARGS="${JAVAC_ARGS} -source 1.8 -target 1.8 -cp ${ASM_JAR}"
-${JAVAC:-javac} ${JAVAC_ARGS} -d ${INTERMEDIATE_CLASSES} $(find src -name '*.java')
+# Wrapper function for javac which invokes the compiler and applies
+# transforms to class files after compilation.
+function javac_wrapper {
+  set -e # Stop on error - the caller script may not have this set.
 
-# Create directory for transformed classes
-rm -rf "${CLASSES}"
-mkdir "${CLASSES}"
+  # Update arguments to add transformer and ASM to the compiler classpath.
+  local args=()
+  local classpath="./transformer.jar:$ASM_JAR"
+  while [ $# -ne 0 ] ; do
+    case $1 in
+      -cp|-classpath|--class-path)
+        shift
+        shift
+        args+=(-cp $classpath)
+        ;;
+      *)
+        args+=("$1")
+        shift
+        ;;
+    esac
+  done
 
-# Run transform
-for class in ${INTERMEDIATE_CLASSES}/*.class ; do
-  transformed_class=${CLASSES}/$(basename ${class})
-  ${JAVA:-java} -cp "${ASM_JAR}:${INTERMEDIATE_CLASSES}" transformer.IndyTransformer ${class} ${transformed_class}
-done
+  # Compile.
+  $ORIGINAL_JAVAC "${args[@]}"
 
-# Create DEX
-DX_FLAGS="${DX_FLAGS} --min-sdk-version=26 --debug --dump-width=1000"
-${DX} -JXmx256m --dex ${DX_FLAGS} --output=classes.dex ${CLASSES}
+  # Move original classes to intermediate location.
+  mv classes intermediate-classes
+  mkdir classes
 
-# Zip DEX to file name expected by test runner
-zip ${TEST_NAME:-classes-dex}.jar classes.dex
+  # Transform intermediate classes.
+  local transformer_args="-cp ${ASM_JAR}:transformer.jar transformer.IndyTransformer"
+  for class in intermediate-classes/*.class ; do
+    local transformed_class=classes/$(basename ${class})
+    ${JAVA:-java} ${transformer_args} $PWD/${class} ${transformed_class}
+  done
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+# Build the transformer to apply to compiled classes.
+mkdir classes
+${ORIGINAL_JAVAC:-javac} ${JAVAC_ARGS} -cp "${ASM_JAR}" -d classes $(find util-src -name '*.java')
+jar -cf transformer.jar -C classes transformer/ -C classes annotations/
+rm -rf classes
+
+# Use API level 28 for invoke-custom bytecode support.
+DESUGAR=false ./default-build "$@" --api-level 28
diff --git a/test/952-invoke-custom/src/annotations/BootstrapMethod.java b/test/952-invoke-custom/util-src/annotations/BootstrapMethod.java
similarity index 100%
rename from test/952-invoke-custom/src/annotations/BootstrapMethod.java
rename to test/952-invoke-custom/util-src/annotations/BootstrapMethod.java
diff --git a/test/952-invoke-custom/src/annotations/CalledByIndy.java b/test/952-invoke-custom/util-src/annotations/CalledByIndy.java
similarity index 100%
rename from test/952-invoke-custom/src/annotations/CalledByIndy.java
rename to test/952-invoke-custom/util-src/annotations/CalledByIndy.java
diff --git a/test/952-invoke-custom/src/annotations/Constant.java b/test/952-invoke-custom/util-src/annotations/Constant.java
similarity index 100%
rename from test/952-invoke-custom/src/annotations/Constant.java
rename to test/952-invoke-custom/util-src/annotations/Constant.java
diff --git a/test/952-invoke-custom/src/transformer/IndyTransformer.java b/test/952-invoke-custom/util-src/transformer/IndyTransformer.java
similarity index 97%
rename from test/952-invoke-custom/src/transformer/IndyTransformer.java
rename to test/952-invoke-custom/util-src/transformer/IndyTransformer.java
index 45cb476..d21dbbe 100644
--- a/test/952-invoke-custom/src/transformer/IndyTransformer.java
+++ b/test/952-invoke-custom/util-src/transformer/IndyTransformer.java
@@ -69,7 +69,7 @@
  *
  * <p>In the example above, this results in add() being replaced by invocations of magicAdd().
  */
-class IndyTransformer {
+public class IndyTransformer {
 
     static class BootstrapBuilder extends ClassVisitor {
 
@@ -164,10 +164,9 @@
     }
 
     private static void transform(Path inputClassPath, Path outputClassPath) throws Throwable {
+        URL url = inputClassPath.getParent().toUri().toURL();
         URLClassLoader classLoader =
-                new URLClassLoader(
-                        new URL[] {inputClassPath.toUri().toURL()},
-                        ClassLoader.getSystemClassLoader());
+                new URLClassLoader(new URL[] {url}, ClassLoader.getSystemClassLoader());
         String inputClassName = inputClassPath.getFileName().toString().replace(".class", "");
         Class<?> inputClass = classLoader.loadClass(inputClassName);
         Map<String, CalledByIndy> callsiteMap = new HashMap<>();
diff --git a/test/961-default-iface-resolution-gen/build b/test/961-default-iface-resolution-gen/build
index d719a9f..1d24589 100755
--- a/test/961-default-iface-resolution-gen/build
+++ b/test/961-default-iface-resolution-gen/build
@@ -22,5 +22,4 @@
 # Generate the smali files and expected.txt or fail
 ./util-src/generate_java.py ./src ./expected.txt
 
-# dx runs out of memory with default 256M, give it more memory.
-./default-build "$@" --experimental default-methods --dx-vm-option -JXmx1024M
+./default-build "$@" --experimental default-methods
diff --git a/test/964-default-iface-init-gen/build b/test/964-default-iface-init-gen/build
index e504690..1d24589 100755
--- a/test/964-default-iface-init-gen/build
+++ b/test/964-default-iface-init-gen/build
@@ -22,5 +22,4 @@
 # Generate the smali files and expected.txt or fail
 ./util-src/generate_java.py ./src ./expected.txt
 
-# dx runs out of memory with just 256m, so increase it.
-./default-build "$@" --experimental default-methods --dx-vm-option -JXmx1024M
+./default-build "$@" --experimental default-methods
diff --git a/test/979-const-method-handle/build b/test/979-const-method-handle/build
index 67fc2a6..4d22cb6 100755
--- a/test/979-const-method-handle/build
+++ b/test/979-const-method-handle/build
@@ -17,34 +17,41 @@
 # make us exit on a failure
 set -e
 
-ASM_JAR="${ANDROID_BUILD_TOP}/prebuilts/misc/common/asm/asm-6.0.jar"
-INTERMEDIATE_CLASSES=classes-intermediate
-TRANSFORMER_CLASSES=classes-transformer
-CLASSES=classes
+export ASM_JAR="${ANDROID_BUILD_TOP}/prebuilts/misc/common/asm/asm-6.0.jar"
 
-# Create directories for classes
-for class_dir in "${INTERMEDIATE_CLASSES}" "${TRANSFORMER_CLASSES}" "${CLASSES}"; do
-  rm -rf "${class_dir}"
-  mkdir "${class_dir}"
-done
+export ORIGINAL_JAVAC="$JAVAC"
 
-# Build transformer
-${JAVAC:-javac} ${JAVAC_ARGS} -cp "${ASM_JAR}" -d ${TRANSFORMER_CLASSES} $(find util-src -name '*.java')
+function javac_wrapper {
+  set -e
 
-# Generate intermediate classes that will allow transform to be applied to test classes
-JAVAC_ARGS="${JAVAC_ARGS} -source 1.8 -target 1.8"
-${JAVAC:-javac} ${JAVAC_ARGS} -cp ${TRANSFORMER_CLASSES} -d ${INTERMEDIATE_CLASSES} $(find src -name '*.java')
+  # Add annotation src files to our compiler inputs.
+  local asrcs=util-src/annotations/*.java
 
-# Run transform
-for class in ${INTERMEDIATE_CLASSES}/*.class ; do
-  transformed_class=${CLASSES}/$(basename ${class})
-  ${JAVA:-java} -cp "${ASM_JAR}:${TRANSFORMER_CLASSES}" \
-      transformer.ConstantTransformer ${class} ${transformed_class}
-done
+  # Compile.
+  $ORIGINAL_JAVAC "$@" $asrcs
 
-# Create DEX
-DX_FLAGS="${DX_FLAGS} --min-sdk-version=28 --debug --dump-width=1000"
-${DX} -JXmx256m --dex ${DX_FLAGS} --output=classes.dex ${CLASSES} ${TRANSFORMER_CLASSES}
+  # Move original classes to intermediate location.
+  mv classes intermediate-classes
+  mkdir classes
 
-# Zip DEX to file name expected by test runner
-zip ${TEST_NAME:-classes-dex}.jar classes.dex
+  # Transform intermediate classes.
+  local transformer_args="-cp ${ASM_JAR}:$PWD/transformer.jar transformer.ConstantTransformer"
+  for class in intermediate-classes/*.class ; do
+    local transformed_class=classes/$(basename ${class})
+    ${JAVA:-java} ${transformer_args} ${class} ${transformed_class}
+  done
+}
+
+export -f javac_wrapper
+export JAVAC=javac_wrapper
+
+######################################################################
+
+# Build the transformer to apply to compiled classes.
+mkdir classes
+${ORIGINAL_JAVAC:-javac} ${JAVAC_ARGS} -cp "${ASM_JAR}" -d classes $(find util-src -name '*.java')
+jar -cf transformer.jar -C classes transformer/ -C classes annotations/
+rm -rf classes
+
+# Use API level 28 for DEX file support constant method handles.
+./default-build "$@" --api-level 28
diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk
index ad292fd..53d4c37 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -19,13 +19,11 @@
 
 # Dependencies for actually running a run-test.
 TEST_ART_RUN_TEST_DEPENDENCIES := \
-  $(HOST_OUT_EXECUTABLES)/dx \
   $(HOST_OUT_EXECUTABLES)/d8 \
   $(HOST_OUT_EXECUTABLES)/d8-compat-dx \
   $(HOST_OUT_EXECUTABLES)/hiddenapi \
   $(HOST_OUT_EXECUTABLES)/jasmin \
-  $(HOST_OUT_EXECUTABLES)/smali \
-  $(HOST_OUT_JAVA_LIBRARIES)/desugar.jar
+  $(HOST_OUT_EXECUTABLES)/smali
 
 # We need dex2oat and dalvikvm on the target as well as the core images (all images as we sync
 # only once).
@@ -97,7 +95,7 @@
 # Host executables.
 host_prereq_rules := $(ART_TEST_HOST_RUN_TEST_DEPENDENCIES)
 
-# Required for dx, jasmin, smali.
+# Required for jasmin and smali.
 host_prereq_rules += $(TEST_ART_RUN_TEST_DEPENDENCIES)
 
 # Sync test files to the target, depends upon all things that must be pushed
diff --git a/test/etc/default-build b/test/etc/default-build
index 39f1a25..073ae2c 100755
--- a/test/etc/default-build
+++ b/test/etc/default-build
@@ -17,9 +17,14 @@
 # Stop if something fails.
 set -e
 
+function fail() {
+  echo "$*" >&2
+  exit 1
+}
+
 if [[ $# -le 0 ]]; then
   echo 'Error:' '$0 should have the parameters from the "build" script forwarded to it' >&2
-  echo 'Error: An example of how do it correctly is ./default-build "$@"'
+  fail 'Error: An example of how do it correctly is ./default-build "$@"'
   exit 1
 fi
 
@@ -98,7 +103,7 @@
   USE_HIDDENAPI=true
 fi
 
-# DESUGAR=false run-test... will disable desugar.
+# DESUGAR=false run-test... will disable desugaring.
 if [[ "$DESUGAR" == false ]]; then
   USE_DESUGAR=false
 fi
@@ -109,45 +114,25 @@
 WITH_ZIP_ALIGN=false
 ZIP_ALIGN_BYTES="-1"
 
-DX_FLAGS="--min-sdk-version=26"
-DX_VM_FLAGS=""
-EXPERIMENTAL=""
-
 BUILD_MODE="target"
 DEV_MODE="no"
 
-# The key for default arguments if no experimental things are enabled.
 DEFAULT_EXPERIMENT="no-experiment"
 
+# The key for default arguments if no experimental things are enabled.
+EXPERIMENTAL=$DEFAULT_EXPERIMENT
+
 # Setup experimental API level mappings in a bash associative array.
 declare -A EXPERIMENTAL_API_LEVEL
+EXPERIMENTAL_API_LEVEL[${DEFAULT_EXPERIMENT}]="26"
 EXPERIMENTAL_API_LEVEL["default-methods"]="24"
 EXPERIMENTAL_API_LEVEL["parameter-annotations"]="25"
 EXPERIMENTAL_API_LEVEL["agents"]="26"
 EXPERIMENTAL_API_LEVEL["method-handles"]="26"
 EXPERIMENTAL_API_LEVEL["var-handles"]="28"
 
-declare -A JAVAC_EXPERIMENTAL_ARGS
-JAVAC_EXPERIMENTAL_ARGS["default-methods"]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS["lambdas"]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS["method-handles"]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS["parameter-annotations"]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS["var-handles"]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-source 1.8 -target 1.8"
-JAVAC_EXPERIMENTAL_ARGS["agents"]="-source 1.8 -target 1.8"
-
 while true; do
-  if [ "x$1" = "x--dx-option" ]; then
-    shift
-    option="$1"
-    DX_FLAGS="${DX_FLAGS} $option"
-    shift
-  elif [ "x$1" = "x--dx-vm-option" ]; then
-    shift
-    option="$1"
-    DX_VM_FLAGS="${DX_VM_FLAGS} $option"
-    shift
-  elif [ "x$1" = "x--no-src" ]; then
+  if [ "x$1" = "x--no-src" ]; then
     HAS_SRC=false
     shift
   elif [ "x$1" = "x--no-src2" ]; then
@@ -168,11 +153,14 @@
   elif [ "x$1" = "x--no-jasmin" ]; then
     HAS_JASMIN=false
     shift
+  elif [ "x$1" = "x--api-level" ]; then
+    shift
+    EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]=$1
+    shift
   elif [ "x$1" = "x--experimental" ]; then
     shift
     # We have a specific experimental configuration so don't use the default.
-    DEFAULT_EXPERIMENT=""
-    EXPERIMENTAL="${EXPERIMENTAL} $1"
+    EXPERIMENTAL="$1"
     shift
   elif [ "x$1" = "x--zip-compression-method" ]; then
     # Allow using different zip compression method, e.g. 'store'
@@ -198,30 +186,25 @@
     DEV_MODE="yes"
     shift
   elif expr "x$1" : "x--" >/dev/null 2>&1; then
-    echo "unknown $0 option: $1" 1>&2
-    exit 1
+    fail "unknown $0 option: $1"
   else
     break
   fi
 done
 
 if [[ $BUILD_MODE == jvm ]]; then
-  # Does not need desugar on jvm because it supports the latest functionality.
+  # Does not need desugaring on jvm because it supports the latest functionality.
   USE_DESUGAR=false
   # Do not attempt to build src-art directories on jvm, it would fail without libcore.
   HAS_SRC_ART=false
 fi
 
-# Add args from the experimental mappings.
-for experiment in ${EXPERIMENTAL} ${DEFAULT_EXPERIMENT}; do
-  JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}"
-done
+# Set API level for smali and d8.
+API_LEVEL="${EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]}"
 
-for experiment in ${EXPERIMENTAL}; do
-  SMALI_ARGS="${SMALI_ARGS} --api ${EXPERIMENTAL_API_LEVEL[${experiment}]}"
-  DX_FLAGS="${DX_FLAGS} --min-sdk-version=${EXPERIMENTAL_API_LEVEL[${experiment}]}"
-  D8_FLAGS="--min-api ${EXPERIMENTAL_API_LEVEL[${experiment}]}"
-done
+# Add API level arguments to smali and dx
+SMALI_ARGS="${SMALI_ARGS} --api $API_LEVEL"
+D8_FLAGS="${D8_FLAGS} --min-api $API_LEVEL"
 
 #########################################
 
@@ -258,16 +241,6 @@
   fi
 }
 
-function desugar() {
-  local desugar_args="--mode=$BUILD_MODE"
-
-  if [[ $DEV_MODE == yes ]]; then
-    desugar_args="$desugar_args --show-commands"
-  fi
-
-  "$DESUGAR" --core-only $desugar_args "$@"
-}
-
 # Like regular javac but may include libcore on the bootclasspath.
 function javac_with_bootclasspath {
   local helper_args="--mode=$BUILD_MODE"
@@ -280,39 +253,34 @@
   "$ANDROID_BUILD_TOP/art/tools/javac-helper.sh" --core-only $helper_args ${JAVAC_ARGS} "$@"
 }
 
-# Make a "dex" file given a directory of classes in $1.
-# Also calls desugar on the classes first to convert lambdas.
+# Make a "dex" file given a directory of classes in $1. This will be
+# packaged in a jar file.
 function make_dex() {
   local name="$1"
-
-  local dx_input
-  if [[ "$USE_DESUGAR" != "true" ]]; then
-    # Use d8 with --no-desugaring for the 3 tests that opt out of desugaring (b/110150973).
-    local d8_inputs=$(find classes -name '*.class' -type f)
-    ${D8} ${D8_FLAGS} --debug --no-desugaring --output ${name}.jar $d8_inputs && \
-      jar -xf ${name}.jar ${name}.dex && \
-      rm ${name}.jar
-    return $?
+  local d8_inputs=$(find $name -name '*.class' -type f)
+  local d8_output=${name}.jar
+  local dex_output=${name}.dex
+  local d8_local_flags=""
+  if [[ "$USE_DESUGAR" = "true" ]]; then
+    local boot_class_path_list=$($ANDROID_BUILD_TOP/art/tools/bootjars.sh --$BUILD_MODE --core --path)
+    for boot_class_path_element in $boot_class_path_list; do
+      d8_local_flags="$d8_local_flags --classpath $boot_class_path_element"
+    done
+  else
+    d8_local_flags="$d8_local_flags --no-desugaring"
   fi
-
-  # Make a jar first so desugar doesn't need every .class file individually.
-  jar cf "$name.before-desugar.jar" -C "$name" .
-
-  # Make desugared JAR.
-  dx_input="${name}.desugar.jar"
-  desugar --input "$name.before-desugar.jar" --output "$dx_input"
-
-  local dexer="${DX}"
-  if [[ "${USE_D8}" != "false" ]]; then
-    dexer="${ANDROID_HOST_OUT}/bin/d8-compat-dx"
+  if [ "$DEV_MODE" = "yes" ]; then
+    echo ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
   fi
+  ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
 
-  # Make dex file from desugared JAR.
-  local dexer_flags="${DX_FLAGS} --debug --dex"
-  if [ $DEV_MODE = "yes" ]; then
-    echo ${dexer} -JXmx256m ${DX_VM_FLAGS} $dexer_flags --output=${name}.dex "${dx_input}"
+  # D8 outputs to JAR files today rather than DEX files as DX used
+  # to. To compensate, we extract the DEX from d8's output to meet the
+  # expectations of make_dex callers.
+  if [ "$DEV_MODE" = "yes" ]; then
+    echo unzip -p $d8_output classes.dex \> $dex_output
   fi
-  ${dexer} -JXmx256m ${DX_VM_FLAGS} $dexer_flags --output=${name}.dex "${dx_input}"
+  unzip -p $d8_output classes.dex > $dex_output
 }
 
 # Merge all the dex files in $1..$N into $1. Skip non-existing files, but at least 1 file must exist.
@@ -342,11 +310,10 @@
 
   # We assume the dexer did all the API level checks and just merge away.
   mkdir d8_merge_out
-  ${DXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}"
+  ${DEXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}"
 
   if [[ -e "./d8_merge_out/classes2.dex" ]]; then
-    echo "Cannot merge all dex files into a single dex"
-    exit 1
+    fail "Cannot merge all dex files into a single dex"
   fi
 
   mv ./d8_merge_out/classes.dex "$dst_file";
@@ -426,7 +393,9 @@
     javac_with_bootclasspath -classpath classes -d classes `find src2 -name '*.java'`
   fi
 
-  if [[ "${HAS_SRC}" == "true" || "${HAS_SRC2}" == "true" || "${HAS_SRC_ART}" == "true" ]]; then
+  # If the classes directory is not-empty, package classes in a DEX file. NB some
+  # tests provide classes rather than java files.
+  if [ "$(ls -A classes)" ]; then
     if [ ${NEED_DEX} = "true" ]; then
       make_dex classes
     fi
@@ -450,8 +419,7 @@
   # Compile Smali classes
   ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
   if [[ ! -s smali_classes.dex ]] ; then
-    echo ${SMALI} produced no output. >&2
-    exit 1
+    fail "${SMALI} produced no output."
   fi
   # Merge smali files into classes.dex, this takes priority over any jasmin files.
   make_dexmerge classes.dex smali_classes.dex
@@ -479,7 +447,6 @@
   make_dexmerge classes2.dex smali_classes2.dex
 fi
 
-
 if [ ${HAS_SRC_EX} = "true" ]; then
   # Build src-ex into classes-ex.
   # Includes 'src', 'src-art' source when compiling classes-ex, but exclude their .class files.
@@ -524,7 +491,7 @@
 
 # Create a single dex jar with two dex files for multidex.
 if [ ${NEED_DEX} = "true" ]; then
-  if $(has_multidex); then
+  if [ -f classes2.dex ] ; then
     zip $TEST_NAME.jar classes.dex classes2.dex
   else
     zip $TEST_NAME.jar classes.dex
diff --git a/test/run-test b/test/run-test
index b5b4285..d90eccd 100755
--- a/test/run-test
+++ b/test/run-test
@@ -41,7 +41,7 @@
 fi
 checker="${progdir}/../tools/checker/checker.py"
 export JAVA="java"
-export JAVAC="javac -g -Xlint:-options"
+export JAVAC="javac -g -Xlint:-options -source 1.8 -target 1.8"
 export RUN="${progdir}/etc/run-test-jar"
 export DEX_LOCATION=/data/run-test/${test_dir}
 export NEED_DEX="true"
@@ -56,10 +56,10 @@
 
 # If dx was not set by the environment variable, assume it is in the path.
 if [ -z "$DX" ]; then
-  export DX="dx"
+  export DX="d8-compat-dx"
 fi
 
-export DXMERGER="$D8"
+export DEXMERGER="$D8"
 
 # If jasmin was not set by the environment variable, assume it is in the path.
 if [ -z "$JASMIN" ]; then
@@ -675,13 +675,6 @@
     shift
 fi
 
-# For building with javac and dx always use Java 7. The dx compiler
-# only support byte codes from Java 7 or earlier (class file major
-# version 51 or lower).
-if [ "$NEED_DEX" = "true" ]; then
-  export JAVAC="${JAVAC} -source 1.7 -target 1.7"
-fi
-
 if [ "$usage" = "yes" ]; then
     prog=`basename $prog`
     (
diff --git a/tools/desugar.sh b/tools/desugar.sh
deleted file mode 100755
index 7f73852..0000000
--- a/tools/desugar.sh
+++ /dev/null
@@ -1,95 +0,0 @@
-#!/bin/bash
-#
-# Copyright (C) 2017 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-#
-# Calls desugar.jar with the --bootclasspath_entry values passed in automatically.
-# (This avoids having to manually set a boot class path).
-#
-#
-# Script-specific args:
-#   --mode=[host|target]: Select between host or target bootclasspath (default target).
-#   --core-only:          Use only "core" bootclasspath (e.g. do not include framework).
-#   --show-commands:      Print the desugar command being executed.
-#   --help:               Print above list of args.
-#
-# All other args are forwarded to desugar.jar
-#
-
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
-TOP=$DIR/../..
-
-pushd "$TOP" >/dev/null # back to android root.
-
-out=${OUT_DIR:-out}
-desugar_jar=$out/host/linux-x86/framework/desugar.jar
-
-if ! [[ -f $desugar_jar ]]; then
-  echo "Error: Missing $desugar_jar; did you do a build?" >&2
-  exit 1
-fi
-
-desugar_jar=$(readlink -f "$desugar_jar") # absolute path to desugar jar
-popd >/dev/null
-
-bootjars_args=
-mode=target
-showcommands=n
-while true; do
-  case $1 in
-    --help)
-      echo "Usage: $0 [--mode=host|target] [--core-only] [--show-commands] <desugar args>"
-      exit 0
-      ;;
-    --mode=host)
-      bootjars_args="$bootjars_args --host"
-      ;;
-    --mode=target)
-      bootjars_args="$bootjars_args --target"
-      ;;
-    --mode=*)
-      echo "Unsupported $0 usage with --mode=$1" >&2
-      exit 1
-      ;;
-    --core-only)
-      bootjars_args="$bootjars_args --core"
-      ;;
-    --show-commands)
-      showcommands=y
-      ;;
-    *)
-      break
-      ;;
-  esac
-  shift
-done
-
-desugar_args=(--min_sdk_version=10000)
-boot_class_path_list=$($TOP/art/tools/bootjars.sh $bootjars_args --path)
-
-for path in $boot_class_path_list; do
-  desugar_args+=(--bootclasspath_entry="$path")
-done
-
-if [[ ${#desugar_args[@]} -eq 0 ]]; then
-  echo "FATAL: Missing bootjars.sh file path list" >&2
-  exit 1
-fi
-
-if [[ $showcommands == y ]]; then
-  echo java -jar "$desugar_jar" "${desugar_args[@]}" "$@"
-fi
-
-java -jar "$desugar_jar" "${desugar_args[@]}" "$@"