Enforce mainline modules to have latest target sdk version by default.

Due to GMS target SDK requirements (https://docs.partner.android.com/gms/policies/preview/mba#min-target-sdk)
we need to ensure all the  mainline packages to target latest API
level. Currently, the team chases each module to bump their target
API level.

Updating the build code to make sure that mainline modules
(i.e updatable modules) target the latest sdk version by default.
It would be by default set to 10000 before SDK finalization and updated to the new API level after finalization.

Effectively it would mean:
1. '10000' in aosp and internal master
2. Finalized number in development branches like "33" in "tm-dev"
3. As sdk hasn't been finalised in "udc-dev", it would be "10000"
which would be automatically updated to finalized version after sdk finalization.

For local development if the target sdk version is required to be set,
users would need to set Updatable flag to false.
Enforce_default_target_sdk_version flag needs to be used in bp file,
if this feature needs to be tested locally when updatable: false.

Bug: b/227460469
Test: m nothing
Change-Id: I05e0ae74ae44fd73df75e91b926bcc924446253f
diff --git a/java/app.go b/java/app.go
index 7f37ff3..b8edf37 100755
--- a/java/app.go
+++ b/java/app.go
@@ -101,6 +101,15 @@
 	PreventInstall    bool `blueprint:"mutated"`
 	IsCoverageVariant bool `blueprint:"mutated"`
 
+	// It can be set to test the behaviour of default target sdk version.
+	// Only required when updatable: false. It is an error if updatable: true and this is false.
+	Enforce_default_target_sdk_version *bool
+
+	// If set, the targetSdkVersion for the target is set to the latest default API level.
+	// This would be by default false, unless updatable: true or
+	// enforce_default_target_sdk_version: true in which case this defaults to true.
+	EnforceDefaultTargetSdkVersion bool `blueprint:"mutated"`
+
 	// Whether this app is considered mainline updatable or not. When set to true, this will enforce
 	// additional rules to make sure an app can safely be updated. Default is false.
 	// Prefer using other specific properties if build behaviour must be changed; avoid using this
@@ -296,6 +305,18 @@
 		} else {
 			ctx.PropertyErrorf("min_sdk_version", "%s", err.Error())
 		}
+
+		if !BoolDefault(a.appProperties.Enforce_default_target_sdk_version, true) {
+			ctx.PropertyErrorf("enforce_default_target_sdk_version", "Updatable apps must enforce default target sdk version")
+		}
+		// TODO(b/227460469) after all the modules removes the target sdk version, throw an error if the target sdk version is explicitly set.
+		if a.deviceProperties.Target_sdk_version == nil {
+			a.SetEnforceDefaultTargetSdkVersion(true)
+		}
+	}
+
+	if Bool(a.appProperties.Enforce_default_target_sdk_version) {
+		a.SetEnforceDefaultTargetSdkVersion(true)
 	}
 
 	a.checkPlatformAPI(ctx)
@@ -427,7 +448,7 @@
 		a.aapt.defaultManifestVersion = android.DefaultUpdatableModuleVersion
 	}
 	a.aapt.buildActions(ctx, android.SdkContext(a), a.classLoaderContexts,
-		a.usesLibraryProperties.Exclude_uses_libs, aaptLinkFlags...)
+		a.usesLibraryProperties.Exclude_uses_libs, a.enforceDefaultTargetSdkVersion(), aaptLinkFlags...)
 
 	// apps manifests are handled by aapt, don't let Module see them
 	a.properties.Manifest = nil
@@ -866,6 +887,14 @@
 	a.ApexBundleDepsInfo.BuildDepsInfoLists(ctx, a.MinSdkVersion(ctx).String(), depsInfo)
 }
 
+func (a *AndroidApp) enforceDefaultTargetSdkVersion() bool {
+	return a.appProperties.EnforceDefaultTargetSdkVersion
+}
+
+func (a *AndroidApp) SetEnforceDefaultTargetSdkVersion(val bool) {
+	a.appProperties.EnforceDefaultTargetSdkVersion = val
+}
+
 func (a *AndroidApp) Updatable() bool {
 	return Bool(a.appProperties.Updatable)
 }