Merge cherrypicks of ['googleplex-android-review.googlesource.com/39173011'] into 26Q2-release.

Change-Id: I69e0dafe50ab4cd7550636a131f9a5a3f95a0c0e
diff --git a/build/mainline_modules_sdks.py b/build/mainline_modules_sdks.py
index 9a91edb..0ef7ec8 100755
--- a/build/mainline_modules_sdks.py
+++ b/build/mainline_modules_sdks.py
@@ -1019,7 +1019,13 @@
 Baklava_1 = BuildRelease(
     name="Baklava-1",
     creator=create_sdk_snapshots_in_soong,
-    soong_env={},
+    soong_env={
+        # This release is a minor SDK bump and is simply a copy of Baklava since
+        # it isn't possible to target minor SDKs with `min_sdk_version` etc.
+        # Going forward we should try to avoid adding new build releases for
+        # minor SDK bumps.
+        "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "Baklava",
+    },
     preferHandling=PreferHandling.USE_NO_PREFER_PROPERTY,
 )
 CinnamonBun = BuildRelease(
@@ -1039,9 +1045,11 @@
 NEXT = BuildRelease(
     name="next",
     creator=create_latest_sdk_snapshots,
-    # There are no build release specific environment variables to pass to
-    # Soong.
-    soong_env={},
+    soong_env={
+        # "next" isn't a valid target build release, so override the default
+        # logic to set it from the name.
+        "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "current",
+    },
     generate_gantry_metadata_and_api_diff=True,
     # Starting with V, setting `prefer|use_source_config_var` on soong modules
     # in prebuilts/module_sdk is not necessary.
@@ -1056,9 +1064,11 @@
 LATEST = BuildRelease(
     name="latest",
     creator=create_latest_sdk_snapshots,
-    # There are no build release specific environment variables to pass to
-    # Soong.
-    soong_env={},
+    soong_env={
+        # "latest" isn't a valid target build release, so override the default
+        # logic to set it from the name.
+        "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "current",
+    },
     # Latest must include flagged APIs because it may be dropped into the main
     # Android branches.
     include_flagged_apis=True,
diff --git a/build/mainline_modules_sdks_test.py b/build/mainline_modules_sdks_test.py
index e64bff8..0fe7f18 100644
--- a/build/mainline_modules_sdks_test.py
+++ b/build/mainline_modules_sdks_test.py
@@ -446,17 +446,19 @@
                 ["com.android.ipsec", "com.google.android.wifi"],
             ),
             (
-                "latest",
-                {},
+                "S",
+                {
+                    "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "S"
+                },
                 [
                     "com.android.art", "com.android.ipsec",
                     "com.google.android.wifi"
                 ],
             ),
             (
-                "S",
+                "latest",
                 {
-                    "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "S"
+                    "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": "current"
                 },
                 [
                     "com.android.art", "com.android.ipsec",