Merge changes Ib0389900,Ib2d69dea

* changes:
  Fix hiddenapi issue when REMOVE_ATB_FROM_BCP=true
  Improve hiddenapi processing so it does not require white list
diff --git a/Android.bp b/Android.bp
index 10e6a2e..4d73049 100644
--- a/Android.bp
+++ b/Android.bp
@@ -55,7 +55,6 @@
         "android/namespace.go",
         "android/neverallow.go",
         "android/onceper.go",
-        "android/override_module.go",
         "android/package_ctx.go",
         "android/paths.go",
         "android/prebuilt.go",
@@ -376,6 +375,7 @@
         "soong-android",
         "soong-cc",
         "soong-java",
+        "soong-python",
     ],
     srcs: [
         "apex/apex.go",
diff --git a/android/config.go b/android/config.go
index 33986f7..24be10a 100644
--- a/android/config.go
+++ b/android/config.go
@@ -895,30 +895,16 @@
 }
 
 func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
-	if newManifestName, overridden := c.manifestPackageNameOverrides().Load(name); overridden {
-		return newManifestName.(string), true
-	}
 	return findOverrideValue(c.config.productVariables.ManifestPackageNameOverrides, name,
 		"invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>")
 }
 
 func (c *deviceConfig) OverrideCertificateFor(name string) (certificatePath string, overridden bool) {
-	if newCert, overridden := c.certificateOverrides().Load(name); overridden {
-		return newCert.(string), true
-	}
-	newCert, overridden := findOverrideValue(c.config.productVariables.CertificateOverrides, name,
+	return findOverrideValue(c.config.productVariables.CertificateOverrides, name,
 		"invalid override rule %q in PRODUCT_CERTIFICATE_OVERRIDES should be <module_name>:<certificate_module_name>")
-	if overridden {
-		// PRODUCT_CERTIFICATE_OVERRIDES only supports cert modules.
-		newCert = ":" + newCert
-	}
-	return newCert, overridden
 }
 
 func (c *deviceConfig) OverridePackageNameFor(name string) string {
-	if newName, overridden := c.moduleNameOverrides().Load(name); overridden {
-		return newName.(string)
-	}
 	newName, overridden := findOverrideValue(
 		c.config.productVariables.PackageNameOverrides,
 		name,
diff --git a/android/module.go b/android/module.go
index f2f1af1..2fa7d31 100644
--- a/android/module.go
+++ b/android/module.go
@@ -154,6 +154,7 @@
 	// Deprecated: use WalkDeps instead to support multiple dependency tags on the same module
 	VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
 	WalkDeps(visit func(Module, Module) bool)
+	WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool)
 
 	Variable(pctx PackageContext, name, value string)
 	Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
@@ -1067,6 +1068,10 @@
 		})
 }
 
+func (a *androidModuleContext) WalkDepsBlueprint(visit func(blueprint.Module, blueprint.Module) bool) {
+	a.ModuleContext.WalkDeps(visit)
+}
+
 func (a *androidModuleContext) WalkDeps(visit func(Module, Module) bool) {
 	a.ModuleContext.WalkDeps(func(child, parent blueprint.Module) bool {
 		childAndroidModule := a.validateAndroidModule(child)
diff --git a/android/override_module.go b/android/override_module.go
deleted file mode 100644
index 9fe5f21..0000000
--- a/android/override_module.go
+++ /dev/null
@@ -1,97 +0,0 @@
-package android
-
-import (
-	"github.com/google/blueprint/proptools"
-	"sync"
-)
-
-func init() {
-	RegisterModuleType("override_module", OverrideModuleFactory)
-}
-
-type OverrideModule struct {
-	ModuleBase
-	properties OverrideModuleProperties
-}
-
-type OverrideModuleProperties struct {
-	// base module to override
-	Base *string
-
-	// file path or module name (in the form ":module") of a certificate to override with
-	Certificate *string
-
-	// manifest package name to override with
-	Manifest_package_name *string
-}
-
-// TODO(jungjw): Work with the mainline team to see if we can deprecate all PRODUCT_*_OVERRIDES vars
-// and hand over overriding values directly to base module code.
-func processOverrides(ctx LoadHookContext, p *OverrideModuleProperties) {
-	base := proptools.String(p.Base)
-	if base == "" {
-		ctx.PropertyErrorf("base", "base module name must be provided")
-	}
-
-	config := ctx.DeviceConfig()
-	if other, loaded := config.moduleNameOverrides().LoadOrStore(base, ctx.ModuleName()); loaded {
-		ctx.ModuleErrorf("multiple overriding modules for %q, the other: %q", base, other.(string))
-	}
-
-	if p.Certificate != nil {
-		config.certificateOverrides().Store(base, *p.Certificate)
-	}
-
-	if p.Manifest_package_name != nil {
-		config.manifestPackageNameOverrides().Store(base, *p.Manifest_package_name)
-	}
-}
-
-func (i *OverrideModule) DepsMutator(ctx BottomUpMutatorContext) {
-	base := *i.properties.Base
-	// Right now, we add a dependency only to check the base module exists, and so are not using a tag here.
-	// TODO(jungjw): Add a tag and check the base module type once we finalize supported base module types.
-	ctx.AddDependency(ctx.Module(), nil, base)
-}
-
-func (i *OverrideModule) GenerateAndroidBuildActions(ctx ModuleContext) {
-	// All the overrides happen in the base module.
-	// TODO(jungjw): Check the base module type.
-}
-
-// override_module overrides an existing module with the specified properties.
-//
-// Currently, only android_app is officially supported.
-func OverrideModuleFactory() Module {
-	m := &OverrideModule{}
-	AddLoadHook(m, func(ctx LoadHookContext) {
-		processOverrides(ctx, &m.properties)
-	})
-	m.AddProperties(&m.properties)
-	InitAndroidModule(m)
-	return m
-}
-
-var moduleNameOverridesKey = NewOnceKey("moduleNameOverrides")
-
-func (c *deviceConfig) moduleNameOverrides() *sync.Map {
-	return c.Once(moduleNameOverridesKey, func() interface{} {
-		return &sync.Map{}
-	}).(*sync.Map)
-}
-
-var certificateOverridesKey = NewOnceKey("certificateOverrides")
-
-func (c *deviceConfig) certificateOverrides() *sync.Map {
-	return c.Once(certificateOverridesKey, func() interface{} {
-		return &sync.Map{}
-	}).(*sync.Map)
-}
-
-var manifestPackageNameOverridesKey = NewOnceKey("manifestPackageNameOverrides")
-
-func (c *deviceConfig) manifestPackageNameOverrides() *sync.Map {
-	return c.Once(manifestPackageNameOverridesKey, func() interface{} {
-		return &sync.Map{}
-	}).(*sync.Map)
-}
diff --git a/apex/apex.go b/apex/apex.go
index 5d0c52a..f72eef3 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -25,8 +25,10 @@
 	"android/soong/android"
 	"android/soong/cc"
 	"android/soong/java"
+	"android/soong/python"
 
 	"github.com/google/blueprint"
+	"github.com/google/blueprint/bootstrap"
 	"github.com/google/blueprint/proptools"
 )
 
@@ -289,6 +291,8 @@
 	nativeSharedLib
 	nativeExecutable
 	shBinary
+	pyBinary
+	goBinary
 	javaSharedLib
 )
 
@@ -348,7 +352,7 @@
 		return "ETC"
 	case nativeSharedLib:
 		return "SHARED_LIBRARIES"
-	case nativeExecutable, shBinary:
+	case nativeExecutable, shBinary, pyBinary, goBinary:
 		return "EXECUTABLES"
 	case javaSharedLib:
 		return "JAVA_LIBRARIES"
@@ -534,7 +538,7 @@
 func (a *apexBundle) getCertString(ctx android.BaseContext) string {
 	certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
 	if overridden {
-		return certificate
+		return ":" + certificate
 	}
 	return String(a.properties.Certificate)
 }
@@ -624,6 +628,22 @@
 	return
 }
 
+func getCopyManifestForPyBinary(py *python.Module) (fileToCopy android.Path, dirInApex string) {
+	dirInApex = "bin"
+	fileToCopy = py.HostToolPath().Path()
+	return
+}
+func getCopyManifestForGoBinary(ctx android.ModuleContext, gb bootstrap.GoBinaryTool) (fileToCopy android.Path, dirInApex string) {
+	dirInApex = "bin"
+	s, err := filepath.Rel(android.PathForOutput(ctx).String(), gb.InstallPath())
+	if err != nil {
+		ctx.ModuleErrorf("Unable to use compiled binary at %s", gb.InstallPath())
+		return
+	}
+	fileToCopy = android.PathForOutput(ctx, s)
+	return
+}
+
 func getCopyManifestForShBinary(sh *android.ShBinary) (fileToCopy android.Path, dirInApex string) {
 	dirInApex = filepath.Join("bin", sh.SubDir())
 	fileToCopy = sh.OutputFile()
@@ -658,7 +678,7 @@
 
 	handleSpecialLibs := !android.Bool(a.properties.Ignore_system_library_special_case)
 
-	ctx.WalkDeps(func(child, parent android.Module) bool {
+	ctx.WalkDepsBlueprint(func(child, parent blueprint.Module) bool {
 		if _, ok := parent.(*apexBundle); ok {
 			// direct dependencies
 			depTag := ctx.OtherModuleDependencyTag(child)
@@ -685,8 +705,17 @@
 				} else if sh, ok := child.(*android.ShBinary); ok {
 					fileToCopy, dirInApex := getCopyManifestForShBinary(sh)
 					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, shBinary, sh, nil})
+				} else if py, ok := child.(*python.Module); ok && py.HostToolPath().Valid() {
+					fileToCopy, dirInApex := getCopyManifestForPyBinary(py)
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, pyBinary, py, nil})
+				} else if gb, ok := child.(bootstrap.GoBinaryTool); ok && a.Host() {
+					fileToCopy, dirInApex := getCopyManifestForGoBinary(ctx, gb)
+					// NB: Since go binaries are static we don't need the module for anything here, which is
+					// good since the go tool is a blueprint.Module not an android.Module like we would
+					// normally use.
+					filesInfo = append(filesInfo, apexFile{fileToCopy, depName, dirInApex, goBinary, nil, nil})
 				} else {
-					ctx.PropertyErrorf("binaries", "%q is neithher cc_binary nor sh_binary", depName)
+					ctx.PropertyErrorf("binaries", "%q is neither cc_binary, (embedded) py_binary, (host) blueprint_go_binary, (host) bootstrap_go_binary, nor sh_binary", depName)
 				}
 			case javaLibTag:
 				if java, ok := child.(*java.Library); ok {
diff --git a/java/app.go b/java/app.go
index 08b2d91..2ff6c98 100644
--- a/java/app.go
+++ b/java/app.go
@@ -93,6 +93,8 @@
 
 	// the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
 	installApkName string
+
+	additionalAaptFlags []string
 }
 
 func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
@@ -222,6 +224,8 @@
 		aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName)
 	}
 
+	aaptLinkFlags = append(aaptLinkFlags, a.additionalAaptFlags...)
+
 	a.aapt.buildActions(ctx, sdkContext(a), aaptLinkFlags...)
 
 	// apps manifests are handled by aapt, don't let Module see them
@@ -392,7 +396,7 @@
 func (a *AndroidApp) getCertString(ctx android.BaseContext) string {
 	certificate, overridden := ctx.DeviceConfig().OverrideCertificateFor(ctx.ModuleName())
 	if overridden {
-		return certificate
+		return ":" + certificate
 	}
 	return String(a.appProperties.Certificate)
 }
@@ -441,6 +445,13 @@
 }
 
 func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	// Check if the instrumentation target package is overridden before generating build actions.
+	if a.appTestProperties.Instrumentation_for != nil {
+		manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(*a.appTestProperties.Instrumentation_for)
+		if overridden {
+			a.additionalAaptFlags = append(a.additionalAaptFlags, "--rename-instrumentation-target-package "+manifestPackageName)
+		}
+	}
 	a.generateAndroidBuildActions(ctx)
 
 	a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites)
diff --git a/java/app_test.go b/java/app_test.go
index 313844f..1784fc3 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -748,56 +748,29 @@
 	}
 }
 
-func TestOverrideModule(t *testing.T) {
-	ctx := testJava(t, `
+func TestInstrumentationTargetOverridden(t *testing.T) {
+	bp := `
 		android_app {
 			name: "foo",
 			srcs: ["a.java"],
 		}
 
-		override_module {
+		android_test {
 			name: "bar",
-			base: "foo",
-			certificate: ":new_certificate",
-			manifest_package_name: "org.dandroid.bp",
+			instrumentation_for: "foo",
 		}
+		`
+	config := testConfig(nil)
+	config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
+	ctx := testAppContext(config, bp, nil)
 
-		android_app_certificate {
-			name: "new_certificate",
-	    certificate: "cert/new_cert",
-		}
-		`)
+	run(t, ctx, config)
 
-	// The base module still contains all the final outputs after overrides.
-	foo := ctx.ModuleForTests("foo", "android_common")
-
-	// Check the final apk name
-	outputs := foo.AllOutputs()
-	e := buildDir + "/target/product/test_device/system/app/bar/bar.apk"
-	found := false
-	for _, o := range outputs {
-		if o == e {
-			found = true
-			break
-		}
-	}
-	if !found {
-		t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
-	}
-
-	// Check the certificate paths
-	signapk := foo.Output("foo.apk")
-	signFlags := signapk.Args["certificates"]
-	e = "cert/new_cert.x509.pem cert/new_cert.pk8"
-	if e != signFlags {
-		t.Errorf("Incorrect signing flags, expected: %q, got: %q", e, signFlags)
-	}
-
-	// Check the manifest package name
-	res := foo.Output("package-res.apk")
+	bar := ctx.ModuleForTests("bar", "android_common")
+	res := bar.Output("package-res.apk")
 	aapt2Flags := res.Args["flags"]
-	e = "--rename-manifest-package org.dandroid.bp"
+	e := "--rename-instrumentation-target-package org.dandroid.bp"
 	if !strings.Contains(aapt2Flags, e) {
-		t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
+		t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
 	}
 }
diff --git a/java/java.go b/java/java.go
index 3501174..ecc3608 100644
--- a/java/java.go
+++ b/java/java.go
@@ -168,6 +168,9 @@
 	}
 
 	Instrument bool `blueprint:"mutated"`
+
+	// List of files to include in the META-INF/services folder of the resulting jar.
+	Services []string `android:"arch_variant"`
 }
 
 type CompilerDeviceProperties struct {
@@ -478,6 +481,7 @@
 	android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
 	android.ExtractSourceDeps(ctx, j.properties.Manifest)
 	android.ExtractSourceDeps(ctx, j.properties.Jarjar_rules)
+	android.ExtractSourcesDeps(ctx, j.properties.Services)
 
 	if j.hasSrcExt(".proto") {
 		protoDeps(ctx, &j.protoProperties)
@@ -1136,6 +1140,25 @@
 		manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
 	}
 
+	services := ctx.ExpandSources(j.properties.Services, nil)
+	if len(services) > 0 {
+		servicesJar := android.PathForModuleOut(ctx, "services", jarName)
+		var zipargs []string
+		for _, file := range services {
+			serviceFile := file.String()
+			zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
+		}
+		ctx.Build(pctx, android.BuildParams{
+			Rule:      zip,
+			Output:    servicesJar,
+			Implicits: services,
+			Args: map[string]string{
+				"jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscape(zipargs), " "),
+			},
+		})
+		jars = append(jars, servicesJar)
+	}
+
 	// Combine the classes built from sources, any manifests, and any static libraries into
 	// classes.jar. If there is only one input jar this step will be skipped.
 	var outputFile android.ModuleOutPath
diff --git a/java/java_test.go b/java/java_test.go
index bbcc9ed..8d3efcb 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -81,13 +81,11 @@
 	ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
 	ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
 	ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
-	ctx.RegisterModuleType("override_module", android.ModuleFactoryAdaptor(android.OverrideModuleFactory))
 	ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
 	ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
 	ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
 	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
 	ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
-		ctx.TopDown("load_hooks", android.LoadHookMutator).Parallel()
 		ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
 		ctx.TopDown("java_sdk_library", SdkLibraryMutator).Parallel()
 	})
diff --git a/java/sdk_library.go b/java/sdk_library.go
index f2df49b..df4e08b 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -607,6 +607,10 @@
 	dir := filepath.Join("prebuilts", "sdk", v, api)
 	jar := filepath.Join(dir, module.BaseModuleName()+".jar")
 	jarPath := android.ExistentPathForSource(ctx, jar)
+	if !jarPath.Valid() {
+		ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", v, jar)
+		return nil
+	}
 	return android.Paths{jarPath.Path()}
 }