Add a prod var to override package names.

Currently only java/app.go is affected by package name overrides. When
the var is used, the corresponding module's install APK name is changed
to the overriding name.

Bug: 122957760
Test: app_test.go + TreeHugger
Change-Id: Ie522da3d7280970d740d779cf2694560feae6180
This commit is contained in:
Jaewoong Jung
2019-01-23 16:27:47 -08:00
parent ea5c1f453c
commit 9d22a914bf
5 changed files with 101 additions and 7 deletions

View File

@@ -82,6 +82,9 @@ type AndroidApp struct {
installJniLibs []jniLib
bundleFile android.Path
// the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
installApkName string
}
func (a *AndroidApp) ExportedProguardFlagFiles() android.Paths {
@@ -215,11 +218,11 @@ func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
// framework-res.apk is installed as system/framework/framework-res.apk
installDir = "framework"
} else if Bool(a.appProperties.Privileged) {
installDir = filepath.Join("priv-app", ctx.ModuleName())
installDir = filepath.Join("priv-app", a.installApkName)
} else {
installDir = filepath.Join("app", ctx.ModuleName())
installDir = filepath.Join("app", a.installApkName)
}
a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, ctx.ModuleName()+".apk")
a.dexpreopter.installPath = android.PathForModuleInstall(ctx, installDir, a.installApkName+".apk")
if ctx.ModuleName() != "framework-res" {
a.Module.compile(ctx, a.aaptSrcJar)
@@ -276,6 +279,9 @@ func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx
}
func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
// Check if the install APK name needs to be overridden.
a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(ctx.ModuleName())
// Process all building blocks, from AAPT to certificates.
a.aaptBuildActions(ctx)
@@ -307,9 +313,9 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
// framework-res.apk is installed as system/framework/framework-res.apk
ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".apk", a.outputFile)
} else if Bool(a.appProperties.Privileged) {
ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app", ctx.ModuleName()), ctx.ModuleName()+".apk", a.outputFile)
ctx.InstallFile(android.PathForModuleInstall(ctx, "priv-app", a.installApkName), a.installApkName+".apk", a.outputFile)
} else {
ctx.InstallFile(android.PathForModuleInstall(ctx, "app", ctx.ModuleName()), ctx.ModuleName()+".apk", a.outputFile)
ctx.InstallFile(android.PathForModuleInstall(ctx, "app", a.installApkName), a.installApkName+".apk", a.outputFile)
}
}

View File

@@ -530,3 +530,66 @@ func TestCertificates(t *testing.T) {
})
}
}
func TestPackageNameOverride(t *testing.T) {
testCases := []struct {
name string
bp string
packageNameOverride string
expected []string
}{
{
name: "default",
bp: `
android_app {
name: "foo",
srcs: ["a.java"],
}
`,
packageNameOverride: "",
expected: []string{
buildDir + "/.intermediates/foo/android_common/foo.apk",
buildDir + "/target/product/test_device/system/app/foo/foo.apk",
},
},
{
name: "overridden",
bp: `
android_app {
name: "foo",
srcs: ["a.java"],
}
`,
packageNameOverride: "foo:bar",
expected: []string{
// The package apk should be still be the original name for test dependencies.
buildDir + "/.intermediates/foo/android_common/foo.apk",
buildDir + "/target/product/test_device/system/app/bar/bar.apk",
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
config := testConfig(nil)
if test.packageNameOverride != "" {
config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
}
ctx := testAppContext(config, test.bp, nil)
run(t, ctx, config)
foo := ctx.ModuleForTests("foo", "android_common")
outputs := foo.AllOutputs()
outputMap := make(map[string]bool)
for _, o := range outputs {
outputMap[o] = true
}
for _, e := range test.expected {
if _, exist := outputMap[e]; !exist {
t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
}
}
})
}
}