Restrict R8 -libraryjars refs for stable targets

The current R8 dex pipeline gathers transitive header jars to avoid
issues with missing class definitions. While this is benign for
platform targets not using a stable SDK, it can introduce issues
for stable targets (e.g., duplicate class defs for mainline framework
modules).

Bypass this transitive jar referencing for such targets that should
reference explicit, stable, stub API libraries. A follow-up change
will resolve specific issues with mainline system server targets.

Bug: 360905238
Test: m
Change-Id: I11a136d33ca9dd0dfff436c7045bf1807d17b1e7
diff --git a/java/dex.go b/java/dex.go
index d88e8f8..6c739a2 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -289,15 +289,18 @@
 	// - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
 	// - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
 	// See b/20667396
-	var proguardRaiseDeps classpath
-	ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
-		if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
-			proguardRaiseDeps = append(proguardRaiseDeps, dep.RepackagedHeaderJars...)
-		}
-	})
+	// TODO(b/360905238): Remove SdkSystemServer exception after resolving missing class references.
+	if !dexParams.sdkVersion.Stable() || dexParams.sdkVersion.Kind == android.SdkSystemServer {
+		var proguardRaiseDeps classpath
+		ctx.VisitDirectDepsWithTag(proguardRaiseTag, func(m android.Module) {
+			if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
+				proguardRaiseDeps = append(proguardRaiseDeps, dep.RepackagedHeaderJars...)
+			}
+		})
+		r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
+		r8Deps = append(r8Deps, proguardRaiseDeps...)
+	}
 
-	r8Flags = append(r8Flags, proguardRaiseDeps.FormJavaClassPath("-libraryjars"))
-	r8Deps = append(r8Deps, proguardRaiseDeps...)
 	r8Flags = append(r8Flags, flags.bootClasspath.FormJavaClassPath("-libraryjars"))
 	r8Deps = append(r8Deps, flags.bootClasspath...)
 	r8Flags = append(r8Flags, flags.dexClasspath.FormJavaClassPath("-libraryjars"))