Merge "Fix error in build when OUT_DIR_COMMON_BASE is used"
diff --git a/cc/cc.go b/cc/cc.go
index 8d05038..89d45a9 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -223,7 +223,7 @@
 	isVndkSp() bool
 	isVndkExt() bool
 	inRecovery() bool
-	createVndkSourceAbiDump() bool
+	shouldCreateVndkSourceAbiDump() bool
 	selectedStl() string
 	baseModuleName() string
 	getVndkExtendsModuleName() string
@@ -562,16 +562,29 @@
 	return ctx.mod.inRecovery()
 }
 
-// Create source abi dumps if the module belongs to the list of VndkLibraries.
-func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
-	skipAbiChecks := ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS")
-	isVariantOnProductionDevice := true
-	sanitize := ctx.mod.sanitize
-	if sanitize != nil {
-		isVariantOnProductionDevice = sanitize.isVariantOnProductionDevice()
+// Check whether ABI dumps should be created for this module.
+func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
+	if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
+		return false
 	}
-	vendorAvailable := Bool(ctx.mod.VendorProperties.Vendor_available)
-	return !skipAbiChecks && isVariantOnProductionDevice && ctx.ctx.Device() && ((ctx.useVndk() && ctx.isVndk() && (vendorAvailable || ctx.isVndkExt())) || inList(ctx.baseModuleName(), llndkLibraries))
+	if sanitize := ctx.mod.sanitize; sanitize != nil {
+		if !sanitize.isVariantOnProductionDevice() {
+			return false
+		}
+	}
+	if !ctx.ctx.Device() {
+		// Host modules do not need ABI dumps.
+		return false
+	}
+	if inList(ctx.baseModuleName(), llndkLibraries) {
+		return true
+	}
+	if ctx.useVndk() && ctx.isVndk() {
+		// Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
+		// VNDK-private.
+		return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
+	}
+	return false
 }
 
 func (ctx *moduleContextImpl) selectedStl() string {
diff --git a/cc/library.go b/cc/library.go
index 3bc1001..e92cf9d 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -362,7 +362,7 @@
 		}
 		return Objects{}
 	}
-	if ctx.createVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
+	if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
 		exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
 		var SourceAbiFlags []string
 		for _, dir := range exportIncludeDirs.Strings() {
@@ -632,14 +632,12 @@
 }
 
 func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
-	//Also take into account object re-use.
-	if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
+	if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
 		vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
 		if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
 			vndkVersion = ver
 		}
 
-		refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, vndkVsNdk(ctx), true)
 		exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
 		var SourceAbiFlags []string
 		for _, dir := range exportIncludeDirs.Strings() {
@@ -650,6 +648,8 @@
 		}
 		exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
 		library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
+
+		refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, vndkVsNdk(ctx), true)
 		if refSourceDumpFile.Valid() {
 			unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
 			library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
diff --git a/cmd/pom2bp/pom2bp.go b/cmd/pom2bp/pom2bp.go
index 7b06035..9aa25f0 100644
--- a/cmd/pom2bp/pom2bp.go
+++ b/cmd/pom2bp/pom2bp.go
@@ -281,8 +281,8 @@
 {{if .IsAar}}android_library{{else}}java_library_static{{end}} {
     name: "{{.BpName}}",
     sdk_version: "{{.SdkVersion}}",{{if .IsAar}}
-    min_sdk_version: "{{.MinSdkVersion}}",{{end}}
-    manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
+    min_sdk_version: "{{.MinSdkVersion}}",
+    manifest: "manifests/{{.BpName}}/AndroidManifest.xml",{{end}}
     static_libs: [
         "{{.BpName}}-nodeps",{{range .BpJarDeps}}
         "{{.}}",{{end}}{{range .BpAarDeps}}
diff --git a/java/aar.go b/java/aar.go
index e054d24..a4069bb 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -336,7 +336,8 @@
 type AARImportProperties struct {
 	Aars []string
 
-	Sdk_version *string
+	Sdk_version     *string
+	Min_sdk_version *string
 
 	Static_libs []string
 	Libs        []string
@@ -362,6 +363,9 @@
 }
 
 func (a *AARImport) minSdkVersion() string {
+	if a.properties.Min_sdk_version != nil {
+		return *a.properties.Min_sdk_version
+	}
 	return a.sdkVersion()
 }