From ba7e532a117a64710bdd0493b21b671cf94033d8 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Fri, 22 Apr 2022 17:28:25 +0000 Subject: [PATCH 1/2] Enforce newapi check only if min_sdk_version < compile_sdk_version - NewApi check should be enforced only if min_sdk_version is less than the compile_sdk_version (the opposite direction should be a different build-time error) - Change the datatype of *sdkVersion to android.ApiLevel (from string) to support version comparisons Test: go build ./java Test: no changes in ninja file Bug: 228956345 Change-Id: Ic408857db7760d912ef4694d2ed72c0b7106eb04 --- java/base.go | 12 ++++++------ java/lint.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/java/base.go b/java/base.go index b925350a0..a0767183d 100644 --- a/java/base.go +++ b/java/base.go @@ -1481,11 +1481,11 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { } if ctx.Device() { - lintSDKVersionString := func(sdkSpec android.SdkSpec) string { + lintSDKVersion := func(sdkSpec android.SdkSpec) android.ApiLevel { if v := sdkSpec.ApiLevel; !v.IsPreview() { - return v.String() + return v } else { - return ctx.Config().DefaultAppTargetSdk(ctx).String() + return ctx.Config().DefaultAppTargetSdk(ctx) } } @@ -1494,9 +1494,9 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { j.linter.srcJars = srcJars j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...) j.linter.classes = j.implementationJarFile - j.linter.minSdkVersion = lintSDKVersionString(j.MinSdkVersion(ctx)) - j.linter.targetSdkVersion = lintSDKVersionString(j.TargetSdkVersion(ctx)) - j.linter.compileSdkVersion = lintSDKVersionString(j.SdkVersion(ctx)) + j.linter.minSdkVersion = lintSDKVersion(j.MinSdkVersion(ctx)) + j.linter.targetSdkVersion = lintSDKVersion(j.TargetSdkVersion(ctx)) + j.linter.compileSdkVersion = lintSDKVersion(j.SdkVersion(ctx)) j.linter.compileSdkKind = j.SdkVersion(ctx).Kind j.linter.javaLanguageLevel = flags.javaVersion.String() j.linter.kotlinLanguageLevel = "1.3" diff --git a/java/lint.go b/java/lint.go index e97c9c225..426a2af25 100644 --- a/java/lint.go +++ b/java/lint.go @@ -75,9 +75,9 @@ type linter struct { extraLintCheckJars android.Paths test bool library bool - minSdkVersion string - targetSdkVersion string - compileSdkVersion string + minSdkVersion android.ApiLevel + targetSdkVersion android.ApiLevel + compileSdkVersion android.ApiLevel compileSdkKind android.SdkKind javaLanguageLevel string kotlinLanguageLevel string @@ -300,7 +300,7 @@ func (l *linter) generateManifest(ctx android.ModuleContext, rule *android.RuleB Text(`echo "" &&`). Textf(`echo " " &&`, - l.minSdkVersion, l.targetSdkVersion). + l.minSdkVersion.String(), l.targetSdkVersion.String()). Text(`echo ""`). Text(") >").Output(manifestPath) @@ -325,7 +325,7 @@ func (l *linter) lint(ctx android.ModuleContext) { return } - if l.minSdkVersion != l.compileSdkVersion { + if l.minSdkVersion.CompareTo(l.compileSdkVersion) == -1 { l.extraMainlineLintErrors = append(l.extraMainlineLintErrors, updatabilityChecks...) _, filtered := android.FilterList(l.properties.Lint.Warning_checks, updatabilityChecks) if len(filtered) != 0 { @@ -427,7 +427,7 @@ func (l *linter) lint(ctx android.ModuleContext) { FlagWithOutput("--html ", html). FlagWithOutput("--text ", text). FlagWithOutput("--xml ", xml). - FlagWithArg("--compile-sdk-version ", l.compileSdkVersion). + FlagWithArg("--compile-sdk-version ", l.compileSdkVersion.String()). FlagWithArg("--java-language-level ", l.javaLanguageLevel). FlagWithArg("--kotlin-language-level ", l.kotlinLanguageLevel). FlagWithArg("--url ", fmt.Sprintf(".=.,%s=out", android.PathForOutput(ctx).String())). From 397e910835144efce4f11049b9fff6992e7c24ec Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Fri, 22 Apr 2022 17:28:57 +0000 Subject: [PATCH 2/2] Disable newapi check in defaults - Making newapi disabled by default will ensure that this lint check does not run on the platform. This prevents noisy lint warnings like b/228956345#1 - This lint check will continue to be enforced on the transitive deps of apexes, since lint.strict_updatability_linting will be true for those Soong modules Test: TH Test: m out/soong/.intermediates/frameworks/base/services/core/services.core.unboosted/android_common/lint/lint-report.xml // file no longer contains "Call requires API level ..." warning Bug: 228956345 Change-Id: I8ef3137394011fb679a1129f80f6351fb05a4eff --- java/lint_defaults.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/java/lint_defaults.txt b/java/lint_defaults.txt index 4bc0c5fd4..1eee354cd 100644 --- a/java/lint_defaults.txt +++ b/java/lint_defaults.txt @@ -28,6 +28,11 @@ --disable_check SuspiciousImport --disable_check UnusedResources --disable_check ViewConstructor +# Disable NewApi checks for the platform since platform is the one that implements +# the API. This prevents noisy lint warnings like b/228956345#1 +# NewApi checks will continue to be enforced for apex deps since +# lint.strict_updatability_linting will be true for those Soong modules +--disable_check NewApi # Downgrade existing errors to warnings --warning_check AppCompatResource # 55 occurences in 10 modules @@ -66,7 +71,6 @@ --warning_check MissingTvBanner # 3 occurences in 3 modules --warning_check NamespaceTypo # 3 occurences in 3 modules --warning_check NetworkSecurityConfig # 46 occurences in 12 modules ---warning_check NewApi # 1996 occurences in 122 modules --warning_check NotSibling # 15 occurences in 10 modules --warning_check ObjectAnimatorBinding # 14 occurences in 5 modules --warning_check OnClick # 49 occurences in 21 modules