Support new property "arch_dpi" for android_import

Support the feature with new property combined with architecture and dpi
resolution.

Bug: 319381869
Test: go test -run TestAndroidAppImport_ArchVariants
Change-Id: Ib43cbae8868fc35701e6666867b2325f5abbb2c7
This commit is contained in:
Herbert Xue 2024-01-29 13:57:51 +08:00
parent 3ce80aef7f
commit 04354ae88b
2 changed files with 100 additions and 8 deletions

View file

@ -70,9 +70,10 @@ type AndroidAppImport struct {
android.ApexModuleBase android.ApexModuleBase
prebuilt android.Prebuilt prebuilt android.Prebuilt
properties AndroidAppImportProperties properties AndroidAppImportProperties
dpiVariants interface{} dpiVariants interface{}
archVariants interface{} archVariants interface{}
arch_dpiVariants interface{}
outputFile android.Path outputFile android.Path
certificate Certificate certificate Certificate
@ -155,8 +156,8 @@ func (a *AndroidAppImport) IsInstallable() bool {
// soong config variables are applied. // soong config variables are applied.
func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) { func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
config := ctx.Config() config := ctx.Config()
dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName(DpiGroupName)
dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
// Try DPI variant matches in the reverse-priority order so that the highest priority match // Try DPI variant matches in the reverse-priority order so that the highest priority match
// overwrites everything else. // overwrites everything else.
// TODO(jungjw): Can we optimize this by making it priority order? // TODO(jungjw): Can we optimize this by making it priority order?
@ -166,11 +167,27 @@ func (a *AndroidAppImport) processVariants(ctx android.DefaultableHookContext) {
if config.ProductAAPTPreferredConfig() != "" { if config.ProductAAPTPreferredConfig() != "" {
MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig()) MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
} }
archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName(ArchGroupName)
archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType archType := ctx.Config().AndroidFirstDeviceTarget.Arch.ArchType
MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name) MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
// Process "arch" includes "dpi_variants"
archStructPtr := reflect.ValueOf(a.arch_dpiVariants).Elem().FieldByName(ArchGroupName)
if archStruct := archStructPtr.Elem(); archStruct.IsValid() {
archPartPropsPtr := archStruct.FieldByName(proptools.FieldNameForProperty(archType.Name))
if archPartProps := archPartPropsPtr.Elem(); archPartProps.IsValid() {
archDpiPropsPtr := archPartProps.FieldByName(DpiGroupName)
if archDpiProps := archDpiPropsPtr.Elem(); archDpiProps.IsValid() {
for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPrebuiltDPI()[i])
}
if config.ProductAAPTPreferredConfig() != "" {
MergePropertiesFromVariant(ctx, &a.properties, archDpiProps, config.ProductAAPTPreferredConfig())
}
}
}
}
if String(a.properties.Apk) == "" { if String(a.properties.Apk) == "" {
// Disable this module since the apk property is still empty after processing all matching // Disable this module since the apk property is still empty after processing all matching
// variants. This likely means there is no matching variant, and the default variant doesn't // variants. This likely means there is no matching variant, and the default variant doesn't
@ -448,18 +465,25 @@ func (a *AndroidAppImport) PrivAppAllowlist() android.OptionalPath {
return android.OptionalPath{} return android.OptionalPath{}
} }
const (
ArchGroupName = "Arch"
DpiGroupName = "Dpi_variants"
)
var dpiVariantGroupType reflect.Type var dpiVariantGroupType reflect.Type
var archVariantGroupType reflect.Type var archVariantGroupType reflect.Type
var archdpiVariantGroupType reflect.Type
var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"} var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
func initAndroidAppImportVariantGroupTypes() { func initAndroidAppImportVariantGroupTypes() {
dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants") dpiVariantGroupType = createVariantGroupType(supportedDpis, DpiGroupName)
archNames := make([]string, len(android.ArchTypeList())) archNames := make([]string, len(android.ArchTypeList()))
for i, archType := range android.ArchTypeList() { for i, archType := range android.ArchTypeList() {
archNames[i] = archType.Name archNames[i] = archType.Name
} }
archVariantGroupType = createVariantGroupType(archNames, "Arch") archVariantGroupType = createVariantGroupType(archNames, ArchGroupName)
archdpiVariantGroupType = createArchDpiVariantGroupType(archNames, supportedDpis)
} }
// Populates all variant struct properties at creation time. // Populates all variant struct properties at creation time.
@ -469,6 +493,9 @@ func (a *AndroidAppImport) populateAllVariantStructs() {
a.archVariants = reflect.New(archVariantGroupType).Interface() a.archVariants = reflect.New(archVariantGroupType).Interface()
a.AddProperties(a.archVariants) a.AddProperties(a.archVariants)
a.arch_dpiVariants = reflect.New(archdpiVariantGroupType).Interface()
a.AddProperties(a.arch_dpiVariants)
} }
func (a *AndroidAppImport) Privileged() bool { func (a *AndroidAppImport) Privileged() bool {
@ -523,6 +550,42 @@ func createVariantGroupType(variants []string, variantGroupName string) reflect.
}) })
} }
func createArchDpiVariantGroupType(archNames []string, dpiNames []string) reflect.Type {
props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
dpiVariantFields := make([]reflect.StructField, len(dpiNames))
for i, variant_dpi := range dpiNames {
dpiVariantFields[i] = reflect.StructField{
Name: proptools.FieldNameForProperty(variant_dpi),
Type: props,
}
}
dpiVariantGroupStruct := reflect.StructOf(dpiVariantFields)
dpi_struct := reflect.StructOf([]reflect.StructField{
{
Name: DpiGroupName,
Type: reflect.PointerTo(dpiVariantGroupStruct),
},
})
archVariantFields := make([]reflect.StructField, len(archNames))
for i, variant_arch := range archNames {
archVariantFields[i] = reflect.StructField{
Name: proptools.FieldNameForProperty(variant_arch),
Type: reflect.PointerTo(dpi_struct),
}
}
archVariantGroupStruct := reflect.StructOf(archVariantFields)
return_struct := reflect.StructOf([]reflect.StructField{
{
Name: ArchGroupName,
Type: reflect.PointerTo(archVariantGroupStruct),
},
})
return return_struct
}
// android_app_import imports a prebuilt apk with additional processing specified in the module. // android_app_import imports a prebuilt apk with additional processing specified in the module.
// DPI-specific apk source files can be specified using dpi_variants. Example: // DPI-specific apk source files can be specified using dpi_variants. Example:
// //

View file

@ -472,6 +472,35 @@ func TestAndroidAppImport_ArchVariants(t *testing.T) {
artifactPath: "prebuilts/apk/app_arm.apk", artifactPath: "prebuilts/apk/app_arm.apk",
installPath: "/system/app/foo/foo.apk", installPath: "/system/app/foo/foo.apk",
}, },
{
name: "matching arch and dpi_variants",
bp: `
android_app_import {
name: "foo",
apk: "prebuilts/apk/app.apk",
arch: {
arm64: {
apk: "prebuilts/apk/app_arm64.apk",
dpi_variants: {
mdpi: {
apk: "prebuilts/apk/app_arm64_mdpi.apk",
},
xhdpi: {
apk: "prebuilts/apk/app_arm64_xhdpi.apk",
},
},
},
},
presigned: true,
dex_preopt: {
enabled: true,
},
}
`,
expected: "verify_uses_libraries/apk/app_arm64_xhdpi.apk",
artifactPath: "prebuilts/apk/app_arm64_xhdpi.apk",
installPath: "/system/app/foo/foo.apk",
},
} }
for _, test := range testCases { for _, test := range testCases {