Do not replace the direct edge between rdeps and java_sdk_library

android/prebuilt.go#isSelected has a special-case inside it to ignore
apex_contributions contents for the top-level java_sdk_library hook.
This was necessary because even though we might want source stubs in
next builds, we still needed the top-level prebuilt hook to be active to
emit the dexpreopt rules to .mk.

This worked fine for rdeps that create a dependency edge on the child
stub modules of java_sdk_library. Notable examples include the full
android api stubs created by f/b/api.go. In postdeps mutator, these
expanded deps get rewritten to source/prebuilt if necesssry.

The exception to this are workflows which depend on the top-level hook
directly via `libs`. We resolve these rdeps to an appropriate provider
during GenerateAndroidBuildActions stage. This meant that rdeps were
getting prebuilt stubs of these even in next builds.

Bug: 323454855

Test: Added a unit test

Test: lunch cf_x86_64_only_phone-next-userdebug
Test: aninja -t query
out/soong/.intermediates/packages/modules/Permission/SafetyCenter/Config/safety-center-config/android_common/javac/safety-center-config.jar
| grep prebilts/module_sdk # empty now

Change-Id: Id91333d88055519f3c58ab40466f9628085f5180
diff --git a/android/prebuilt.go b/android/prebuilt.go
index a94f5b7..2896dbd 100644
--- a/android/prebuilt.go
+++ b/android/prebuilt.go
@@ -547,13 +547,29 @@
 	if p := GetEmbeddedPrebuilt(m); p != nil {
 		bmn, _ := m.(baseModuleName)
 		name := bmn.BaseModuleName()
+		psi := PrebuiltSelectionInfoMap{}
+		ctx.VisitDirectDepsWithTag(acDepTag, func(am Module) {
+			psi, _ = OtherModuleProvider(ctx, am, PrebuiltSelectionInfoProvider)
+		})
+
 		if p.properties.UsePrebuilt {
 			if p.properties.SourceExists {
 				ctx.ReplaceDependenciesIf(name, func(from blueprint.Module, tag blueprint.DependencyTag, to blueprint.Module) bool {
+					if sdkLibrary, ok := m.(interface{ SdkLibraryName() *string }); ok && sdkLibrary.SdkLibraryName() != nil {
+						// Do not replace deps to the top-level prebuilt java_sdk_library hook.
+						// This hook has been special-cased in #isSelected to be _always_ active, even in next builds
+						// for dexpreopt and hiddenapi processing.
+						// If we do not special-case this here, rdeps referring to a java_sdk_library in next builds via libs
+						// will get prebuilt stubs
+						// TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
+						if psi.IsSelected(*sdkLibrary.SdkLibraryName()) {
+							return false
+						}
+					}
+
 					if t, ok := tag.(ReplaceSourceWithPrebuilt); ok {
 						return t.ReplaceSourceWithPrebuilt()
 					}
-
 					return true
 				})
 			}
@@ -584,6 +600,7 @@
 		sln := proptools.String(sdkLibrary.SdkLibraryName())
 		// This is the top-level library
 		// Do not supersede the existing prebuilts vs source selection mechanisms
+		// TODO (b/308187268): Remove this after the apexes have been added to apex_contributions
 		if sln == m.base().BaseModuleName() {
 			return false
 		}