Add "exported" mode to xx_aconfig_library build rules

Test: added unit tests and CI

Bug: 309990433
Change-Id: Iae1b85265d9780bde7d41ec2ec6e8e441c2b3814
diff --git a/aconfig/java_aconfig_library.go b/aconfig/java_aconfig_library.go
index f7f8db8..b6c90fc 100644
--- a/aconfig/java_aconfig_library.go
+++ b/aconfig/java_aconfig_library.go
@@ -30,12 +30,21 @@
 
 var declarationsTag = declarationsTagType{}
 
+var aconfigSupportedModes = []string{"production", "test", "exported"}
+
 type JavaAconfigDeclarationsLibraryProperties struct {
 	// name of the aconfig_declarations module to generate a library for
 	Aconfig_declarations string
 
 	// whether to generate test mode version of the library
+	// TODO: remove "Test" property when "Mode" can be used in all the branches
 	Test *bool
+
+	// default mode is "production", the other accepted modes are:
+	// "test": to generate test mode version of the library
+	// "exported": to generate exported mode version of the library
+	// an error will be thrown if the mode is not supported
+	Mode *string
 }
 
 type JavaAconfigDeclarationsLibraryCallbacks struct {
@@ -72,12 +81,19 @@
 
 	// Generate the action to build the srcjar
 	srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
-	var mode string
+
+	if callbacks.properties.Mode != nil && callbacks.properties.Test != nil {
+		ctx.PropertyErrorf("test", "test prop should not be specified when mode prop is set")
+	}
+	mode := proptools.StringDefault(callbacks.properties.Mode, "production")
+	if !isModeSupported(mode) {
+		ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
+	}
+	// TODO: remove "Test" property
 	if proptools.Bool(callbacks.properties.Test) {
 		mode = "test"
-	} else {
-		mode = "production"
 	}
+
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        javaRule,
 		Input:       declarations.IntermediatePath,
@@ -95,9 +111,12 @@
 	return srcJarPath
 }
 
+func isModeSupported(mode string) bool {
+	return android.InList(mode, aconfigSupportedModes)
+}
+
 type bazelJavaAconfigLibraryAttributes struct {
 	Aconfig_declarations bazel.LabelAttribute
-	Test                 *bool
 	Sdk_version          *string
 	Libs                 bazel.LabelListAttribute
 }
@@ -138,7 +157,6 @@
 
 	attrs := bazelJavaAconfigLibraryAttributes{
 		Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label),
-		Test:                 callbacks.properties.Test,
 		Sdk_version:          &sdkVersion,
 		Libs:                 libs,
 	}