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);
}
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());
}
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);
}
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();
}
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();
}
Aggregations