Merge "Add PLATFORM_SYSTEMSDK_VERSIONS and BOARD_SYSTEMSDK_VERSIONS"
diff --git a/android/config.go b/android/config.go
index 43d743b..2ce7f48 100644
--- a/android/config.go
+++ b/android/config.go
@@ -31,6 +31,7 @@
 
 var Bool = proptools.Bool
 var String = proptools.String
+var FutureApiLevel = 10000
 
 // The configuration file name
 const configFileName = "soong.config"
@@ -453,7 +454,7 @@
 	if Bool(c.ProductVariables.Platform_sdk_final) {
 		return c.PlatformSdkVersionInt()
 	} else {
-		return 10000
+		return FutureApiLevel
 	}
 }
 
@@ -657,6 +658,17 @@
 	return c.config.ProductVariables.ExtraVndkVersions
 }
 
+func (c *deviceConfig) SystemSdkVersions() []string {
+	if c.config.ProductVariables.DeviceSystemSdkVersions == nil {
+		return nil
+	}
+	return *c.config.ProductVariables.DeviceSystemSdkVersions
+}
+
+func (c *deviceConfig) PlatformSystemSdkVersions() []string {
+	return c.config.ProductVariables.Platform_systemsdk_versions
+}
+
 func (c *deviceConfig) OdmPath() string {
 	if c.config.ProductVariables.OdmPath != nil {
 		return *c.config.ProductVariables.OdmPath
diff --git a/android/variable.go b/android/variable.go
index d58ed6a..40fa45e 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -115,13 +115,15 @@
 	Platform_version_active_codenames []string `json:",omitempty"`
 	Platform_version_future_codenames []string `json:",omitempty"`
 	Platform_vndk_version             *string  `json:",omitempty"`
+	Platform_systemsdk_versions       []string `json:",omitempty"`
 
-	DeviceName        *string   `json:",omitempty"`
-	DeviceArch        *string   `json:",omitempty"`
-	DeviceArchVariant *string   `json:",omitempty"`
-	DeviceCpuVariant  *string   `json:",omitempty"`
-	DeviceAbi         *[]string `json:",omitempty"`
-	DeviceVndkVersion *string   `json:",omitempty"`
+	DeviceName              *string   `json:",omitempty"`
+	DeviceArch              *string   `json:",omitempty"`
+	DeviceArchVariant       *string   `json:",omitempty"`
+	DeviceCpuVariant        *string   `json:",omitempty"`
+	DeviceAbi               *[]string `json:",omitempty"`
+	DeviceVndkVersion       *string   `json:",omitempty"`
+	DeviceSystemSdkVersions *[]string `json:",omitempty"`
 
 	DeviceSecondaryArch        *string   `json:",omitempty"`
 	DeviceSecondaryArchVariant *string   `json:",omitempty"`
diff --git a/java/java.go b/java/java.go
index 94ec85e..65e3b41 100644
--- a/java/java.go
+++ b/java/java.go
@@ -308,7 +308,7 @@
 func sdkStringToNumber(ctx android.BaseContext, v string) int {
 	switch v {
 	case "", "current", "system_current", "test_current":
-		return 10000
+		return android.FutureApiLevel
 	default:
 		if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
 			ctx.PropertyErrorf("sdk_version", "invalid sdk version")
@@ -336,6 +336,22 @@
 		return sdkDep{}
 	}
 
+	// Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
+	// or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
+	if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
+		allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
+		if ctx.DeviceSpecific() || ctx.SocSpecific() {
+			if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
+				allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
+			}
+		}
+		version := strings.TrimPrefix(v, "system_")
+		if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
+			ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
+				v, allowed_versions)
+		}
+	}
+
 	toFile := func(v string) sdkDep {
 		dir := filepath.Join("prebuilts/sdk", v)
 		jar := filepath.Join(dir, "android.jar")
@@ -638,7 +654,7 @@
 		flags.javaVersion = "1.7"
 	} else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
 		flags.javaVersion = "1.8"
-	} else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == 10000 {
+	} else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
 		// TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
 		flags.javaVersion = "1.8"
 	} else {
diff --git a/java/java_test.go b/java/java_test.go
index e8298a2..db0b7a7 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -59,7 +59,9 @@
 	if env["ANDROID_JAVA8_HOME"] == "" {
 		env["ANDROID_JAVA8_HOME"] = "jdk8"
 	}
-	return android.TestArchConfig(buildDir, env)
+	config := android.TestArchConfig(buildDir, env)
+	config.ProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
+	return config
 
 }