diff --git a/android/apex.go b/android/apex.go index 91d2c5b65..3c7f4d0e8 100644 --- a/android/apex.go +++ b/android/apex.go @@ -847,7 +847,7 @@ type WalkPayloadDepsFunc func(ctx ModuleContext, do PayloadDepsCallback) // ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks type ModuleWithMinSdkVersionCheck interface { Module - MinSdkVersion(ctx EarlyModuleContext) SdkSpec + MinSdkVersion(ctx EarlyModuleContext) ApiLevel CheckMinSdkVersion(ctx ModuleContext) } diff --git a/android/api_levels.go b/android/api_levels.go index 9440ee9e7..0c0b2b433 100644 --- a/android/api_levels.go +++ b/android/api_levels.go @@ -55,6 +55,9 @@ type ApiLevel struct { } func (this ApiLevel) FinalInt() int { + if this.IsInvalid() { + panic(fmt.Errorf("%v is not a recognized api_level\n", this)) + } if this.IsPreview() { panic("Requested a final int from a non-final ApiLevel") } else { @@ -63,6 +66,9 @@ func (this ApiLevel) FinalInt() int { } func (this ApiLevel) FinalOrFutureInt() int { + if this.IsInvalid() { + panic(fmt.Errorf("%v is not a recognized api_level\n", this)) + } if this.IsPreview() { return FutureApiLevelInt } else { @@ -76,6 +82,9 @@ func (this ApiLevel) FinalOrFutureInt() int { // - preview codenames -> preview base (9000) + index // - otherwise -> cast to int func (this ApiLevel) FinalOrPreviewInt() int { + if this.IsInvalid() { + panic(fmt.Errorf("%v is not a recognized api_level\n", this)) + } if this.IsCurrent() { return this.number } @@ -97,6 +106,11 @@ func (this ApiLevel) IsPreview() bool { return this.isPreview } +// Returns true if the raw api level string is invalid +func (this ApiLevel) IsInvalid() bool { + return this.EqualTo(InvalidApiLevel) +} + // Returns true if this is the unfinalized "current" API level. This means // different things across Java and native. Java APIs do not use explicit // codenames, so all non-final codenames are grouped into "current". For native @@ -113,6 +127,72 @@ func (this ApiLevel) IsNone() bool { return this.number == -1 } +// Returns true if an app is compiling against private apis. +// e.g. if sdk_version = "" in Android.bp, then the ApiLevel of that "sdk" is at PrivateApiLevel. +func (this ApiLevel) IsPrivate() bool { + return this.number == PrivateApiLevel.number +} + +// EffectiveVersion converts an ApiLevel into the concrete ApiLevel that the module should use. For +// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns +// FutureApiLevel(10000). +func (l ApiLevel) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { + if l.EqualTo(InvalidApiLevel) { + return l, fmt.Errorf("invalid version in sdk_version %q", l.value) + } + if !l.IsPreview() { + return l, nil + } + ret := ctx.Config().DefaultAppTargetSdk(ctx) + if ret.IsPreview() { + return FutureApiLevel, nil + } + return ret, nil +} + +// EffectiveVersionString converts an SdkSpec into the concrete version string that the module +// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) +// it returns the codename (P, Q, R, etc.) +func (l ApiLevel) EffectiveVersionString(ctx EarlyModuleContext) (string, error) { + if l.EqualTo(InvalidApiLevel) { + return l.value, fmt.Errorf("invalid version in sdk_version %q", l.value) + } + if !l.IsPreview() { + return l.String(), nil + } + // Determine the default sdk + ret := ctx.Config().DefaultAppTargetSdk(ctx) + if !ret.IsPreview() { + // If the default sdk has been finalized, return that + return ret.String(), nil + } + // There can be more than one active in-development sdks + // If an app is targeting an active sdk, but not the default one, return the requested active sdk. + // e.g. + // SETUP + // In-development: UpsideDownCake, VanillaIceCream + // Default: VanillaIceCream + // Android.bp + // min_sdk_version: `UpsideDownCake` + // RETURN + // UpsideDownCake and not VanillaIceCream + for _, preview := range ctx.Config().PreviewApiLevels() { + if l.String() == preview.String() { + return preview.String(), nil + } + } + // Otherwise return the default one + return ret.String(), nil +} + +// Specified returns true if the module is targeting a recognzized api_level. +// It returns false if either +// 1. min_sdk_version is not an int or a recognized codename +// 2. both min_sdk_version and sdk_version are empty. In this case, MinSdkVersion() defaults to SdkSpecPrivate.ApiLevel +func (this ApiLevel) Specified() bool { + return !this.IsInvalid() && !this.IsPrivate() +} + // Returns -1 if the current API level is less than the argument, 0 if they // are equal, and 1 if it is greater than the argument. func (this ApiLevel) CompareTo(other ApiLevel) int { @@ -166,6 +246,19 @@ var NoneApiLevel = ApiLevel{ isPreview: true, } +// Sentinel ApiLevel to validate that an apiLevel is either an int or a recognized codename. +var InvalidApiLevel = NewInvalidApiLevel("invalid") + +// Returns an apiLevel object at the same level as InvalidApiLevel. +// The object contains the raw string provied in bp file, and can be used for error handling. +func NewInvalidApiLevel(raw string) ApiLevel { + return ApiLevel{ + value: raw, + number: -2, // One less than NoneApiLevel + isPreview: true, + } +} + // The first version that introduced 64-bit ABIs. var FirstLp64Version = uncheckedFinalApiLevel(21) @@ -204,6 +297,16 @@ func ReplaceFinalizedCodenames(config Config, raw string) string { return strconv.Itoa(num) } +// ApiLevelFrom converts the given string `raw` to an ApiLevel. +// If `raw` is invalid (empty string, unrecognized codename etc.) it returns an invalid ApiLevel +func ApiLevelFrom(ctx PathContext, raw string) ApiLevel { + ret, err := ApiLevelFromUser(ctx, raw) + if err != nil { + return NewInvalidApiLevel(raw) + } + return ret +} + // ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error. // // `raw` must be non-empty. Passing an empty string results in a panic. diff --git a/android/config.go b/android/config.go index 292fcf2fe..593812c5f 100644 --- a/android/config.go +++ b/android/config.go @@ -52,6 +52,15 @@ var StringDefault = proptools.StringDefault // FutureApiLevelInt is a placeholder constant for unreleased API levels. const FutureApiLevelInt = 10000 +// PrivateApiLevel represents the api level of SdkSpecPrivate (sdk_version: "") +// This api_level exists to differentiate user-provided "" from "current" sdk_version +// The differentiation is necessary to enable different validation rules for these two possible values. +var PrivateApiLevel = ApiLevel{ + value: "current", // The value is current since aidl expects `current` as the default (TestAidlFlagsWithMinSdkVersion) + number: FutureApiLevelInt + 1, // This is used to differentiate it from FutureApiLevel + isPreview: true, +} + // FutureApiLevel represents unreleased API levels. var FutureApiLevel = ApiLevel{ value: "current", diff --git a/android/sdk.go b/android/sdk.go index 8b23d63a9..63e0bbeec 100644 --- a/android/sdk.go +++ b/android/sdk.go @@ -25,7 +25,7 @@ import ( // minApiLevelForSdkSnapshot provides access to the min_sdk_version for MinApiLevelForSdkSnapshot type minApiLevelForSdkSnapshot interface { - MinSdkVersion(ctx EarlyModuleContext) SdkSpec + MinSdkVersion(ctx EarlyModuleContext) ApiLevel } // MinApiLevelForSdkSnapshot returns the ApiLevel of the min_sdk_version of the supplied module. @@ -34,7 +34,7 @@ type minApiLevelForSdkSnapshot interface { func MinApiLevelForSdkSnapshot(ctx EarlyModuleContext, module Module) ApiLevel { minApiLevel := NoneApiLevel if m, ok := module.(minApiLevelForSdkSnapshot); ok { - minApiLevel = m.MinSdkVersion(ctx).ApiLevel + minApiLevel = m.MinSdkVersion(ctx) } if minApiLevel == NoneApiLevel { // The default min API level is 1. diff --git a/android/sdk_version.go b/android/sdk_version.go index a7e03dcd8..18b819a7b 100644 --- a/android/sdk_version.go +++ b/android/sdk_version.go @@ -25,9 +25,9 @@ type SdkContext interface { SdkVersion(ctx EarlyModuleContext) SdkSpec // SystemModules returns the system_modules property of the current module, or an empty string if it is not set. SystemModules() string - // MinSdkVersion returns SdkSpec that corresponds to the min_sdk_version property of the current module, + // MinSdkVersion returns ApiLevel that corresponds to the min_sdk_version property of the current module, // or from sdk_version if it is not set. - MinSdkVersion(ctx EarlyModuleContext) SdkSpec + MinSdkVersion(ctx EarlyModuleContext) ApiLevel // ReplaceMaxSdkVersionPlaceholder returns SdkSpec to replace the maxSdkVersion property of permission and // uses-permission tags if it is set. ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) SdkSpec @@ -187,14 +187,7 @@ func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { if ctx.DeviceSpecific() || ctx.SocSpecific() { s = s.ForVendorPartition(ctx) } - if !s.ApiLevel.IsPreview() { - return s.ApiLevel, nil - } - ret := ctx.Config().DefaultAppTargetSdk(ctx) - if ret.IsPreview() { - return FutureApiLevel, nil - } - return ret, nil + return s.ApiLevel.EffectiveVersion(ctx) } // EffectiveVersionString converts an SdkSpec into the concrete version string that the module @@ -208,37 +201,12 @@ func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) if ctx.DeviceSpecific() || ctx.SocSpecific() { s = s.ForVendorPartition(ctx) } - if !s.ApiLevel.IsPreview() { - return s.ApiLevel.String(), nil - } - // Determine the default sdk - ret := ctx.Config().DefaultAppTargetSdk(ctx) - if !ret.IsPreview() { - // If the default sdk has been finalized, return that - return ret.String(), nil - } - // There can be more than one active in-development sdks - // If an app is targeting an active sdk, but not the default one, return the requested active sdk. - // e.g. - // SETUP - // In-development: UpsideDownCake, VanillaIceCream - // Default: VanillaIceCream - // Android.bp - // min_sdk_version: `UpsideDownCake` - // RETURN - // UpsideDownCake and not VanillaIceCream - for _, preview := range ctx.Config().PreviewApiLevels() { - if s.ApiLevel.String() == preview.String() { - return preview.String(), nil - } - } - // Otherwise return the default one - return ret.String(), nil + return s.ApiLevel.EffectiveVersionString(ctx) } var ( SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"} - SdkSpecPrivate = SdkSpec{SdkPrivate, FutureApiLevel, ""} + SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""} SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"} ) @@ -261,7 +229,7 @@ func SdkSpecFromWithConfig(config Config, str string) SdkSpec { var kindString string if sep == 0 { - return SdkSpec{SdkInvalid, NoneApiLevel, str} + return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str} } else if sep == -1 { kindString = "" } else { @@ -289,7 +257,7 @@ func SdkSpecFromWithConfig(config Config, str string) SdkSpec { apiLevel, err := ApiLevelFromUserWithConfig(config, versionString) if err != nil { - return SdkSpec{SdkInvalid, apiLevel, str} + return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str} } return SdkSpec{kind, apiLevel, str} } diff --git a/android/sdk_version_test.go b/android/sdk_version_test.go index ec81782f0..ea99c4d62 100644 --- a/android/sdk_version_test.go +++ b/android/sdk_version_test.go @@ -37,11 +37,11 @@ func TestSdkSpecFrom(t *testing.T) { }, { input: "_", - expected: "invalid_(no version)", + expected: "invalid__", }, { input: "_31", - expected: "invalid_(no version)", + expected: "invalid__31", }, { input: "system_R", diff --git a/apex/apex.go b/apex/apex.go index b2ca6c480..9e9d74211 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -2946,12 +2946,8 @@ func (a *apexBundle) minSdkVersionValue(ctx android.EarlyModuleContext) string { } // Returns apex's min_sdk_version SdkSpec, honoring overrides -func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { - return android.SdkSpec{ - Kind: android.SdkNone, - ApiLevel: a.minSdkVersion(ctx), - Raw: a.minSdkVersionValue(ctx), - } +func (a *apexBundle) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { + return a.minSdkVersion(ctx) } // Returns apex's min_sdk_version ApiLevel, honoring overrides diff --git a/apex/builder.go b/apex/builder.go index ee6c473bd..45c52671c 100644 --- a/apex/builder.go +++ b/apex/builder.go @@ -1063,10 +1063,10 @@ func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) { } else { toMinSdkVersion := "(no version)" if m, ok := to.(interface { - MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec + MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel }); ok { - if v := m.MinSdkVersion(ctx); !v.ApiLevel.IsNone() { - toMinSdkVersion = v.ApiLevel.String() + if v := m.MinSdkVersion(ctx); !v.IsNone() { + toMinSdkVersion = v.String() } } else if m, ok := to.(interface{ MinSdkVersion() string }); ok { // TODO(b/175678607) eliminate the use of MinSdkVersion returning @@ -1087,7 +1087,7 @@ func (a *apexBundle) buildApexDependencyInfo(ctx android.ModuleContext) { return !externalDep }) - a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).Raw, depInfos) + a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).String(), depInfos) ctx.Build(pctx, android.BuildParams{ Rule: android.Phony, diff --git a/java/aar.go b/java/aar.go index 4bc5465aa..4e5ac1f36 100644 --- a/java/aar.go +++ b/java/aar.go @@ -220,12 +220,12 @@ func (a *aapt) aapt2Flags(ctx android.ModuleContext, sdkContext android.SdkConte linkDeps = append(linkDeps, assetDeps...) // Returns the effective version for {min|target}_sdk_version - effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.SdkSpec) string { + effectiveVersionString := func(sdkVersion android.SdkSpec, minSdkVersion android.ApiLevel) string { // If {min|target}_sdk_version is current, use sdk_version to determine the effective level // This is necessary for vendor modules. // The effective version does not _only_ depend on {min|target}_sdk_version(level), // but also on the sdk_version (kind+level) - if minSdkVersion.ApiLevel.IsCurrent() { + if minSdkVersion.IsCurrent() { ret, err := sdkVersion.EffectiveVersionString(ctx) if err != nil { ctx.ModuleErrorf("invalid sdk_version: %s", err) @@ -689,7 +689,7 @@ type AARImport struct { jniPackages android.Paths sdkVersion android.SdkSpec - minSdkVersion android.SdkSpec + minSdkVersion android.ApiLevel } var _ android.OutputFileProducer = (*AARImport)(nil) @@ -714,11 +714,11 @@ func (a *AARImport) SystemModules() string { return "" } -func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { +func (a *AARImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { if a.properties.Min_sdk_version != nil { - return android.SdkSpecFrom(ctx, *a.properties.Min_sdk_version) + return android.ApiLevelFrom(ctx, *a.properties.Min_sdk_version) } - return a.SdkVersion(ctx) + return a.SdkVersion(ctx).ApiLevel } func (a *AARImport) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec { diff --git a/java/app.go b/java/app.go index 1a324bfeb..939b95dd7 100755 --- a/java/app.go +++ b/java/app.go @@ -752,7 +752,7 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { type appDepsInterface interface { SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec - MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec + MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel RequiresStableAPIs(ctx android.BaseModuleContext) bool } @@ -865,10 +865,10 @@ func (a *AndroidApp) buildAppDependencyInfo(ctx android.ModuleContext) { } else { toMinSdkVersion := "(no version)" if m, ok := to.(interface { - MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec + MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel }); ok { - if v := m.MinSdkVersion(ctx); !v.ApiLevel.IsNone() { - toMinSdkVersion = v.ApiLevel.String() + if v := m.MinSdkVersion(ctx); !v.IsNone() { + toMinSdkVersion = v.String() } } else if m, ok := to.(interface{ MinSdkVersion() string }); ok { // TODO(b/175678607) eliminate the use of MinSdkVersion returning @@ -1523,9 +1523,9 @@ func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) { manifestValues := &manifestValueAttribute{} // MinSdkVersion(ctx) calls SdkVersion(ctx) if no value for min_sdk_version is set - minSdkSpec := a.MinSdkVersion(ctx) - if !minSdkSpec.ApiLevel.IsPreview() && minSdkSpec.Valid() { - minSdkStr, err := minSdkSpec.EffectiveVersionString(ctx) + minSdkVersion := a.MinSdkVersion(ctx) + if !minSdkVersion.IsPreview() && !minSdkVersion.IsInvalid() { + minSdkStr, err := minSdkVersion.EffectiveVersionString(ctx) if err == nil { manifestValues.MinSdkVersion = &minSdkStr } diff --git a/java/app_import.go b/java/app_import.go index e24e7804e..c1de66745 100644 --- a/java/app_import.go +++ b/java/app_import.go @@ -426,8 +426,8 @@ func (a *AndroidAppImport) SdkVersion(ctx android.EarlyModuleContext) android.Sd return android.SdkSpecPrivate } -func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { - return android.SdkSpecPrivate +func (a *AndroidAppImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { + return android.SdkSpecPrivate.ApiLevel } func (a *AndroidAppImport) LintDepSets() LintDepSets { diff --git a/java/base.go b/java/base.go index 85f4a5e06..75ec16d6f 100644 --- a/java/base.go +++ b/java/base.go @@ -489,7 +489,7 @@ type Module struct { hideApexVariantFromMake bool sdkVersion android.SdkSpec - minSdkVersion android.SdkSpec + minSdkVersion android.ApiLevel maxSdkVersion android.SdkSpec sourceExtensions []string @@ -665,11 +665,11 @@ func (j *Module) SystemModules() string { return proptools.String(j.deviceProperties.System_modules) } -func (j *Module) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { +func (j *Module) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { if j.deviceProperties.Min_sdk_version != nil { - return android.SdkSpecFrom(ctx, *j.deviceProperties.Min_sdk_version) + return android.ApiLevelFrom(ctx, *j.deviceProperties.Min_sdk_version) } - return j.SdkVersion(ctx) + return j.SdkVersion(ctx).ApiLevel } func (j *Module) MaxSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { @@ -685,7 +685,7 @@ func (j *Module) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) } func (j *Module) MinSdkVersionString() string { - return j.minSdkVersion.Raw + return j.minSdkVersion.String() } func (j *Module) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { @@ -881,7 +881,7 @@ func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.Opt j.ignoredAidlPermissionList = android.PathsForModuleSrcExcludes(ctx, exceptions, nil) } - aidlMinSdkVersion := j.MinSdkVersion(ctx).ApiLevel.String() + aidlMinSdkVersion := j.MinSdkVersion(ctx).String() flags = append(flags, "--min_sdk_version="+aidlMinSdkVersion) return strings.Join(flags, " "), deps @@ -1542,9 +1542,9 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { } if ctx.Device() { - lintSDKVersion := func(sdkSpec android.SdkSpec) int { - if v := sdkSpec.ApiLevel; !v.IsPreview() { - return v.FinalInt() + lintSDKVersion := func(apiLevel android.ApiLevel) int { + if !apiLevel.IsPreview() { + return apiLevel.FinalInt() } else { // When running metalava, we pass --version-codename. When that value // is not REL, metalava will add 1 to the --current-version argument. @@ -1575,8 +1575,8 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...) j.linter.classes = j.implementationJarFile j.linter.minSdkVersion = lintSDKVersion(j.MinSdkVersion(ctx)) - j.linter.targetSdkVersion = lintSDKVersion(j.TargetSdkVersion(ctx)) - j.linter.compileSdkVersion = lintSDKVersion(j.SdkVersion(ctx)) + j.linter.targetSdkVersion = lintSDKVersion(j.TargetSdkVersion(ctx).ApiLevel) + j.linter.compileSdkVersion = lintSDKVersion(j.SdkVersion(ctx).ApiLevel) j.linter.compileSdkKind = j.SdkVersion(ctx).Kind j.linter.javaLanguageLevel = flags.javaVersion.String() j.linter.kotlinLanguageLevel = "1.3" @@ -1846,15 +1846,14 @@ func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Modu // Implements android.ApexModule func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error { sdkVersionSpec := j.SdkVersion(ctx) - minSdkVersionSpec := j.MinSdkVersion(ctx) - if !minSdkVersionSpec.Specified() { + minSdkVersion := j.MinSdkVersion(ctx) + if !minSdkVersion.Specified() { return fmt.Errorf("min_sdk_version is not specified") } // If the module is compiling against core (via sdk_version), skip comparison check. if sdkVersionSpec.Kind == android.SdkCore { return nil } - minSdkVersion := minSdkVersionSpec.ApiLevel if minSdkVersion.GreaterThan(sdkVersion) { return fmt.Errorf("newer SDK(%v)", minSdkVersion) } diff --git a/java/classpath_fragment.go b/java/classpath_fragment.go index cf81ddb5d..45e6175f2 100644 --- a/java/classpath_fragment.go +++ b/java/classpath_fragment.go @@ -130,13 +130,13 @@ func configuredJarListToClasspathJars(ctx android.ModuleContext, configuredJars if s, ok := m.(*SdkLibrary); ok { // TODO(208456999): instead of mapping "current" to latest, min_sdk_version should never be set to "current" if s.minSdkVersion.Specified() { - if s.minSdkVersion.ApiLevel.IsCurrent() { + if s.minSdkVersion.IsCurrent() { jar.minSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String() } else { - jar.minSdkVersion = s.minSdkVersion.ApiLevel.String() + jar.minSdkVersion = s.minSdkVersion.String() } } - if s.maxSdkVersion.Specified() { + if s.maxSdkVersion.ApiLevel.Specified() { if s.maxSdkVersion.ApiLevel.IsCurrent() { jar.maxSdkVersion = ctx.Config().DefaultAppTargetSdk(ctx).String() } else { diff --git a/java/dex.go b/java/dex.go index 7b6a99a8a..4d6aa3456 100644 --- a/java/dex.go +++ b/java/dex.go @@ -343,7 +343,7 @@ func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Fl type compileDexParams struct { flags javaBuilderFlags sdkVersion android.SdkSpec - minSdkVersion android.SdkSpec + minSdkVersion android.ApiLevel classesJar android.Path jarName string } diff --git a/java/droiddoc.go b/java/droiddoc.go index e98b9ea29..3e210088b 100644 --- a/java/droiddoc.go +++ b/java/droiddoc.go @@ -248,8 +248,8 @@ func (j *Javadoc) SystemModules() string { return proptools.String(j.properties.System_modules) } -func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { - return j.SdkVersion(ctx) +func (j *Javadoc) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { + return j.SdkVersion(ctx).ApiLevel } func (j *Javadoc) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec { @@ -304,7 +304,7 @@ func (j *Javadoc) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.Op flags = append(flags, "-I"+src.String()) } - minSdkVersion := j.MinSdkVersion(ctx).ApiLevel.FinalOrFutureInt() + minSdkVersion := j.MinSdkVersion(ctx).FinalOrFutureInt() flags = append(flags, fmt.Sprintf("--min_sdk_version=%v", minSdkVersion)) return strings.Join(flags, " "), deps diff --git a/java/hiddenapi.go b/java/hiddenapi.go index c4fc65f2e..d25096b15 100644 --- a/java/hiddenapi.go +++ b/java/hiddenapi.go @@ -73,7 +73,7 @@ type hiddenAPIModule interface { android.Module hiddenAPIIntf - MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec + MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel } type hiddenAPIIntf interface { @@ -157,7 +157,7 @@ func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android // Create a copy of the dex jar which has been encoded with hiddenapi flags. flagsCSV := hiddenAPISingletonPaths(ctx).flags outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath - encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, android.SdkSpecNone, outputDir) + encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, android.NoneApiLevel, outputDir) // Use the encoded dex jar from here onwards. return encodedDex @@ -253,7 +253,7 @@ var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", bluepr // The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with // all the resources from the input jar. It also ensures that if it was uncompressed in the input // it stays uncompressed in the output. -func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, minSdkVersion android.SdkSpec, outputDir android.OutputPath) android.OutputPath { +func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, minSdkVersion android.ApiLevel, outputDir android.OutputPath) android.OutputPath { // The output file has the same name as the input file and is in the output directory. output := outputDir.Join(ctx, dexInput.Base()) @@ -283,7 +283,7 @@ func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Pa // If the library is targeted for Q and/or R then make sure that they do not // have any S+ flags encoded as that will break the runtime. - minApiLevel := minSdkVersion.ApiLevel + minApiLevel := minSdkVersion if !minApiLevel.IsNone() { if minApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(ctx, "R")) { hiddenapiFlags = hiddenapiFlags + " --max-hiddenapi-level=max-target-r" diff --git a/java/hiddenapi_modular.go b/java/hiddenapi_modular.go index be4a48e5b..445ca1e39 100644 --- a/java/hiddenapi_modular.go +++ b/java/hiddenapi_modular.go @@ -1278,7 +1278,7 @@ type bootDexInfo struct { uncompressDex bool // The minimum sdk version that the dex jar will be used on. - minSdkVersion android.SdkSpec + minSdkVersion android.ApiLevel } // bootDexInfoByModule is a map from module name (as returned by module.Name()) to the boot dex diff --git a/java/java.go b/java/java.go index 1168cd9a2..424a6f600 100644 --- a/java/java.go +++ b/java/java.go @@ -811,7 +811,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 { - canonical := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.ApiLevel.String()) + canonical := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String()) p.MinSdkVersion = proptools.StringPtr(canonical) } @@ -1873,7 +1873,7 @@ type Import struct { hideApexVariantFromMake bool sdkVersion android.SdkSpec - minSdkVersion android.SdkSpec + minSdkVersion android.ApiLevel } var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil) @@ -1890,11 +1890,11 @@ func (j *Import) SystemModules() string { return "none" } -func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { +func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { if j.properties.Min_sdk_version != nil { - return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version) + return android.ApiLevelFrom(ctx, *j.properties.Min_sdk_version) } - return j.SdkVersion(ctx) + return j.SdkVersion(ctx).ApiLevel } func (j *Import) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec { @@ -2160,15 +2160,14 @@ func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Modu func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error { sdkVersionSpec := j.SdkVersion(ctx) - minSdkVersionSpec := j.MinSdkVersion(ctx) - if !minSdkVersionSpec.Specified() { + minSdkVersion := j.MinSdkVersion(ctx) + if !minSdkVersion.Specified() { return fmt.Errorf("min_sdk_version is not specified") } // If the module is compiling against core (via sdk_version), skip comparison check. if sdkVersionSpec.Kind == android.SdkCore { return nil } - minSdkVersion := minSdkVersionSpec.ApiLevel if minSdkVersion.GreaterThan(sdkVersion) { return fmt.Errorf("newer SDK(%v)", minSdkVersion) } diff --git a/java/rro.go b/java/rro.go index 9d0667cf0..49737b946 100644 --- a/java/rro.go +++ b/java/rro.go @@ -175,11 +175,11 @@ func (r *RuntimeResourceOverlay) SystemModules() string { return "" } -func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { +func (r *RuntimeResourceOverlay) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { if r.properties.Min_sdk_version != nil { - return android.SdkSpecFrom(ctx, *r.properties.Min_sdk_version) + return android.ApiLevelFrom(ctx, *r.properties.Min_sdk_version) } - return r.SdkVersion(ctx) + return r.SdkVersion(ctx).ApiLevel } func (r *RuntimeResourceOverlay) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec { diff --git a/java/sdk_library.go b/java/sdk_library.go index d2fbfd953..5477ed664 100644 --- a/java/sdk_library.go +++ b/java/sdk_library.go @@ -1231,7 +1231,7 @@ func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) var _ android.ModuleWithMinSdkVersionCheck = (*SdkLibrary)(nil) func (module *SdkLibrary) CheckMinSdkVersion(ctx android.ModuleContext) { - android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx).ApiLevel, func(c android.ModuleContext, do android.PayloadDepsCallback) { + android.CheckMinSdkVersion(ctx, module.MinSdkVersion(ctx), func(c android.ModuleContext, do android.PayloadDepsCallback) { ctx.WalkDeps(func(child android.Module, parent android.Module) bool { isExternal := !module.depIsInSameApex(ctx, child) if am, ok := child.(android.ApexModule); ok { @@ -1775,7 +1775,7 @@ func (module *SdkLibrary) UniqueApexVariations() bool { // Creates the xml file that publicizes the runtime library func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) { - moduleMinApiLevel := module.Library.MinSdkVersion(mctx).ApiLevel + moduleMinApiLevel := module.Library.MinSdkVersion(mctx) var moduleMinApiLevelStr = moduleMinApiLevel.String() if moduleMinApiLevel == android.NoneApiLevel { moduleMinApiLevelStr = "current" @@ -2414,8 +2414,8 @@ func (module *SdkLibraryImport) UniqueApexVariations() bool { } // MinSdkVersion - Implements hiddenAPIModule -func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { - return android.SdkSpecNone +func (module *SdkLibraryImport) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { + return android.NoneApiLevel } var _ hiddenAPIModule = (*SdkLibraryImport)(nil) diff --git a/sdk/bootclasspath_fragment_sdk_test.go b/sdk/bootclasspath_fragment_sdk_test.go index efb97be37..0d6496dd3 100644 --- a/sdk/bootclasspath_fragment_sdk_test.go +++ b/sdk/bootclasspath_fragment_sdk_test.go @@ -839,6 +839,7 @@ func TestSnapshotWithBootclasspathFragment_HiddenAPI(t *testing.T) { compile_dex: true, public: {enabled: true}, permitted_packages: ["mysdklibrary"], + min_sdk_version: "current", } java_sdk_library { diff --git a/sdk/update.go b/sdk/update.go index 0820d62b8..5fb6f2d34 100644 --- a/sdk/update.go +++ b/sdk/update.go @@ -319,7 +319,7 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) { targetBuildRelease := s.targetBuildRelease(ctx) targetApiLevel, err := android.ApiLevelFromUser(ctx, targetBuildRelease.name) if err != nil { - targetApiLevel = android.FutureApiLevel + targetApiLevel = android.InvalidApiLevel } // Aggregate all the sdkMemberVariantDep instances from all the sdk variants. @@ -357,6 +357,12 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) { // If the minApiLevel of the member is greater than the target API level then exclude it from // this snapshot. exclude := memberVariantDep.minApiLevel.GreaterThan(targetApiLevel) + // Always include host variants (e.g. host tools) in the snapshot. + // Host variants should not be guarded by a min_sdk_version check. In fact, host variants + // do not have a `min_sdk_version`. + if memberVariantDep.Host() { + exclude = false + } addMember(name, export, exclude) @@ -1263,6 +1269,11 @@ type sdkMemberVariantDep struct { minApiLevel android.ApiLevel } +// Host returns true if the sdk member is a host variant (e.g. host tool) +func (s *sdkMemberVariantDep) Host() bool { + return s.variant.Target().Os.Class == android.Host +} + var _ android.SdkMember = (*sdkMember)(nil) // sdkMember groups all the variants of a specific member module together along with the name of the