Fix aapt2 --min-sdk-version after finalized SDK

aapt2 --min-sdk-version was using AppsDefaultVersionName(), which
is OMR1 for a non-finalized SDK, but 8.1.0 after finalization.
Add PlatformSdkCodename() for non-finalized SDKs, use it for
DefaultAppTargetSdk(), and pass it for aapt2 --min-sdk-version.

Bug: 78224641
Test: TestAppSdkVersion in app_test.go
Change-Id: I622eaf92f8a940f79007c2a579536da325700b06
diff --git a/android/config.go b/android/config.go
index 7122f48..40ba8c1 100644
--- a/android/config.go
+++ b/android/config.go
@@ -476,6 +476,10 @@
 	return strconv.Itoa(c.PlatformSdkVersionInt())
 }
 
+func (c *config) PlatformSdkCodename() string {
+	return String(c.productVariables.Platform_sdk_codename)
+}
+
 func (c *config) MinSupportedSdkVersion() int {
 	return 14
 }
@@ -488,6 +492,14 @@
 	}
 }
 
+func (c *config) DefaultAppTargetSdk() string {
+	if Bool(c.productVariables.Platform_sdk_final) {
+		return c.PlatformSdkVersion()
+	} else {
+		return c.PlatformSdkCodename()
+	}
+}
+
 func (c *config) AppsDefaultVersionName() string {
 	return String(c.productVariables.AppsDefaultVersionName)
 }
diff --git a/android/variable.go b/android/variable.go
index 3eee988..2057903 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -110,6 +110,7 @@
 	DateFromFile        *string `json:",omitempty"`
 
 	Platform_sdk_version              *int     `json:",omitempty"`
+	Platform_sdk_codename             *string  `json:",omitempty"`
 	Platform_sdk_final                *bool    `json:",omitempty"`
 	Platform_version_active_codenames []string `json:",omitempty"`
 	Platform_version_future_codenames []string `json:",omitempty"`
diff --git a/java/aar.go b/java/aar.go
index 57c752c..8b5b85e 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -134,7 +134,7 @@
 	// SDK version flags
 	switch sdkVersion {
 	case "", "current", "system_current", "test_current":
-		sdkVersion = proptools.NinjaEscape([]string{ctx.Config().AppsDefaultVersionName()})[0]
+		sdkVersion = proptools.NinjaEscape([]string{ctx.Config().DefaultAppTargetSdk()})[0]
 	}
 
 	linkFlags = append(linkFlags, "--min-sdk-version "+sdkVersion)
diff --git a/java/app_test.go b/java/app_test.go
index 7a61771..6770119 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -16,8 +16,10 @@
 
 import (
 	"android/soong/android"
+	"fmt"
 	"reflect"
 	"sort"
+	"strings"
 	"testing"
 )
 
@@ -240,3 +242,96 @@
 		})
 	}
 }
+
+func TestAppSdkVersion(t *testing.T) {
+	testCases := []struct {
+		name                  string
+		sdkVersion            string
+		platformSdkInt        int
+		platformSdkCodename   string
+		platformSdkFinal      bool
+		expectedMinSdkVersion string
+	}{
+		{
+			name:                  "current final SDK",
+			sdkVersion:            "current",
+			platformSdkInt:        27,
+			platformSdkCodename:   "REL",
+			platformSdkFinal:      true,
+			expectedMinSdkVersion: "27",
+		},
+		{
+			name:                  "current non-final SDK",
+			sdkVersion:            "current",
+			platformSdkInt:        27,
+			platformSdkCodename:   "OMR1",
+			platformSdkFinal:      false,
+			expectedMinSdkVersion: "OMR1",
+		},
+		{
+			name:                  "default final SDK",
+			sdkVersion:            "",
+			platformSdkInt:        27,
+			platformSdkCodename:   "REL",
+			platformSdkFinal:      true,
+			expectedMinSdkVersion: "27",
+		},
+		{
+			name:                  "default non-final SDK",
+			sdkVersion:            "",
+			platformSdkInt:        27,
+			platformSdkCodename:   "OMR1",
+			platformSdkFinal:      false,
+			expectedMinSdkVersion: "OMR1",
+		},
+		{
+			name:                  "14",
+			sdkVersion:            "14",
+			expectedMinSdkVersion: "14",
+		},
+	}
+
+	for _, moduleType := range []string{"android_app", "android_library"} {
+		for _, test := range testCases {
+			t.Run(moduleType+" "+test.name, func(t *testing.T) {
+				bp := fmt.Sprintf(`%s {
+					name: "foo",
+					srcs: ["a.java"],
+					sdk_version: "%s",
+				}`, moduleType, test.sdkVersion)
+
+				config := testConfig(nil)
+				config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
+				config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
+				config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
+
+				ctx := testAppContext(config, bp, nil)
+
+				run(t, ctx, config)
+
+				foo := ctx.ModuleForTests("foo", "android_common")
+				link := foo.Output("package-res.apk")
+				linkFlags := strings.Split(link.Args["flags"], " ")
+				min := android.IndexList("--min-sdk-version", linkFlags)
+				target := android.IndexList("--target-sdk-version", linkFlags)
+
+				if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
+					t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
+				}
+
+				gotMinSdkVersion := linkFlags[min+1]
+				gotTargetSdkVersion := linkFlags[target+1]
+
+				if gotMinSdkVersion != test.expectedMinSdkVersion {
+					t.Errorf("incorrect --min-sdk-version, expected %q got %q",
+						test.expectedMinSdkVersion, gotMinSdkVersion)
+				}
+
+				if gotTargetSdkVersion != test.expectedMinSdkVersion {
+					t.Errorf("incorrect --target-sdk-version, expected %q got %q",
+						test.expectedMinSdkVersion, gotTargetSdkVersion)
+				}
+			})
+		}
+	}
+}