Merge "Reverse order of transitive R.jar classpath entries" into main
diff --git a/aconfig/codegen/java_aconfig_library.go b/aconfig/codegen/java_aconfig_library.go
index 7d7296e..3d15ac9 100644
--- a/aconfig/codegen/java_aconfig_library.go
+++ b/aconfig/codegen/java_aconfig_library.go
@@ -92,12 +92,12 @@
 	if !isModeSupported(mode) {
 		ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
 	}
-	// TODO: uncomment this part after internal clean up
-	//if mode == "exported" && !declarations.Exportable {
-	//	// if mode is exported, the corresponding aconfig_declaration must mark its
-	//	// exportable property true
-	//	ctx.PropertyErrorf("mode", "exported mode requires its aconfig_declaration has exportable prop true")
-	//}
+
+	if mode == "exported" && !declarations.Exportable {
+		// if mode is exported, the corresponding aconfig_declaration must mark its
+		// exportable property true
+		ctx.PropertyErrorf("mode", "exported mode requires its aconfig_declaration has exportable prop true")
+	}
 
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        javaRule,
diff --git a/java/app.go b/java/app.go
index 9736ffd..8209d4c 100755
--- a/java/app.go
+++ b/java/app.go
@@ -1316,6 +1316,12 @@
 	a.extraTestConfigs = android.PathsForModuleSrc(ctx, a.testProperties.Test_options.Extra_test_configs)
 	a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data)
 	android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
+	android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{
+		InstalledFiles:          a.data,
+		OutputFile:              a.OutputFile(),
+		TestConfig:              a.testConfig,
+		HostRequiredModuleNames: a.HostRequiredModuleNames(),
+	})
 }
 
 func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path {
diff --git a/java/droiddoc.go b/java/droiddoc.go
index df40d01..6a66f45 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -222,8 +222,6 @@
 	stubsSrcJar android.WritablePath
 
 	exportableStubsSrcJar android.WritablePath
-
-	runtimeStubsSrcJar android.WritablePath
 }
 
 func (j *Javadoc) OutputFiles(tag string) (android.Paths, error) {
diff --git a/java/droidstubs.go b/java/droidstubs.go
index f4bcaca..76c8d88 100644
--- a/java/droidstubs.go
+++ b/java/droidstubs.go
@@ -227,7 +227,6 @@
 type annotationFlagsParams struct {
 	migratingNullability    bool
 	validatingNullability   bool
-	extractAnnotations      bool
 	nullabilityWarningsFile android.WritablePath
 	annotationsZip          android.WritablePath
 }
@@ -243,19 +242,16 @@
 	stubConfig              stubsCommandConfigParams
 }
 type stubsCommandConfigParams struct {
-	stubsType                   StubsType
-	javaVersion                 javaVersion
-	deps                        deps
-	checkApi                    bool
-	generateStubs               bool
-	doApiLint                   bool
-	doCheckReleased             bool
-	writeSdkValues              bool
-	migratingNullability        bool
-	validatingNullability       bool
-	annotationsEnabled          bool
-	apiLevelsAnnotationsEnabled bool
-	extractAnnotations          bool
+	stubsType             StubsType
+	javaVersion           javaVersion
+	deps                  deps
+	checkApi              bool
+	generateStubs         bool
+	doApiLint             bool
+	doCheckReleased       bool
+	writeSdkValues        bool
+	migratingNullability  bool
+	validatingNullability bool
 }
 
 // droidstubs passes sources files through Metalava to generate stub .java files that only contain the API to be
@@ -525,30 +521,30 @@
 }
 
 func (d *Droidstubs) annotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, params annotationFlagsParams) {
-	cmd.Flag(config.MetalavaAnnotationsFlags)
+	if Bool(d.properties.Annotations_enabled) {
+		cmd.Flag(config.MetalavaAnnotationsFlags)
 
-	if params.migratingNullability {
-		previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
-		cmd.FlagWithInput("--migrate-nullness ", previousApi)
-	}
+		if params.migratingNullability {
+			previousApi := android.PathForModuleSrc(ctx, String(d.properties.Previous_api))
+			cmd.FlagWithInput("--migrate-nullness ", previousApi)
+		}
 
-	if s := String(d.properties.Validate_nullability_from_list); s != "" {
-		cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
-	}
+		if s := String(d.properties.Validate_nullability_from_list); s != "" {
+			cmd.FlagWithInput("--validate-nullability-from-list ", android.PathForModuleSrc(ctx, s))
+		}
 
-	if params.validatingNullability {
-		cmd.FlagWithOutput("--nullability-warnings-txt ", params.nullabilityWarningsFile)
-	}
+		if params.validatingNullability {
+			cmd.FlagWithOutput("--nullability-warnings-txt ", params.nullabilityWarningsFile)
+		}
 
-	if params.extractAnnotations {
 		cmd.FlagWithOutput("--extract-annotations ", params.annotationsZip)
-	}
 
-	if len(d.properties.Merge_annotations_dirs) != 0 {
-		d.mergeAnnoDirFlags(ctx, cmd)
-	}
+		if len(d.properties.Merge_annotations_dirs) != 0 {
+			d.mergeAnnoDirFlags(ctx, cmd)
+		}
 
-	cmd.Flag(config.MetalavaAnnotationsWarningsFlags)
+		cmd.Flag(config.MetalavaAnnotationsWarningsFlags)
+	}
 }
 
 func (d *Droidstubs) mergeAnnoDirFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand) {
@@ -573,11 +569,9 @@
 	})
 }
 
-func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, params stubsCommandParams) {
+func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsType StubsType, apiVersionsXml android.WritablePath) {
 	var apiVersions android.Path
-	stubsType := params.stubConfig.stubsType
-	apiVersionsXml := params.apiVersionsXml
-	if params.stubConfig.apiLevelsAnnotationsEnabled {
+	if proptools.Bool(d.properties.Api_levels_annotations_enabled) {
 		d.apiLevelsGenerationFlags(ctx, cmd, stubsType, apiVersionsXml)
 		apiVersions = apiVersionsXml
 	} else {
@@ -588,9 +582,7 @@
 				} else if stubsType == Exportable {
 					apiVersions = s.exportableArtifacts.apiVersionsXml
 				} else {
-					// if the stubs type does not generate api-versions.xml file, default to using the
-					// everything artifacts
-					apiVersions = s.everythingArtifacts.apiVersionsXml
+					ctx.ModuleErrorf("%s stubs type does not generate api-versions.xml file", stubsType.String())
 				}
 			} else {
 				ctx.PropertyErrorf("api_levels_module",
@@ -824,16 +816,13 @@
 	annotationParams := annotationFlagsParams{
 		migratingNullability:    params.stubConfig.migratingNullability,
 		validatingNullability:   params.stubConfig.validatingNullability,
-		extractAnnotations:      params.stubConfig.extractAnnotations,
 		nullabilityWarningsFile: params.nullabilityWarningsFile,
 		annotationsZip:          params.annotationsZip,
 	}
 
-	if params.stubConfig.annotationsEnabled {
-		d.annotationsFlags(ctx, cmd, annotationParams)
-	}
+	d.annotationsFlags(ctx, cmd, annotationParams)
 	d.inclusionAnnotationsFlags(ctx, cmd)
-	d.apiLevelsAnnotationsFlags(ctx, cmd, params)
+	d.apiLevelsAnnotationsFlags(ctx, cmd, params.stubConfig.stubsType, params.apiVersionsXml)
 
 	d.expandArgs(ctx, cmd)
 
@@ -863,13 +852,13 @@
 		d.everythingArtifacts.metadataZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"-metadata.zip")
 	}
 
-	if params.annotationsEnabled {
+	if Bool(d.properties.Annotations_enabled) {
 		if params.validatingNullability {
 			d.everythingArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_nullability_warnings.txt")
 		}
 		d.everythingArtifacts.annotationsZip = android.PathForModuleOut(ctx, Everything.String(), ctx.ModuleName()+"_annotations.zip")
 	}
-	if params.apiLevelsAnnotationsEnabled {
+	if Bool(d.properties.Api_levels_annotations_enabled) {
 		d.everythingArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, Everything.String(), "api-versions.xml")
 	}
 
@@ -1047,7 +1036,7 @@
 		optionalCmdParams.metadataDir = d.exportableArtifacts.metadataDir
 	}
 
-	if params.annotationsEnabled {
+	if Bool(d.properties.Annotations_enabled) {
 		if params.validatingNullability {
 			d.exportableArtifacts.nullabilityWarningsFile = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_nullability_warnings.txt")
 			optionalCmdParams.nullabilityWarningsFile = d.exportableArtifacts.nullabilityWarningsFile
@@ -1055,7 +1044,7 @@
 		d.exportableArtifacts.annotationsZip = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"_annotations.zip")
 		optionalCmdParams.annotationsZip = d.exportableArtifacts.annotationsZip
 	}
-	if params.apiLevelsAnnotationsEnabled {
+	if Bool(d.properties.Api_levels_annotations_enabled) {
 		d.exportableArtifacts.apiVersionsXml = android.PathForModuleOut(ctx, params.stubsType.String(), "api-versions.xml")
 		optionalCmdParams.apiVersionsXml = d.exportableArtifacts.apiVersionsXml
 	}
@@ -1073,38 +1062,6 @@
 	d.optionalStubCmd(ctx, optionalCmdParams)
 }
 
-// Sandbox rule for generating runtime stubs
-func (d *Droidstubs) runtimeStubCmd(ctx android.ModuleContext, params stubsCommandConfigParams) {
-
-	// We are only interested in generating the stubs srcjar,
-	// not other artifacts for the runtime stubs
-	params.checkApi = false
-	params.writeSdkValues = false
-	params.validatingNullability = false
-	params.extractAnnotations = false
-	params.apiLevelsAnnotationsEnabled = false
-
-	optionalCmdParams := stubsCommandParams{
-		stubConfig: params,
-	}
-
-	d.Javadoc.runtimeStubsSrcJar = android.PathForModuleOut(ctx, params.stubsType.String(), ctx.ModuleName()+"-"+"stubs.srcjar")
-	optionalCmdParams.stubsSrcJar = d.Javadoc.runtimeStubsSrcJar
-
-	// If aconfig_declarations property is not defined, all flagged apis symbols are stripped
-	// as no aconfig flags are enabled. In such case, the runtime stubs are identical to the
-	// exportable stubs, thus no additional metalava invocation is needed.
-	if len(d.properties.Aconfig_declarations) == 0 {
-		rule := android.NewRuleBuilder(pctx, ctx)
-		rule.Command().
-			Text("cp").Flag("-f").
-			Input(d.exportableStubsSrcJar).Output(d.runtimeStubsSrcJar)
-		rule.Build(fmt.Sprintf("metalava_%s", params.stubsType.String()), "metalava merged")
-	} else {
-		d.optionalStubCmd(ctx, optionalCmdParams)
-	}
-}
-
 func (d *Droidstubs) optionalStubCmd(ctx android.ModuleContext, params stubsCommandParams) {
 
 	params.srcJarDir = android.PathForModuleOut(ctx, params.stubConfig.stubsType.String(), "srcjars")
@@ -1176,8 +1133,6 @@
 
 	annotationsEnabled := Bool(d.properties.Annotations_enabled)
 
-	extractAnnotations := annotationsEnabled
-
 	migratingNullability := annotationsEnabled && String(d.properties.Previous_api) != ""
 	validatingNullability := annotationsEnabled && (strings.Contains(String(d.Javadoc.properties.Args), "--validate-nullability-from-merged-stubs") ||
 		String(d.properties.Validate_nullability_from_list) != "")
@@ -1185,40 +1140,27 @@
 	checkApi := apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") ||
 		apiCheckEnabled(ctx, d.properties.Check_api.Last_released, "last_released")
 
-	apiLevelsAnnotationsEnabled := proptools.Bool(d.properties.Api_levels_annotations_enabled)
-
 	stubCmdParams := stubsCommandConfigParams{
-		javaVersion:                 javaVersion,
-		deps:                        deps,
-		checkApi:                    checkApi,
-		generateStubs:               generateStubs,
-		doApiLint:                   doApiLint,
-		doCheckReleased:             doCheckReleased,
-		writeSdkValues:              writeSdkValues,
-		migratingNullability:        migratingNullability,
-		validatingNullability:       validatingNullability,
-		annotationsEnabled:          annotationsEnabled,
-		apiLevelsAnnotationsEnabled: apiLevelsAnnotationsEnabled,
-		extractAnnotations:          extractAnnotations,
+		javaVersion:           javaVersion,
+		deps:                  deps,
+		checkApi:              checkApi,
+		generateStubs:         generateStubs,
+		doApiLint:             doApiLint,
+		doCheckReleased:       doCheckReleased,
+		writeSdkValues:        writeSdkValues,
+		migratingNullability:  migratingNullability,
+		validatingNullability: validatingNullability,
 	}
 	stubCmdParams.stubsType = Everything
 	// Create default (i.e. "everything" stubs) rule for metalava
 	d.everythingStubCmd(ctx, stubCmdParams)
 
-	// The module generates "exportable" stubs regardless of whether
+	// The module generates "exportable" (and "runtime" eventually) stubs regardless of whether
 	// aconfig_declarations property is defined or not. If the property is not defined, the module simply
 	// strips all flagged apis to generate the "exportable" stubs
 	stubCmdParams.stubsType = Exportable
 	d.exportableStubCmd(ctx, stubCmdParams)
 
-	// "runtime" stubs do not generate any other artifacts than the stubs.
-	// Therefore, metalava does not have to run for "runtime" configuration
-	// when the module does not generate stubs.
-	if stubCmdParams.generateStubs {
-		stubCmdParams.stubsType = Runtime
-		d.runtimeStubCmd(ctx, stubCmdParams)
-	}
-
 	if apiCheckEnabled(ctx, d.properties.Check_api.Current, "current") {
 
 		if len(d.Javadoc.properties.Out) > 0 {
diff --git a/java/droidstubs_test.go b/java/droidstubs_test.go
index c86e8bf..8da695f 100644
--- a/java/droidstubs_test.go
+++ b/java/droidstubs_test.go
@@ -397,47 +397,23 @@
 			"bar",
 		],
 	}
-	droidstubs {
-		name: "baz",
-		srcs: ["a/A.java"],
-		api_surface: "public",
-		check_api: {
-			current: {
-				api_file: "a/current.txt",
-				removed_api_file: "a/removed.txt",
-			}
-		},
-	}
 	`)
 
 	// Check that droidstubs depend on aconfig_declarations
 	android.AssertBoolEquals(t, "foo expected to depend on bar",
 		CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "bar"), true)
 
-	fooModule := result.ModuleForTests("foo", "android_common")
+	m := result.ModuleForTests("foo", "android_common")
 	android.AssertStringDoesContain(t, "foo generates revert annotations file",
-		strings.Join(fooModule.AllOutputs(), ""), "revert-annotations-exportable.txt")
+		strings.Join(m.AllOutputs(), ""), "revert-annotations-exportable.txt")
 
 	// revert-annotations.txt passed to exportable stubs generation metalava command
-	exportableManifest := fooModule.Output("metalava_exportable.sbox.textproto")
-	exportableCmdline := String(android.RuleBuilderSboxProtoForTests(t, result.TestContext, exportableManifest).Commands[0].Command)
-	android.AssertStringDoesContain(t, "flagged api hide command not included", exportableCmdline, "revert-annotations-exportable.txt")
+	manifest := m.Output("metalava_exportable.sbox.textproto")
+	cmdline := String(android.RuleBuilderSboxProtoForTests(t, result.TestContext, manifest).Commands[0].Command)
+	android.AssertStringDoesContain(t, "flagged api hide command not included", cmdline, "revert-annotations-exportable.txt")
 
 	android.AssertStringDoesContain(t, "foo generates exportable stubs jar",
-		strings.Join(fooModule.AllOutputs(), ""), "exportable/foo-stubs.srcjar")
-
-	// revert-annotations.txt passed to runtime stubs generation metalava command
-	runtimeManifest := fooModule.Output("metalava_runtime.sbox.textproto")
-	runtimeCmdline := String(android.RuleBuilderSboxProtoForTests(t, result.TestContext, runtimeManifest).Commands[0].Command)
-	android.AssertStringDoesContain(t, "flagged api hide command not included", runtimeCmdline, "revert-annotations-runtime.txt")
-
-	android.AssertStringDoesContain(t, "foo generates runtime stubs jar",
-		strings.Join(fooModule.AllOutputs(), ""), "runtime/foo-stubs.srcjar")
-
-	// If aconfig_declarations property is not defined, the runtime stubs is a copy of the exportable stubs
-	bazModule := result.ModuleForTests("baz", "android_common")
-	bazRuntimeCmdline := bazModule.Rule("metalava_runtime").RuleParams.Command
-	android.AssertStringDoesContain(t, "copy command should include the input stub", bazRuntimeCmdline, "exportable/baz-stubs.srcjar")
+		strings.Join(m.AllOutputs(), ""), "exportable/foo-stubs.srcjar")
 }
 
 func TestReleaseExportRuntimeApis(t *testing.T) {
diff --git a/scripts/test_config_fixer.py b/scripts/test_config_fixer.py
index 07e01a1..2876bcb 100644
--- a/scripts/test_config_fixer.py
+++ b/scripts/test_config_fixer.py
@@ -19,6 +19,7 @@
 from __future__ import print_function
 
 import argparse
+import json
 import sys
 from xml.dom import minidom
 
@@ -31,6 +32,8 @@
 KNOWN_PREPARERS = ['com.android.tradefed.targetprep.TestAppInstallSetup',
                    'com.android.tradefed.targetprep.suite.SuiteApkInstaller']
 
+KNOWN_TEST_RUNNERS = ['com.android.tradefed.testtype.AndroidJUnitTest']
+
 MAINLINE_CONTROLLER = 'com.android.tradefed.testtype.suite.module.MainlineTestModuleController'
 
 def parse_args():
@@ -43,8 +46,12 @@
                       help=('overwrite package fields in the test config'))
   parser.add_argument('--test-file-name', default='', dest='test_file_name',
                       help=('overwrite test file name in the test config'))
+  parser.add_argument('--orig-test-file-name', default='', dest='orig_test_file_name',
+                      help=('Use with test-file-name to only override a single apk'))
   parser.add_argument('--mainline-package-name', default='', dest='mainline_package_name',
                       help=('overwrite mainline module package name in the test config'))
+  parser.add_argument('--test-runner-options', default='', dest='test_runner_options',
+                      help=('Add test runner options in the test config'))
   parser.add_argument('input', help='input test config file')
   parser.add_argument('output', help='output test config file')
   return parser.parse_args()
@@ -76,6 +83,18 @@
         if option.getAttribute('name') == "test-file-name":
           option.setAttribute('value', test_file_name)
 
+def overwrite_single_test_file_name(test_config_doc, orig_test_file_name, new_test_file_name):
+
+  test_config = parse_test_config(test_config_doc)
+  tests = get_children_with_tag(test_config, 'target_preparer')
+
+  for test in tests:
+    if test.getAttribute('class') in KNOWN_PREPARERS:
+      options = get_children_with_tag(test, 'option')
+      for option in options:
+        if option.getAttribute('name') == "test-file-name" and option.getAttribute('value') == orig_test_file_name:
+          option.setAttribute('value', new_test_file_name)
+
 def overwrite_mainline_module_package_name(test_config_doc, mainline_package_name):
 
   test_config = parse_test_config(test_config_doc)
@@ -86,6 +105,31 @@
         if option.getAttribute('name') == "mainline-module-package-name":
           option.setAttribute('value', mainline_package_name)
 
+def add_test_runner_options_toplevel(test_config_doc, test_runner_options):
+
+  test_config = parse_test_config(test_config_doc)
+
+  test_config.appendChild(test_config_doc.createComment("Options from Android.bp"))
+  test_config.appendChild(test_config_doc.createTextNode("\n"))
+  for new_option in json.loads(test_runner_options):
+    option = test_config_doc.createElement("option")
+    # name and value are mandatory,
+    name = new_option.get('Name')
+    if not name:
+      raise RuntimeError('"name" must set in test_runner_option"')
+    value = new_option.get('Value')
+    if not value:
+      raise RuntimeError('"value" must set in test_runner_option"')
+    option.setAttribute('name', name) # 'include-filter')
+    option.setAttribute('value', value) # 'android.test.example.devcodelab.DevCodelabTest#testHelloFail')
+    key = new_option.get('Key')
+    if key:
+      option.setAttribute('key', key) # 'include-filter')
+    # add tab and newline for readability
+    test_config.appendChild(test_config_doc.createTextNode("    "))
+    test_config.appendChild(option)
+    test_config.appendChild(test_config_doc.createTextNode("\n"))
+
 def main():
   """Program entry point."""
   try:
@@ -100,11 +144,20 @@
       overwrite_package_name(doc, manifest_doc, args.package_name)
 
     if args.test_file_name:
-      overwrite_test_file_name(doc, args.test_file_name)
+      if args.orig_test_file_name:
+        overwrite_single_test_file_name(doc, args.orig_test_file_name, args.test_file_name)
+      else:
+        # You probably never want to override the test_file_name if there
+        # are several in the xml, but this is currently only used on generated
+        # AndroidTest.xml where there is only a single test-file-name (no data)
+        overwrite_test_file_name(doc, args.test_file_name)
 
     if args.mainline_package_name:
       overwrite_mainline_module_package_name(doc, args.mainline_package_name)
 
+    if args.test_runner_options:
+      add_test_runner_options_toplevel(doc, args.test_runner_options)
+
     with open(args.output, 'w') as f:
       write_xml(f, doc)
 
diff --git a/tradefed/Android.bp b/tradefed/Android.bp
index f0336a3..e852584 100644
--- a/tradefed/Android.bp
+++ b/tradefed/Android.bp
@@ -13,6 +13,7 @@
         "autogen.go",
         "config.go",
         "makevars.go",
+        "providers.go",
     ],
     pluginFor: ["soong_build"],
 }
diff --git a/tradefed/providers.go b/tradefed/providers.go
new file mode 100644
index 0000000..f41e09e
--- /dev/null
+++ b/tradefed/providers.go
@@ -0,0 +1,21 @@
+package tradefed
+
+import (
+	"android/soong/android"
+
+	"github.com/google/blueprint"
+)
+
+// Output files we need from a base test that we derive from.
+type BaseTestProviderData struct {
+	// data files and apps for android_test
+	InstalledFiles android.Paths
+	// apk for android_test
+	OutputFile android.Path
+	// Either handwritten or generated TF xml.
+	TestConfig android.Path
+	// Other modules we require to be installed to run tests. We expect base to build them.
+	HostRequiredModuleNames []string
+}
+
+var BaseTestProviderKey = blueprint.NewProvider[BaseTestProviderData]()
diff --git a/tradefed_modules/Android.bp b/tradefed_modules/Android.bp
new file mode 100644
index 0000000..9969ae2
--- /dev/null
+++ b/tradefed_modules/Android.bp
@@ -0,0 +1,21 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+bootstrap_go_package {
+    name: "soong-tradefed-modules",
+    pkgPath: "android/soong/tradefed_modules",
+    deps: [
+        "blueprint",
+        "soong-android",
+        "soong-java",
+        "soong-tradefed",
+    ],
+    srcs: [
+        "test_module_config.go",
+    ],
+    testSrcs: [
+        "test_module_config_test.go",
+    ],
+    pluginFor: ["soong_build"],
+}
diff --git a/tradefed_modules/test_module_config.go b/tradefed_modules/test_module_config.go
new file mode 100644
index 0000000..ba6ab94
--- /dev/null
+++ b/tradefed_modules/test_module_config.go
@@ -0,0 +1,219 @@
+package tradefed_modules
+
+import (
+	"android/soong/android"
+	"android/soong/tradefed"
+	"encoding/json"
+	"fmt"
+
+	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
+)
+
+func init() {
+	RegisterTestModuleConfigBuildComponents(android.InitRegistrationContext)
+}
+
+// Register the license_kind module type.
+func RegisterTestModuleConfigBuildComponents(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("test_module_config", TestModuleConfigFactory)
+}
+
+type testModuleConfigModule struct {
+	android.ModuleBase
+	android.DefaultableModuleBase
+	base android.Module
+
+	tradefedProperties
+
+	// Our updated testConfig.
+	testConfig android.OutputPath
+	manifest   android.InstallPath
+	provider   tradefed.BaseTestProviderData
+}
+
+// Properties to list in Android.bp for this module.
+type tradefedProperties struct {
+	// Module name of the base test that we will run.
+	Base *string `android:"path,arch_variant"`
+
+	// Tradefed Options to add to tradefed xml when not one of the include or exclude filter or property.
+	// Sample: [{name: "TestRunnerOptionName", value: "OptionValue" }]
+	Options []tradefed.Option
+
+	// List of tradefed include annotations to add to tradefed xml, like "android.platform.test.annotations.Presubmit".
+	// Tests will be restricted to those matching an include_annotation or include_filter.
+	Include_annotations []string
+
+	// List of tradefed include annotations to add to tradefed xml, like "android.support.test.filters.FlakyTest".
+	// Tests matching an exclude annotation or filter will be skipped.
+	Exclude_annotations []string
+
+	// List of tradefed include filters to add to tradefed xml, like "fully.qualified.class#method".
+	// Tests will be restricted to those matching an include_annotation or include_filter.
+	Include_filters []string
+
+	// List of tradefed exclude filters to add to tradefed xml, like "fully.qualified.class#method".
+	// Tests matching an exclude annotation or filter will be skipped.
+	Exclude_filters []string
+
+	// List of compatibility suites (for example "cts", "vts") that the module should be
+	// installed into.
+	Test_suites []string
+}
+
+type dependencyTag struct {
+	blueprint.BaseDependencyTag
+	name string
+}
+
+var (
+	testModuleConfigTag = dependencyTag{name: "TestModuleConfigBase"}
+	pctx                = android.NewPackageContext("android/soong/tradefed_modules")
+)
+
+func (m *testModuleConfigModule) InstallInTestcases() bool {
+	return true
+}
+
+func (m *testModuleConfigModule) DepsMutator(ctx android.BottomUpMutatorContext) {
+	ctx.AddDependency(ctx.Module(), testModuleConfigTag, *m.Base)
+}
+
+// Takes base's Tradefed Config xml file and generates a new one with the test properties
+// appeneded from this module.
+// Rewrite the name of the apk in "test-file-name" to be our module's name, rather than the original one.
+func (m *testModuleConfigModule) fixTestConfig(ctx android.ModuleContext, baseTestConfig android.Path) android.OutputPath {
+	// Test safe to do when no test_runner_options, but check for that earlier?
+	fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", ctx.ModuleName()+".config")
+	rule := android.NewRuleBuilder(pctx, ctx)
+	command := rule.Command().BuiltTool("test_config_fixer").Input(baseTestConfig).Output(fixedConfig)
+	options := m.composeOptions()
+	if len(options) == 0 {
+		ctx.ModuleErrorf("Test options must be given when using test_module_config. Set include/exclude filter or annotation.")
+	}
+	xmlTestModuleConfigSnippet, _ := json.Marshal(options)
+	escaped := proptools.NinjaAndShellEscape(string(xmlTestModuleConfigSnippet))
+	command.FlagWithArg("--test-file-name=", ctx.ModuleName()+".apk").
+		FlagWithArg("--orig-test-file-name=", *m.tradefedProperties.Base+".apk").
+		FlagWithArg("--test-runner-options=", escaped)
+	rule.Build("fix_test_config", "fix test config")
+	return fixedConfig.OutputPath
+}
+
+// Convert --exclude_filters: ["filter1", "filter2"] ->
+// [ Option{Name: "exclude-filters", Value: "filter1"}, Option{Name: "exclude-filters", Value: "filter2"},
+// ... + include + annotations ]
+func (m *testModuleConfigModule) composeOptions() []tradefed.Option {
+	options := m.Options
+	for _, e := range m.Exclude_filters {
+		options = append(options, tradefed.Option{Name: "exclude-filter", Value: e})
+	}
+	for _, i := range m.Include_filters {
+		options = append(options, tradefed.Option{Name: "include-filter", Value: i})
+	}
+	for _, e := range m.Exclude_annotations {
+		options = append(options, tradefed.Option{Name: "exclude-annotation", Value: e})
+	}
+	for _, i := range m.Include_annotations {
+		options = append(options, tradefed.Option{Name: "include-annotation", Value: i})
+	}
+	return options
+}
+
+// Files to write and where they come from:
+// 1) test_module_config.manifest
+//   - Leave a trail of where we got files from in case other tools need it.
+//
+// 2) $Module.config
+//   - comes from base's module.config (AndroidTest.xml), and then we add our test_options.
+//     provider.TestConfig
+//     [rules via soong_app_prebuilt]
+//
+// 3) $ARCH/$Module.apk
+//   - comes from base
+//     provider.OutputFile
+//     [rules via soong_app_prebuilt]
+//
+// 4) [bases data]
+//   - We copy all of bases data (like helper apks) to our install directory too.
+//     Since we call AndroidMkEntries on base, it will write out LOCAL_COMPATIBILITY_SUPPORT_FILES
+//     with this data and app_prebuilt.mk will generate the rules to copy it from base.
+//     We have no direct rules here to add to ninja.
+//
+// If we change to symlinks, this all needs to change.
+func (m *testModuleConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+
+	ctx.VisitDirectDepsWithTag(testModuleConfigTag, func(dep android.Module) {
+		if provider, ok := android.OtherModuleProvider(ctx, dep, tradefed.BaseTestProviderKey); ok {
+			m.base = dep
+			m.provider = provider
+		} else {
+			ctx.ModuleErrorf("The base module '%s' does not provide test BaseTestProviderData.  Only 'android_test' modules are supported.", dep.Name())
+			return
+		}
+	})
+
+	// 1) A manifest file listing the base.
+	installDir := android.PathForModuleInstall(ctx, ctx.ModuleName())
+	out := android.PathForModuleOut(ctx, "test_module_config.manifest")
+	android.WriteFileRule(ctx, out, fmt.Sprintf("{%q: %q}", "base", *m.tradefedProperties.Base))
+	ctx.InstallFile(installDir, out.Base(), out)
+
+	// 2) Module.config / AndroidTest.xml
+	// Note, there is still a "test-tag" element with base's module name, but
+	// Tradefed team says its ignored anyway.
+	m.testConfig = m.fixTestConfig(ctx, m.provider.TestConfig)
+
+	// 3) Write ARCH/Module.apk in testcases.
+	// Handled by soong_app_prebuilt and OutputFile in entries.
+	// Nothing to do here.
+
+	// 4) Copy base's data files.
+	// Handled by soong_app_prebuilt and LOCAL_COMPATIBILITY_SUPPORT_FILES.
+	// Nothing to do here.
+}
+
+func TestModuleConfigFactory() android.Module {
+	module := &testModuleConfigModule{}
+
+	module.AddProperties(&module.tradefedProperties)
+	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
+	android.InitDefaultableModule(module)
+
+	return module
+}
+
+// Implements android.AndroidMkEntriesProvider
+var _ android.AndroidMkEntriesProvider = (*testModuleConfigModule)(nil)
+
+func (m *testModuleConfigModule) AndroidMkEntries() []android.AndroidMkEntries {
+	// We rely on base writing LOCAL_COMPATIBILITY_SUPPORT_FILES for its data files
+	entriesList := m.base.(android.AndroidMkEntriesProvider).AndroidMkEntries()
+	entries := &entriesList[0]
+	entries.OutputFile = android.OptionalPathForPath(m.provider.OutputFile)
+	entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+		entries.SetString("LOCAL_MODULE", m.Name()) //  out module name, not base's
+
+		// Out update config file with extra options.
+		entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
+		entries.SetString("LOCAL_MODULE_TAGS", "tests")
+		// Required for atest to run additional tradefed testtypes
+		entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", m.provider.HostRequiredModuleNames...)
+
+		// Clear the JNI symbols because they belong to base not us. Either transform the names in the string
+		// or clear the variable because we don't need it, we are copying bases libraries not generating
+		// new ones.
+		entries.SetString("LOCAL_SOONG_JNI_LIBS_SYMBOLS", "")
+
+		// Don't append to base's test-suites, only use the ones we define, so clear it before
+		// appending to it.
+		entries.SetString("LOCAL_COMPATIBILITY_SUITE", "")
+		if len(m.tradefedProperties.Test_suites) > 0 {
+			entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
+		} else {
+			entries.AddCompatibilityTestSuites("null-suite")
+		}
+	})
+	return entriesList
+}
diff --git a/tradefed_modules/test_module_config_test.go b/tradefed_modules/test_module_config_test.go
new file mode 100644
index 0000000..ff53043
--- /dev/null
+++ b/tradefed_modules/test_module_config_test.go
@@ -0,0 +1,204 @@
+// Copyright 2024 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package tradefed_modules
+
+import (
+	"android/soong/android"
+	"android/soong/java"
+	"strings"
+	"testing"
+)
+
+const bp = `
+		android_app {
+			name: "foo",
+			srcs: ["a.java"],
+			sdk_version: "current",
+		}
+
+                android_test_helper_app {
+                        name: "HelperApp",
+                        srcs: ["helper.java"],
+                }
+
+		android_test {
+			name: "base",
+			sdk_version: "current",
+                        data: [":HelperApp", "data/testfile"],
+		}
+
+                test_module_config {
+                        name: "derived_test",
+                        base: "base",
+                        exclude_filters: ["android.test.example.devcodelab.DevCodelabTest#testHelloFail"],
+                        include_annotations: ["android.platform.test.annotations.LargeTest"],
+                }
+
+`
+
+// Ensure we create files needed and set the AndroidMkEntries needed
+func TestModuleConfigAndroidTest(t *testing.T) {
+
+	ctx := android.GroupFixturePreparers(
+		java.PrepareForTestWithJavaDefaultModules,
+		android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
+	).RunTestWithBp(t, bp)
+
+	derived := ctx.ModuleForTests("derived_test", "android_common")
+	// Assert there are rules to create these files.
+	derived.Output("test_module_config.manifest")
+	derived.Output("test_config_fixer/derived_test.config")
+
+	// Ensure some basic rules exist.
+	ctx.ModuleForTests("base", "android_common").Output("package-res.apk")
+	entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
+
+	// Ensure some entries from base are there, specifically support files for data and helper apps.
+	assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
+
+	// And some new derived entries are there.
+	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE_TAGS"], []string{"tests"})
+
+	// And ones we override
+	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
+
+	android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
+}
+
+// Make sure we call test-config-fixer with the right args.
+func TestModuleConfigOptions(t *testing.T) {
+
+	ctx := android.GroupFixturePreparers(
+		java.PrepareForTestWithJavaDefaultModules,
+		android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
+	).RunTestWithBp(t, bp)
+
+	// Check that we generate a rule to make a new AndroidTest.xml/Module.config file.
+	derived := ctx.ModuleForTests("derived_test", "android_common")
+	rule_cmd := derived.Rule("fix_test_config").RuleParams.Command
+	android.AssertStringDoesContain(t, "Bad FixConfig rule inputs", rule_cmd,
+		`--test-file-name=derived_test.apk --orig-test-file-name=base.apk --test-runner-options='[{"Name":"exclude-filter","Key":"","Value":"android.test.example.devcodelab.DevCodelabTest#testHelloFail"},{"Name":"include-annotation","Key":"","Value":"android.platform.test.annotations.LargeTest"}]'`)
+}
+
+// Ensure we error for a base we don't support.
+func TestModuleConfigBadBaseShouldFail(t *testing.T) {
+	badBp := `
+		java_test_host {
+			name: "base",
+                        srcs: ["a.java"],
+		}
+
+                test_module_config {
+                        name: "derived_test",
+                        base: "base",
+                        exclude_filters: ["android.test.example.devcodelab.DevCodelabTest#testHelloFail"],
+                        include_annotations: ["android.platform.test.annotations.LargeTest"],
+                }`
+
+	ctx := android.GroupFixturePreparers(
+		java.PrepareForTestWithJavaDefaultModules,
+		android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
+	).ExtendWithErrorHandler(
+		android.FixtureExpectsAtLeastOneErrorMatchingPattern("does not provide test BaseTestProviderData")).
+		RunTestWithBp(t, badBp)
+
+	ctx.ModuleForTests("derived_test", "android_common")
+}
+
+// Ensure we error for a base we don't support.
+func TestModuleConfigNoFiltersOrAnnotationsShouldFail(t *testing.T) {
+	badBp := `
+		android_test {
+			name: "base",
+			sdk_version: "current",
+                        srcs: ["a.java"],
+		}
+
+                test_module_config {
+                        name: "derived_test",
+                        base: "base",
+                }`
+
+	ctx := android.GroupFixturePreparers(
+		java.PrepareForTestWithJavaDefaultModules,
+		android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
+	).ExtendWithErrorHandler(
+		android.FixtureExpectsAtLeastOneErrorMatchingPattern("Test options must be given")).
+		RunTestWithBp(t, badBp)
+
+	ctx.ModuleForTests("derived_test", "android_common")
+}
+
+func TestModuleConfigMultipleDerivedTestsWriteDistinctMakeEntries(t *testing.T) {
+	multiBp := `
+		android_test {
+			name: "base",
+			sdk_version: "current",
+                        srcs: ["a.java"],
+		}
+
+                test_module_config {
+                        name: "derived_test",
+                        base: "base",
+                        include_annotations: ["android.platform.test.annotations.LargeTest"],
+                }
+
+                test_module_config {
+                        name: "another_derived_test",
+                        base: "base",
+                        include_annotations: ["android.platform.test.annotations.LargeTest"],
+                }`
+
+	ctx := android.GroupFixturePreparers(
+		java.PrepareForTestWithJavaDefaultModules,
+		android.FixtureRegisterWithContext(RegisterTestModuleConfigBuildComponents),
+	).RunTestWithBp(t, multiBp)
+
+	{
+		derived := ctx.ModuleForTests("derived_test", "android_common")
+		entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
+		// All these should be the same in both derived tests
+		assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
+		// Except this one, which points to the updated tradefed xml file.
+		android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
+		// And this one, the module name.
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE"], []string{"derived_test"})
+	}
+
+	{
+		derived := ctx.ModuleForTests("another_derived_test", "android_common")
+		entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
+		// All these should be the same in both derived tests
+		assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
+		// Except this one, which points to the updated tradefed xml file.
+		android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "another_derived_test/android_common/test_config_fixer/another_derived_test.config")
+		// And this one, the module name.
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE"], []string{"another_derived_test"})
+	}
+}
+
+// Use for situations where the entries map contains pairs:  [srcPath:installedPath1, srcPath2:installedPath2]
+// and we want to compare the RHS of the pairs, i.e. installedPath1, installedPath2
+func assertEntryPairValues(t *testing.T, actual []string, expected []string) {
+	for i, e := range actual {
+		parts := strings.Split(e, ":")
+		if len(parts) != 2 {
+			t.Errorf("Expected entry to have a value delimited by :, received: %s", e)
+			return
+		}
+		android.AssertStringEquals(t, "", parts[1], expected[i])
+	}
+}