Allow selective R8 optimization for eng test_suites

Eng builds implicitly run R8 in debug mode. This is typically fine, but
test_suites can be built in eng mode, and some tests exercise behavior
that may require R8 optimizations. For example, annotation tests that
check the effective dex output in the presence of such annotations.

Allow a target to override this default behavior by adding "--release"
to its dxflags property.

Bug: 222468116
Test: atest InternalAnnotationsTests
Change-Id: Ie3328f1b56a6fe7c9f331281e6527e40f17f9271
diff --git a/java/dex.go b/java/dex.go
index e0e642c..faf51a3 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -220,14 +220,28 @@
 		deps = append(deps, f)
 	}
 
+	var requestReleaseMode bool
+	requestReleaseMode, flags = android.RemoveFromList("--release", flags)
+
 	if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
 		flags = append(flags, "--debug")
+		requestReleaseMode = false
 	}
 
 	if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
 		flags = append(flags,
 			"--debug",
 			"--verbose")
+		requestReleaseMode = false
+	}
+
+	// Don't strip out debug information for eng builds, unless the target
+	// explicitly provided the `--release` build flag. This allows certain
+	// test targets to remain optimized as part of eng test_suites builds.
+	if requestReleaseMode {
+		flags = append(flags, "--release")
+	} else if ctx.Config().Eng() {
+		flags = append(flags, "--debug")
 	}
 
 	// Supplying the platform build flag disables various features like API modeling and desugaring.
@@ -384,11 +398,6 @@
 	// TODO(ccross): if this is an instrumentation test of an obfuscated app, use the
 	// dictionary of the app and move the app from libraryjars to injars.
 
-	// Don't strip out debug information for eng builds.
-	if ctx.Config().Eng() {
-		r8Flags = append(r8Flags, "--debug")
-	}
-
 	// TODO(b/180878971): missing classes should be added to the relevant builds.
 	// TODO(b/229727645): do not use true as default for Android platform builds.
 	if proptools.BoolDefault(opt.Ignore_warnings, true) {