diff --git a/java/androidmk.go b/java/androidmk.go index 19fe7e2fb..b930441f3 100644 --- a/java/androidmk.go +++ b/java/androidmk.go @@ -433,8 +433,10 @@ func (a *AndroidApp) getOverriddenPackages() []string { if len(a.appProperties.Overrides) > 0 { overridden = append(overridden, a.appProperties.Overrides...) } - if a.Name() != a.installApkName { - overridden = append(overridden, a.Name()) + // When APK name is overridden via PRODUCT_PACKAGE_NAME_OVERRIDES + // ensure that the original name is overridden. + if a.Stem() != a.installApkName { + overridden = append(overridden, a.Stem()) } return overridden } diff --git a/java/app.go b/java/app.go index 1c69aeb7a..f57459902 100755 --- a/java/app.go +++ b/java/app.go @@ -621,7 +621,7 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { a.aapt.useEmbeddedDex = Bool(a.appProperties.Use_embedded_dex) // Check if the install APK name needs to be overridden. - a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name()) + a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Stem()) if ctx.ModuleName() == "framework-res" { // framework-res.apk is installed as system/framework/framework-res.apk @@ -1006,6 +1006,7 @@ func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig androi command := rule.Command().BuiltTool("test_config_fixer").Input(testConfig).Output(fixedConfig) fixNeeded := false + // Auto-generated test config uses `ModuleName` as the APK name. So fix it if it is not the case. if ctx.ModuleName() != a.installApkName { fixNeeded = true command.FlagWithArg("--test-file-name ", a.installApkName+".apk") @@ -1162,7 +1163,10 @@ func (i *OverrideAndroidApp) GenerateAndroidBuildActions(_ android.ModuleContext // some of its properties. func OverrideAndroidAppModuleFactory() android.Module { m := &OverrideAndroidApp{} - m.AddProperties(&overridableAppProperties{}) + m.AddProperties( + &OverridableDeviceProperties{}, + &overridableAppProperties{}, + ) android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) android.InitOverrideModule(m) diff --git a/java/app_test.go b/java/app_test.go index 4da7c3dba..d9667b980 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -1707,7 +1707,7 @@ func TestPackageNameOverride(t *testing.T) { }, }, { - name: "overridden", + name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES", bp: ` android_app { name: "foo", @@ -1722,6 +1722,22 @@ func TestPackageNameOverride(t *testing.T) { "out/soong/target/product/test_device/system/app/bar/bar.apk", }, }, + { + name: "overridden via stem", + bp: ` + android_app { + name: "foo", + srcs: ["a.java"], + sdk_version: "current", + stem: "bar", + } + `, + packageNameOverride: "", + expected: []string{ + "out/soong/.intermediates/foo/android_common/bar.apk", + "out/soong/target/product/test_device/system/app/bar/bar.apk", + }, + }, } for _, test := range testCases { @@ -1965,6 +1981,80 @@ func TestOverrideAndroidApp(t *testing.T) { } } +func TestOverrideAndroidAppStem(t *testing.T) { + ctx, _ := testJava(t, ` + android_app { + name: "foo", + srcs: ["a.java"], + sdk_version: "current", + } + override_android_app { + name: "bar", + base: "foo", + } + override_android_app { + name: "baz", + base: "foo", + stem: "baz_stem", + } + android_app { + name: "foo2", + srcs: ["a.java"], + sdk_version: "current", + stem: "foo2_stem", + } + override_android_app { + name: "bar2", + base: "foo2", + } + override_android_app { + name: "baz2", + base: "foo2", + stem: "baz2_stem", + } + `) + for _, expected := range []struct { + moduleName string + variantName string + apkPath string + }{ + { + moduleName: "foo", + variantName: "android_common", + apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk", + }, + { + moduleName: "foo", + variantName: "android_common_bar", + apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk", + }, + { + moduleName: "foo", + variantName: "android_common_baz", + apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk", + }, + { + moduleName: "foo2", + variantName: "android_common", + apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk", + }, + { + moduleName: "foo2", + variantName: "android_common_bar2", + // Note that this may cause the duplicate output error. + apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk", + }, + { + moduleName: "foo2", + variantName: "android_common_baz2", + apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk", + }, + } { + variant := ctx.ModuleForTests(expected.moduleName, expected.variantName) + variant.Output(expected.apkPath) + } +} + func TestOverrideAndroidAppDependency(t *testing.T) { ctx, _ := testJava(t, ` android_app { diff --git a/java/base.go b/java/base.go index 7cd71a270..bc8da9a77 100644 --- a/java/base.go +++ b/java/base.go @@ -253,9 +253,6 @@ type DeviceProperties struct { // otherwise provides defaults libraries to add to the bootclasspath. System_modules *string - // set the name of the output - Stem *string - IsSDKLibrary bool `blueprint:"mutated"` // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file. @@ -267,6 +264,15 @@ type DeviceProperties struct { SyspropPublicStub string `blueprint:"mutated"` } +// Device properties that can be overridden by overriding module (e.g. override_android_app) +type OverridableDeviceProperties struct { + // set the name of the output. If not set, `name` is used. + // To override a module with this property set, overriding module might need to set this as well. + // Otherwise, both the overridden and the overriding modules will have the same output name, which + // can cause the duplicate output error. + Stem *string +} + // Functionality common to Module and Import // // It is embedded in Module so its functionality can be used by methods in Module @@ -389,6 +395,8 @@ type Module struct { protoProperties android.ProtoProperties deviceProperties DeviceProperties + overridableDeviceProperties OverridableDeviceProperties + // jar file containing header classes including static library dependencies, suitable for // inserting into the bootclasspath/classpath of another compile headerJarFile android.Path @@ -544,6 +552,7 @@ func (j *Module) addHostAndDeviceProperties() { j.addHostProperties() j.AddProperties( &j.deviceProperties, + &j.overridableDeviceProperties, &j.dexer.dexProperties, &j.dexpreoptProperties, &j.linter.properties, @@ -1671,7 +1680,7 @@ func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersi } func (j *Module) Stem() string { - return proptools.StringDefault(j.deviceProperties.Stem, j.Name()) + return proptools.StringDefault(j.overridableDeviceProperties.Stem, j.Name()) } func (j *Module) JacocoReportClassesFile() android.Path { diff --git a/java/java.go b/java/java.go index 73fd9c226..bb7c32be8 100644 --- a/java/java.go +++ b/java/java.go @@ -1867,6 +1867,7 @@ func DefaultsFactory() android.Module { module.AddProperties( &CommonProperties{}, &DeviceProperties{}, + &OverridableDeviceProperties{}, &DexProperties{}, &DexpreoptProperties{}, &android.ProtoProperties{},