From d09b0b653b615c322266fa2a41ecf0f0066bc93d Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 18 Apr 2018 11:06:47 -0700 Subject: [PATCH] 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 --- android/config.go | 12 ++++++ android/variable.go | 1 + java/aar.go | 2 +- java/app_test.go | 95 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) diff --git a/android/config.go b/android/config.go index 7122f489e..40ba8c177 100644 --- a/android/config.go +++ b/android/config.go @@ -476,6 +476,10 @@ func (c *config) PlatformSdkVersion() string { 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) DefaultAppTargetSdkInt() int { } } +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 3eee9887e..205790333 100644 --- a/android/variable.go +++ b/android/variable.go @@ -110,6 +110,7 @@ type productVariables struct { 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 57c752c6e..8b5b85e34 100644 --- a/java/aar.go +++ b/java/aar.go @@ -134,7 +134,7 @@ func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkVersion string) (flags [ // 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 7a6177138..6770119ef 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -16,8 +16,10 @@ package java import ( "android/soong/android" + "fmt" "reflect" "sort" + "strings" "testing" ) @@ -240,3 +242,96 @@ func TestEnforceRRO(t *testing.T) { }) } } + +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) + } + }) + } + } +}