From b830796ca4fe07dc8aa9b41090d795b6c1c970ca Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Mon, 27 Apr 2020 09:42:27 -0700 Subject: [PATCH] soong: Add support for override_runtime_resource_overlay Bug: 154960712 Test: go test ./... -test.v -run TestOverrideAndroidApp Change-Id: I8ca9d173b6ab7f9306c83c2b434107111aa2ca88 --- java/app.go | 59 +++++++++++++++++++++++++++++++++---- java/app_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 5 deletions(-) diff --git a/java/app.go b/java/app.go index 0ec2502ba..4468dc543 100755 --- a/java/app.go +++ b/java/app.go @@ -45,6 +45,7 @@ func RegisterAppBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) + ctx.RegisterModuleType("override_runtime_resource_overlay", OverrideRuntimeResourceOverlayModuleFactory) ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) ctx.RegisterModuleType("runtime_resource_overlay", RuntimeResourceOverlayFactory) @@ -129,6 +130,15 @@ type overridableAppProperties struct { Logging_parent *string } +// runtime_resource_overlay properties that can be overridden by override_runtime_resource_overlay +type OverridableRuntimeResourceOverlayProperties struct { + // the package name of this app. The package name in the manifest file is used if one was not given. + Package_name *string + + // the target package name of this overlay app. The target package name in the manifest file is used if one was not given. + Target_package_name *string +} + type AndroidApp struct { Library aapt @@ -959,6 +969,27 @@ func OverrideAndroidTestModuleFactory() android.Module { return m } +type OverrideRuntimeResourceOverlay struct { + android.ModuleBase + android.OverrideModuleBase +} + +func (i *OverrideRuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleContext) { + // All the overrides happen in the base module. + // TODO(jungjw): Check the base module type. +} + +// override_runtime_resource_overlay is used to create a module based on another +// runtime_resource_overlay module by overriding some of its properties. +func OverrideRuntimeResourceOverlayModuleFactory() android.Module { + m := &OverrideRuntimeResourceOverlay{} + m.AddProperties(&OverridableRuntimeResourceOverlayProperties{}) + + android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibCommon) + android.InitOverrideModule(m) + return m +} + type AndroidAppImport struct { android.ModuleBase android.DefaultableModuleBase @@ -1342,9 +1373,11 @@ func AndroidTestImportFactory() android.Module { type RuntimeResourceOverlay struct { android.ModuleBase android.DefaultableModuleBase + android.OverridableModuleBase aapt - properties RuntimeResourceOverlayProperties + properties RuntimeResourceOverlayProperties + overridableProperties OverridableRuntimeResourceOverlayProperties certificate Certificate @@ -1402,7 +1435,21 @@ func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleC // Compile and link resources r.aapt.hasNoCode = true // Do not remove resources without default values nor dedupe resource configurations with the same value - r.aapt.buildActions(ctx, r, "--no-resource-deduping", "--no-resource-removal") + aaptLinkFlags := []string{"--no-resource-deduping", "--no-resource-removal"} + // Allow the override of "package name" and "overlay target package name" + manifestPackageName, overridden := ctx.DeviceConfig().OverrideManifestPackageNameFor(ctx.ModuleName()) + if overridden || r.overridableProperties.Package_name != nil { + // The product override variable has a priority over the package_name property. + if !overridden { + manifestPackageName = *r.overridableProperties.Package_name + } + aaptLinkFlags = append(aaptLinkFlags, "--rename-manifest-package "+manifestPackageName) + } + if r.overridableProperties.Target_package_name != nil { + aaptLinkFlags = append(aaptLinkFlags, + "--rename-overlay-target-package "+*r.overridableProperties.Target_package_name) + } + r.aapt.buildActions(ctx, r, aaptLinkFlags...) // Sign the built package _, certificates := collectAppDeps(ctx, false) @@ -1441,10 +1488,12 @@ func RuntimeResourceOverlayFactory() android.Module { module := &RuntimeResourceOverlay{} module.AddProperties( &module.properties, - &module.aaptProperties) - - InitJavaModule(module, android.DeviceSupported) + &module.aaptProperties, + &module.overridableProperties) + android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) + android.InitDefaultableModule(module) + android.InitOverridableModule(module, &module.properties.Overrides) return module } diff --git a/java/app_test.go b/java/app_test.go index 39460dce6..5b1e37007 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -2599,3 +2599,78 @@ func TestRuntimeResourceOverlay_JavaDefaults(t *testing.T) { t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath) } } + +func TestOverrideRuntimeResourceOverlay(t *testing.T) { + ctx, _ := testJava(t, ` + runtime_resource_overlay { + name: "foo_overlay", + certificate: "platform", + product_specific: true, + sdk_version: "current", + } + + override_runtime_resource_overlay { + name: "bar_overlay", + base: "foo_overlay", + package_name: "com.android.bar.overlay", + target_package_name: "com.android.bar", + } + `) + + expectedVariants := []struct { + moduleName string + variantName string + apkPath string + overrides []string + targetVariant string + packageFlag string + targetPackageFlag string + }{ + { + variantName: "android_common", + apkPath: "/target/product/test_device/product/overlay/foo_overlay.apk", + overrides: nil, + targetVariant: "android_common", + packageFlag: "", + targetPackageFlag: "", + }, + { + variantName: "android_common_bar_overlay", + apkPath: "/target/product/test_device/product/overlay/bar_overlay.apk", + overrides: []string{"foo_overlay"}, + targetVariant: "android_common_bar", + packageFlag: "com.android.bar.overlay", + targetPackageFlag: "com.android.bar", + }, + } + for _, expected := range expectedVariants { + variant := ctx.ModuleForTests("foo_overlay", expected.variantName) + + // Check the final apk name + outputs := variant.AllOutputs() + expectedApkPath := buildDir + expected.apkPath + found := false + for _, o := range outputs { + if o == expectedApkPath { + found = true + break + } + } + if !found { + t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) + } + + // Check if the overrides field values are correctly aggregated. + mod := variant.Module().(*RuntimeResourceOverlay) + if !reflect.DeepEqual(expected.overrides, mod.properties.Overrides) { + t.Errorf("Incorrect overrides property value, expected: %q, got: %q", + expected.overrides, mod.properties.Overrides) + } + + // Check aapt2 flags. + res := variant.Output("package-res.apk") + aapt2Flags := res.Args["flags"] + checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) + checkAapt2LinkFlag(t, aapt2Flags, "rename-overlay-target-package", expected.targetPackageFlag) + } +}