aapt2 flag for target manifest package override

This change adds an aapt2 flag, rename-instrumentation-target-package,
when an android test's instrumention target module's manifest package
name is overridden.

Bug: 122957760
Test: app_test.go, atest DocumentsUITests
Change-Id: I7116a51ec5ec9d61a20cd28509b3af0e383730d7
diff --git a/java/app.go b/java/app.go
index c08aefd..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
@@ -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)