Search in sources :

Example 1 with ResourceTableEntry

use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.

the class ResourceAnalyzer method transitiveClosure.

private ImmutableSet<ResourceId> transitiveClosure(ImmutableSet<ResourceId> anchorResources) throws IOException {
    Set<ResourceId> referencedResources = new HashSet<>();
    Queue<ResourceId> resourcesToInspect = new ArrayDeque<>();
    resourcesToInspect.addAll(anchorResources);
    while (!resourcesToInspect.isEmpty()) {
        ResourceId resourceId = resourcesToInspect.remove();
        if (referencedResources.contains(resourceId) || !baseModuleResourcesById.containsKey(resourceId)) {
            continue;
        }
        referencedResources.add(resourceId);
        ResourceTableEntry resourceEntry = baseModuleResourcesById.get(resourceId);
        for (ConfigValue configValue : resourceEntry.getEntry().getConfigValueList()) {
            switch(configValue.getValue().getValueCase()) {
                case ITEM:
                    Item item = configValue.getValue().getItem();
                    resourcesToInspect.addAll(findAllReferencedAppResources(item));
                    break;
                case COMPOUND_VALUE:
                    CompoundValue compoundValue = configValue.getValue().getCompoundValue();
                    resourcesToInspect.addAll(findAllReferencedAppResources(compoundValue));
                    break;
                case VALUE_NOT_SET:
            }
        }
    }
    return ImmutableSet.copyOf(referencedResources);
}
Also used : Item(com.android.aapt.Resources.Item) ConfigValue(com.android.aapt.Resources.ConfigValue) CompoundValue(com.android.aapt.Resources.CompoundValue) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Example 2 with ResourceTableEntry

use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.

the class ResourcesUtilsTest method filter_withEntryPredicate.

@Test
public void filter_withEntryPredicate() {
    ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addDrawableResource("image1", "drawable/image1.jpg").addDrawableResource("image2", "drawable/image2.jpg").addStringResource("label_hello", "Hello world").build();
    ResourceTable filteredTable = ResourcesUtils.filterResourceTable(resourceTable, /* removeEntryPredicate= */
    resourceTableEntry -> resourceTableEntry.getResourceId().getFullResourceId() == 0x7f010001, /* image2.jpg */
    ResourceTableEntry::getEntry);
    assertThat(filteredTable).ignoringRepeatedFieldOrder().isEqualTo(new ResourceTableBuilder().addPackage("com.test.app").addDrawableResource("image1", "drawable/image1.jpg").addStringResource("label_hello", "Hello world").build());
}
Also used : ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 3 with ResourceTableEntry

use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.

the class ResourcesUtilsTest method filter_noFiltering_returnsIdentity.

@Test
public void filter_noFiltering_returnsIdentity() throws Exception {
    ResourceTable table = resourceTable(pkg(0x7f, "package.without.density.resources", type(0x01, "drawable", entry(0x00, "image", fileReference("res/drawable/image.png", Configuration.getDefaultInstance())))));
    ResourceTable filteredTable = ResourcesUtils.filterResourceTable(table, Predicates.alwaysFalse(), ResourceTableEntry::getEntry);
    assertThat(filteredTable).ignoringRepeatedFieldOrder().isEqualTo(table);
}
Also used : ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 4 with ResourceTableEntry

use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.

the class ScreenDensityResourcesSplitter method filterEntryForDensity.

/**
 * Only leaves the density specific config values optimized for a given density.
 *
 * <p>As any other resource qualifiers can be requested when delivering resources, the algorithm
 * chooses the best match only within group of resources differing by density only.
 *
 * @param tableEntry the entry to be updated
 * @param targetDensity the desired density to match
 * @return the entry with the best matching density config values.
 */
private Entry filterEntryForDensity(ResourceTableEntry tableEntry, DensityAlias targetDensity) {
    Entry initialEntry = tableEntry.getEntry();
    // Groups together configs that only differ on density.
    ImmutableMap<Configuration, ? extends List<ConfigValue>> configValuesByConfiguration = initialEntry.getConfigValueList().stream().filter(configValue -> RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT.enabledForVersion(bundleVersion) || configValue.getConfig().getDensity() != DEFAULT_DENSITY_VALUE).collect(groupingByDeterministic(configValue -> clearDensity(configValue.getConfig())));
    // the master split.
    if (RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT.enabledForVersion(bundleVersion)) {
        configValuesByConfiguration = ImmutableMap.copyOf(Maps.filterValues(configValuesByConfiguration, configValues -> configValues.size() > 1));
    }
    ImmutableList<List<ConfigValue>> densityGroups = ImmutableList.copyOf(configValuesByConfiguration.values());
    // We want to pin specific configs to the master, instead of putting them into a density split.
    Predicate<ConfigValue> pinConfigToMaster;
    if (pinWholeResourceToMaster.test(tableEntry.getResourceId())) {
        pinConfigToMaster = anyConfig -> true;
    } else if (pinLowestBucketToMaster(tableEntry)) {
        ImmutableSet<ConfigValue> lowDensityConfigsPinnedToMaster = pickBestDensityForEachGroup(densityGroups, getLowestDensity(densityBuckets)).collect(toImmutableSet());
        pinConfigToMaster = lowDensityConfigsPinnedToMaster::contains;
    } else {
        pinConfigToMaster = anyConfig -> false;
    }
    ImmutableList<ConfigValue> valuesToKeep = pickBestDensityForEachGroup(densityGroups, targetDensity).filter(config -> !pinConfigToMaster.test(config)).collect(toImmutableList());
    return initialEntry.toBuilder().clearConfigValue().addAllConfigValue(valuesToKeep).build();
}
Also used : DEFAULT_DENSITY_VALUE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.DEFAULT_DENSITY_VALUE) ScreenDensitySelector(com.android.tools.build.bundletool.model.targeting.ScreenDensitySelector) ImmutableCollection(com.google.common.collect.ImmutableCollection) CollectorUtils.groupingByDeterministic(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingByDeterministic) ArrayList(java.util.ArrayList) ManifestMutator.withSplitsRequired(com.android.tools.build.bundletool.model.ManifestMutator.withSplitsRequired) Type(com.android.aapt.Resources.Type) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ImmutableList(com.google.common.collect.ImmutableList) ResourcesUtils.getLowestDensity(com.android.tools.build.bundletool.model.utils.ResourcesUtils.getLowestDensity) Package(com.android.aapt.Resources.Package) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) DensityAlias(com.android.bundle.Targeting.ScreenDensity.DensityAlias) Version(com.android.tools.build.bundletool.model.version.Version) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ResourceTable(com.android.aapt.Resources.ResourceTable) MIPMAP_TYPE(com.android.tools.build.bundletool.model.utils.ResourcesUtils.MIPMAP_TYPE) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigValue(com.android.aapt.Resources.ConfigValue) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Entry(com.android.aapt.Resources.Entry) Maps(com.google.common.collect.Maps) ScreenDensity(com.android.bundle.Targeting.ScreenDensity) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ScreenDensityTargeting(com.android.bundle.Targeting.ScreenDensityTargeting) List(java.util.List) Stream(java.util.stream.Stream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ResourcesUtils(com.android.tools.build.bundletool.model.utils.ResourcesUtils) Optional(java.util.Optional) RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.RESOURCES_WITH_NO_ALTERNATIVES_IN_MASTER_SPLIT) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Entry(com.android.aapt.Resources.Entry) ConfigValue(com.android.aapt.Resources.ConfigValue) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Example 5 with ResourceTableEntry

use of com.android.tools.build.bundletool.model.ResourceTableEntry in project bundletool by google.

the class LanguageResourcesSplitter method groupByLanguage.

private ImmutableMap<String, ResourceTable> groupByLanguage(ResourceTable table, boolean hasNonResourceEntries) {
    ImmutableSet<String> languages = ResourcesUtils.getAllLanguages(table);
    ImmutableMap.Builder<String, ResourceTable> resourceTableByLanguage = new ImmutableMap.Builder<>();
    for (String language : languages) {
        ResourceTable languageResourceTable = filterByLanguage(table, language);
        // a language split.
        if (!languageResourceTable.equals(ResourceTable.getDefaultInstance())) {
            resourceTableByLanguage.put(language, languageResourceTable);
        }
    }
    // non resource related entries and no pinned entries.
    if (!languages.contains("")) {
        ResourceTable pinnedResources = ResourcesUtils.filterResourceTable(table, /* removeEntryPredicate= */
        pinResourceToMaster.negate(), /* configValuesFilterFn= */
        ResourceTableEntry::getEntry);
        if (hasNonResourceEntries || entries(pinnedResources).count() > 0) {
            resourceTableByLanguage.put("", pinnedResources);
        }
    }
    return resourceTableByLanguage.build();
}
Also used : ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ImmutableMap(com.google.common.collect.ImmutableMap) ResourceTable(com.android.aapt.Resources.ResourceTable)

Aggregations

ResourceTableEntry (com.android.tools.build.bundletool.model.ResourceTableEntry)10 ResourceTable (com.android.aapt.Resources.ResourceTable)8 ConfigValue (com.android.aapt.Resources.ConfigValue)3 ResourceId (com.android.tools.build.bundletool.model.ResourceId)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Optional (java.util.Optional)3 Predicate (java.util.function.Predicate)3 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)2 Entry (com.android.aapt.Resources.Entry)2 Package (com.android.aapt.Resources.Package)2 Type (com.android.aapt.Resources.Type)2 BundleModuleName (com.android.tools.build.bundletool.model.BundleModuleName)2 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)2 ResourcesUtils (com.android.tools.build.bundletool.model.utils.ResourcesUtils)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 Test (org.junit.Test)2 CompoundValue (com.android.aapt.Resources.CompoundValue)1