Convert api.xml dist rules to Soong

...from make to support dist'ing *-api.xml files in soong-only builds.

Test: m droid dist --soong-only && ls out/dist/api.xml
Bug: 395162087
Bug: 394365683
Change-Id: I98e92423a107d24b04e6f6f0f96c23c24de01fa5
diff --git a/java/java.go b/java/java.go
index c204476..2db7b1e 100644
--- a/java/java.go
+++ b/java/java.go
@@ -828,6 +828,8 @@
 	combinedExportedProguardFlagsFile android.Path
 
 	InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.InstallPaths)
+
+	apiXmlFile android.WritablePath
 }
 
 var _ android.ApexModule = (*Library)(nil)
@@ -1155,6 +1157,8 @@
 	j.javaLibraryModuleInfoJSON(ctx)
 
 	buildComplianceMetadata(ctx)
+
+	j.createApiXmlFile(ctx)
 }
 
 func (j *Library) javaLibraryModuleInfoJSON(ctx android.ModuleContext) *android.ModuleInfoJSON {
@@ -1259,6 +1263,35 @@
 	}
 }
 
+var apiXMLGeneratingApiSurfaces = []android.SdkKind{
+	android.SdkPublic,
+	android.SdkSystem,
+	android.SdkModule,
+	android.SdkSystemServer,
+	android.SdkTest,
+}
+
+func (j *Library) createApiXmlFile(ctx android.ModuleContext) {
+	if kind, ok := android.JavaLibraryNameToSdkKind(ctx.ModuleName()); ok && android.InList(kind, apiXMLGeneratingApiSurfaces) {
+		scopePrefix := AllApiScopes.matchingScopeFromSdkKind(kind).apiFilePrefix
+		j.apiXmlFile = android.PathForModuleOut(ctx, fmt.Sprintf("%sapi.xml", scopePrefix))
+		ctx.Build(pctx, android.BuildParams{
+			Rule: generateApiXMLRule,
+			// LOCAL_SOONG_CLASSES_JAR
+			Input:  j.implementationAndResourcesJar,
+			Output: j.apiXmlFile,
+		})
+	}
+}
+
+var _ android.ModuleMakeVarsProvider = (*Library)(nil)
+
+func (j *Library) MakeVars(ctx android.MakeVarsModuleContext) {
+	if j.apiXmlFile != nil {
+		ctx.DistForGoal("dist_files", j.apiXmlFile)
+	}
+}
+
 const (
 	aidlIncludeDir   = "aidl"
 	javaDir          = "java"