ART: Retire desugar and dx in run-tests

Run-tests now use d8 for desugaring and dexing.

Build scripts no longer have hand written dexer invocations.

Bug: 110150973
Bug: 73711890
Test: art/test.py --host --64 -r
Test: art/test.py --target --prebuild --optimizing -r
Change-Id: Iae2199cf756624c1f045de7777c5ee6432398bd2
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/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[@]}" "$@"