Merge "Refactor the hiddenAPI() method for reusability"
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 05ad79b..12201a3 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -4495,6 +4495,12 @@
 		}
 	}
 
+	checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
+		hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
+		indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
+		java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
+	}
+
 	t.Run("prebuilt only", func(t *testing.T) {
 		bp := `
 		prebuilt_apex {
@@ -4519,6 +4525,10 @@
 
 		ctx := testDexpreoptWithApexes(t, bp, "", transform)
 		checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+
+		// Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
+		// file.
+		checkHiddenAPIIndexInputs(t, ctx, ``)
 	})
 
 	t.Run("prebuilt with source library preferred", func(t *testing.T) {
@@ -4588,6 +4598,10 @@
 
 		ctx := testDexpreoptWithApexes(t, bp, "", transform)
 		checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+
+		// Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
+		// file.
+		checkHiddenAPIIndexInputs(t, ctx, ``)
 	})
 
 	t.Run("prebuilt with source apex preferred", func(t *testing.T) {
@@ -4632,6 +4646,11 @@
 
 		ctx := testDexpreoptWithApexes(t, bp, "", transform)
 		checkBootDexJarPath(t, ctx, ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
+
+		// Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
+		checkHiddenAPIIndexInputs(t, ctx, `
+.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
+`)
 	})
 
 	t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
@@ -4678,6 +4697,10 @@
 
 		ctx := testDexpreoptWithApexes(t, bp, "", transform)
 		checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
+
+		// Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index
+		// file.
+		checkHiddenAPIIndexInputs(t, ctx, ``)
 	})
 }
 
@@ -6317,7 +6340,7 @@
 	dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
 
 	// Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
-	// product variables that are used by hiddenapi.
+	// product variables.
 	config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
 	config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
 
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index 7e34c83..66d9ca0 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -123,19 +123,21 @@
 	h.annotationsOnly = strings.HasSuffix(name, "-hiddenapi")
 }
 
-// hiddenAPI is called by any module that could contribute to the hiddenapi processing.
+// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi
+// processing.
 //
 // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
 // jar list.
 //
 // Otherwise, it generates ninja rules to do the following:
-// 1. Generates CSV files needed for hiddenapi processing.
+// 1. Extract information needed for hiddenapi processing from the module and output it into CSV
+//    files.
 // 2. Conditionally adds the supplied dex file to the list of files used to generate the
 //    hiddenAPISingletonPathsStruct.stubsFlag file.
 // 3. Conditionally creates a copy of the supplied dex file into which it has encoded the hiddenapi
 //    flags and returns this instead of the supplied dex jar, otherwise simply returns the supplied
 //    dex jar.
-func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
+func (h *hiddenAPI) hiddenAPIExtractAndEncode(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
 	implementationJar android.Path, uncompressDex bool) android.OutputPath {
 
 	if !h.active {
@@ -146,13 +148,7 @@
 	// used as a source of information for hidden API processing otherwise it will result in
 	// duplicate entries in the files.
 	if primary {
-		// Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
-		// in the hiddenAPI structure.
-		h.hiddenAPIGenerateCSV(ctx, implementationJar)
-
-		// Save the unencoded dex jar so it can be used when generating the
-		// hiddenAPISingletonPathsStruct.stubFlags file.
-		h.bootDexJarPath = dexJar
+		h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar)
 	}
 
 	if !h.annotationsOnly {
@@ -168,7 +164,12 @@
 	return dexJar
 }
 
-func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
+// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
+// jar, and outputs it to the appropriate module specific CSV file.
+//
+// It also makes the dex jar available for use when generating the
+// hiddenAPISingletonPathsStruct.stubFlags.
+func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path) {
 	stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
 
 	flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
@@ -207,6 +208,10 @@
 		FlagWithOutput("--output=", indexCSV)
 	rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
 	h.indexCSVPath = indexCSV
+
+	// Save the unencoded dex jar so it can be used when generating the
+	// hiddenAPISingletonPathsStruct.stubFlags file.
+	h.bootDexJarPath = dexJar
 }
 
 var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
diff --git a/java/hiddenapi_singleton_test.go b/java/hiddenapi_singleton_test.go
index fcaa94a..77cfff4 100644
--- a/java/hiddenapi_singleton_test.go
+++ b/java/hiddenapi_singleton_test.go
@@ -76,14 +76,6 @@
 	}
 }
 
-func checkRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
-	actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
-	expected = strings.TrimSpace(expected)
-	if actual != expected {
-		t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
-	}
-}
-
 func TestHiddenAPIIndexSingleton(t *testing.T) {
 	ctx, _ := testHiddenAPIBootJars(t, `
 		java_library {
@@ -108,7 +100,7 @@
 
 	hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
 	indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
-	checkRuleInputs(t, `
+	CheckHiddenAPIRuleInputs(t, `
 .intermediates/bar/android_common/hiddenapi/index.csv
 .intermediates/foo/android_common/hiddenapi/index.csv
 `,
diff --git a/java/java.go b/java/java.go
index cee14cc..dfbcd6f 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1805,7 +1805,7 @@
 		primary = primary && !j.IsReplacedByPrebuilt()
 
 		// Hidden API CSV generation and dex encoding
-		dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile,
+		dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile,
 			proptools.Bool(j.dexProperties.Uncompress_dex))
 
 		// merge dex jar with resources if necessary
@@ -2965,7 +2965,7 @@
 			primary := j.Prebuilt().UsePrebuilt()
 
 			// Hidden API CSV generation and dex encoding
-			dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, outputFile,
+			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, outputFile,
 				proptools.Bool(j.dexProperties.Uncompress_dex))
 
 			j.dexJarFile = dexOutputFile
diff --git a/java/testing.go b/java/testing.go
index 5fcf84c..781106f 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -18,6 +18,7 @@
 	"fmt"
 	"reflect"
 	"sort"
+	"strings"
 	"testing"
 
 	"android/soong/android"
@@ -237,3 +238,11 @@
 		t.Errorf("expected %#q, found %#q", expected, actual)
 	}
 }
+
+func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
+	actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
+	expected = strings.TrimSpace(expected)
+	if actual != expected {
+		t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
+	}
+}