From b9c58350caa1da5414db4bbfe86a6af72ef67278 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Mon, 13 May 2024 18:29:45 +0000 Subject: [PATCH] Support min_sdk_version overrides in apps This replaces the global override of min_sdk_version (via `APEX_GLOBAL_MIN_SDK_VERSION_OVERRIDE`) with an min_sdk_version override that can be set by each individual soong override_app. The use case for this are go apps which are only installed in T and above, even though the base AOSP apexes might be installable on < T devices. Test: added a unit test Bug: 295311875 Change-Id: Ie2e738a6786bb24417c675617f7c78358017c96c --- java/app.go | 13 +------- java/app_test.go | 79 +++++++++++++++++++-------------------------- java/base.go | 12 +++---- java/java.go | 4 +-- java/sdk_library.go | 1 + 5 files changed, 43 insertions(+), 66 deletions(-) diff --git a/java/app.go b/java/app.go index 377851e39..ea721578a 100644 --- a/java/app.go +++ b/java/app.go @@ -360,23 +360,12 @@ func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { a.generateJavaUsedByApex(ctx) } -func (a *AndroidApp) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { - defaultMinSdkVersion := a.Module.MinSdkVersion(ctx) - if proptools.Bool(a.appProperties.Updatable) { - overrideApiLevel := android.MinSdkVersionFromValue(ctx, ctx.DeviceConfig().ApexGlobalMinSdkVersionOverride()) - if !overrideApiLevel.IsNone() && overrideApiLevel.CompareTo(defaultMinSdkVersion) > 0 { - return overrideApiLevel - } - } - return defaultMinSdkVersion -} - func (a *AndroidApp) checkAppSdkVersions(ctx android.ModuleContext) { if a.Updatable() { if !a.SdkVersion(ctx).Stable() { ctx.PropertyErrorf("sdk_version", "Updatable apps must use stable SDKs, found %v", a.SdkVersion(ctx)) } - if String(a.deviceProperties.Min_sdk_version) == "" { + if String(a.overridableProperties.Min_sdk_version) == "" { ctx.PropertyErrorf("updatable", "updatable apps must set min_sdk_version.") } diff --git a/java/app_test.go b/java/app_test.go index a7c48a1ed..804949435 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -4322,52 +4322,6 @@ func TestPrivappAllowlistAndroidMk(t *testing.T) { ) } -func TestApexGlobalMinSdkVersionOverride(t *testing.T) { - result := android.GroupFixturePreparers( - PrepareForTestWithJavaDefaultModules, - android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { - variables.ApexGlobalMinSdkVersionOverride = proptools.StringPtr("Tiramisu") - }), - ).RunTestWithBp(t, ` - android_app { - name: "com.android.bar", - srcs: ["a.java"], - sdk_version: "current", - } - android_app { - name: "com.android.foo", - srcs: ["a.java"], - sdk_version: "current", - min_sdk_version: "S", - updatable: true, - } - override_android_app { - name: "com.android.go.foo", - base: "com.android.foo", - } - `) - foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer") - fooOverride := result.ModuleForTests("com.android.foo", "android_common_com.android.go.foo").Rule("manifestFixer") - bar := result.ModuleForTests("com.android.bar", "android_common").Rule("manifestFixer") - - android.AssertStringDoesContain(t, - "expected manifest fixer to set com.android.bar minSdkVersion to S", - bar.BuildParams.Args["args"], - "--minSdkVersion S", - ) - android.AssertStringDoesContain(t, - "com.android.foo: expected manifest fixer to set minSdkVersion to T", - foo.BuildParams.Args["args"], - "--minSdkVersion T", - ) - android.AssertStringDoesContain(t, - "com.android.go.foo: expected manifest fixer to set minSdkVersion to T", - fooOverride.BuildParams.Args["args"], - "--minSdkVersion T", - ) - -} - func TestAppFlagsPackages(t *testing.T) { ctx := testApp(t, ` android_app { @@ -4492,3 +4446,36 @@ func TestAppStem(t *testing.T) { t.Errorf("Module output does not contain expected apk %s", "foo-new.apk") } } + +func TestAppMinSdkVersionOverride(t *testing.T) { + result := android.GroupFixturePreparers( + PrepareForTestWithJavaDefaultModules, + ).RunTestWithBp(t, ` + android_app { + name: "com.android.foo", + srcs: ["a.java"], + sdk_version: "current", + min_sdk_version: "31", + updatable: true, + } + override_android_app { + name: "com.android.go.foo", + base: "com.android.foo", + min_sdk_version: "33", + } + `) + foo := result.ModuleForTests("com.android.foo", "android_common").Rule("manifestFixer") + fooOverride := result.ModuleForTests("com.android.foo", "android_common_com.android.go.foo").Rule("manifestFixer") + + android.AssertStringDoesContain(t, + "com.android.foo: expected manifest fixer to set minSdkVersion to T", + foo.BuildParams.Args["args"], + "--minSdkVersion 31", + ) + android.AssertStringDoesContain(t, + "com.android.go.foo: expected manifest fixer to set minSdkVersion to T", + fooOverride.BuildParams.Args["args"], + "--minSdkVersion 33", + ) + +} diff --git a/java/base.go b/java/base.go index 0c2867177..e97d28de2 100644 --- a/java/base.go +++ b/java/base.go @@ -229,10 +229,6 @@ type DeviceProperties struct { // If the SDK kind is empty, it will be set to public. Sdk_version *string - // if not blank, set the minimum version of the sdk that the compiled artifacts will run against. - // Defaults to sdk_version if not set. See sdk_version for possible values. - Min_sdk_version *string - // if not blank, set the maximum version of the sdk that the compiled artifacts will run against. // Defaults to empty string "". See sdk_version for possible values. Max_sdk_version *string @@ -312,6 +308,10 @@ type OverridableProperties struct { // Otherwise, both the overridden and the overriding modules will have the same output name, which // can cause the duplicate output error. Stem *string + + // if not blank, set the minimum version of the sdk that the compiled artifacts will run against. + // Defaults to sdk_version if not set. See sdk_version for possible values. + Min_sdk_version *string } // Functionality common to Module and Import @@ -738,8 +738,8 @@ func (j *Module) SystemModules() string { } func (j *Module) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { - if j.deviceProperties.Min_sdk_version != nil { - return android.ApiLevelFrom(ctx, *j.deviceProperties.Min_sdk_version) + if j.overridableProperties.Min_sdk_version != nil { + return android.ApiLevelFrom(ctx, *j.overridableProperties.Min_sdk_version) } return j.SdkVersion(ctx).ApiLevel } diff --git a/java/java.go b/java/java.go index 05ef5d04f..e3f482450 100644 --- a/java/java.go +++ b/java/java.go @@ -909,7 +909,7 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { // Check min_sdk_version of the transitive dependencies if this module is created from // java_sdk_library. - if j.deviceProperties.Min_sdk_version != nil && j.SdkLibraryName() != nil { + if j.overridableProperties.Min_sdk_version != nil && j.SdkLibraryName() != nil { j.CheckDepsMinSdkVersion(ctx) } @@ -1097,7 +1097,7 @@ func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberCo // If the min_sdk_version was set then add the canonical representation of the API level to the // snapshot. - if j.deviceProperties.Min_sdk_version != nil { + if j.overridableProperties.Min_sdk_version != nil { canonical, err := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String()) if err != nil { ctx.ModuleErrorf("%s", err) diff --git a/java/sdk_library.go b/java/sdk_library.go index 677b32a5e..2d7ea6351 100644 --- a/java/sdk_library.go +++ b/java/sdk_library.go @@ -1842,6 +1842,7 @@ func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) &module.dexProperties, &module.dexpreoptProperties, &module.linter.properties, + &module.overridableProperties, &props, module.sdkComponentPropertiesForChildLibrary(), }