Merge "Support building a par file that does not automatically run"
diff --git a/java/config/config.go b/java/config/config.go
index 75be9e2..2602e6b 100644
--- a/java/config/config.go
+++ b/java/config/config.go
@@ -33,7 +33,12 @@
 	DefaultLambdaStubsLibrary     = "core-lambda-stubs"
 	SdkLambdaStubsPath            = "prebuilts/sdk/tools/core-lambda-stubs.jar"
 
-	// A list of the jars that provide information about usages of the hidden API.
+	// A list of the non-boot jars that provide hidden APIs, i.e. libraries.
+	HiddenAPIProvidingNonBootJars = []string{
+		"android.test.base",
+	}
+
+	// A list of the non-boot jars that provide information about usages of the hidden API.
 	HiddenAPIExtraAppUsageJars = []string{
 		// The core-oj-hiddenapi provides information for the core-oj jar.
 		"core-oj-hiddenapi",
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index f199051..01e2c5e 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -59,7 +59,14 @@
 
 	if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
 		isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
-		if isBootJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) {
+		// Check to see if this module provides part of the hiddenapi, i.e. is a boot jar or a white listed
+		// library.
+		isProvidingJar := isBootJar || inList(ctx.ModuleName(), config.HiddenAPIProvidingNonBootJars)
+
+		// If this module provides part of the hiddenapi or is a special module that simply provides information
+		// about the hiddenapi then extract information about the hiddenapi from the UnsupportedAppUsage
+		// annotations compiled into the classes.jar.
+		if isProvidingJar || inList(ctx.ModuleName(), config.HiddenAPIExtraAppUsageJars) {
 			// Derive the greylist from classes jar.
 			flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
 			metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
@@ -67,7 +74,10 @@
 			h.flagsCSVPath = flagsCSV
 			h.metadataCSVPath = metadataCSV
 		}
-		if isBootJar {
+
+		// If this module provides part of the hiddenapi then encode the information about the hiddenapi into
+		// the dex file created for this module.
+		if isProvidingJar {
 			hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", ctx.ModuleName()+".jar")
 			h.bootDexJarPath = dexJar
 			hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go
index 49cc931..02b9b45 100644
--- a/java/prebuilt_apis.go
+++ b/java/prebuilt_apis.go
@@ -103,19 +103,25 @@
 	mctx.CreateModule(android.ModuleFactoryAdaptor(android.FileGroupFactory), &filegroupProps)
 }
 
-func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
+func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string {
 	mydir := mctx.ModuleDir() + "/"
-	// <apiver>/<scope>/<module>.jar
 	var files []string
 	for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
 		for _, scope := range []string{"public", "system", "test", "core"} {
-			vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"*/*.jar", nil)
+			vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
 			if err != nil {
-				mctx.ModuleErrorf("failed to glob jar files under %q: %s", mydir+apiver+"/"+scope, err)
+				mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
 			}
 			files = append(files, vfiles...)
 		}
 	}
+	return files
+}
+
+func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
+	mydir := mctx.ModuleDir() + "/"
+	// <apiver>/<scope>/<module>.jar
+	files := getPrebuiltFiles(mctx, "*.jar")
 
 	for _, f := range files {
 		// create a Import module for each jar file
@@ -128,10 +134,8 @@
 func prebuiltApiFiles(mctx android.TopDownMutatorContext) {
 	mydir := mctx.ModuleDir() + "/"
 	// <apiver>/<scope>/api/<module>.txt
-	files, err := mctx.GlobWithDeps(mydir+"*/*/api/*.txt", nil)
-	if err != nil {
-		mctx.ModuleErrorf("failed to glob api txt files under %q: %s", mydir, err)
-	}
+	files := getPrebuiltFiles(mctx, "api/*.txt")
+
 	if len(files) == 0 {
 		mctx.ModuleErrorf("no api file found under %q", mydir)
 	}
@@ -161,6 +165,7 @@
 			strings.Compare(apiver, info.apiver) > 0) {
 			info.apiver = apiver
 			info.path = localPath
+			m[key] = info
 		}
 	}
 	// create filegroups for the latest version of (<module>, <scope>) pairs