Search in sources :

Example 6 with ResourceTableEntry

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

the class ResourcesUtils method filterResourceTable.

/**
 * Filters the given resource table according to the specified criteria.
 *
 * <p>If any of {@link Package}, {@link Type} or {@link Entry} is empty after the filtering, it
 * gets removed from the table altogether.
 *
 * @param originalTable the original resource table
 * @param removeEntryPredicate determines whether an entry should be completely removed with all
 *     their configurations
 * @param configValuesFilterFn computes a new {@link Entry} with filtered {@link ConfigValue} list
 *     (possibly empty)
 * @return filtered resource table
 */
public static ResourceTable filterResourceTable(ResourceTable originalTable, Predicate<ResourceTableEntry> removeEntryPredicate, Function<ResourceTableEntry, Entry> configValuesFilterFn) {
    ResourceTable.Builder filteredTable = originalTable.toBuilder();
    for (int pkgIdx = filteredTable.getPackageCount() - 1; pkgIdx >= 0; pkgIdx--) {
        Package.Builder pkg = filteredTable.getPackageBuilder(pkgIdx);
        for (int typeIdx = pkg.getTypeCount() - 1; typeIdx >= 0; typeIdx--) {
            Type.Builder type = pkg.getTypeBuilder(typeIdx);
            List<Entry> unfilteredEntries = type.getEntryList();
            type.clearEntry();
            for (Entry unfilteredEntry : unfilteredEntries) {
                ResourceTableEntry entry = ResourceTableEntry.create(filteredTable.getPackage(pkgIdx), pkg.getType(typeIdx), unfilteredEntry);
                if (removeEntryPredicate.test(entry)) {
                    continue;
                }
                Entry filteredEntry = configValuesFilterFn.apply(entry);
                if (filteredEntry.getConfigValueCount() > 0) {
                    type.addEntry(filteredEntry);
                }
            }
            if (type.getEntryCount() == 0) {
                pkg.removeType(typeIdx);
            }
        }
        if (pkg.getTypeCount() == 0) {
            filteredTable.removePackage(pkgIdx);
        }
    }
    return filteredTable.build();
}
Also used : Type(com.android.aapt.Resources.Type) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Entry(com.android.aapt.Resources.Entry) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Package(com.android.aapt.Resources.Package) ResourceTable(com.android.aapt.Resources.ResourceTable)

Example 7 with ResourceTableEntry

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

the class ResourcesUtilsTest method filter_withRemoveTypePredicate.

@Test
public void filter_withRemoveTypePredicate() throws Exception {
    ResourceTable table = resourceTable(pkg(0x7f, "package.without.density.resources", type(0x01, "drawable", entry(0x00, "layout_main", fileReference("res/drawable/image.png", Configuration.getDefaultInstance()))), type(0x02, "layout", entry(0x00, "layout_menu", fileReference("res/layout/menu.xml", Configuration.getDefaultInstance())))));
    ResourceTable filteredTable = ResourcesUtils.filterResourceTable(table, resource -> resource.getType().getTypeId().getId() == 0x02, ResourceTableEntry::getEntry);
    assertThat(filteredTable).ignoringRepeatedFieldOrder().isEqualTo(resourceTable(pkg(0x7f, "package.without.density.resources", type(0x01, "drawable", entry(0x00, "layout_main", fileReference("res/drawable/image.png", Configuration.getDefaultInstance()))))));
}
Also used : ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 8 with ResourceTableEntry

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

the class DumpManager method printResources.

void printResources(Predicate<ResourceTableEntry> resourcePredicate, boolean printValues) {
    ImmutableList<ResourceTable> resourceTables;
    try (ZipFile zipFile = new ZipFile(bundlePath.toFile())) {
        resourceTables = ZipUtils.allFileEntriesPaths(zipFile).filter(path -> path.endsWith(SpecialModuleEntry.RESOURCE_TABLE.getPath())).map(path -> extractAndParse(zipFile, path, ResourceTable::parseFrom)).collect(toImmutableList());
    } catch (IOException e) {
        throw new UncheckedIOException("Error occurred when reading the bundle.", e);
    }
    ImmutableListMultimap<String, ResourceTableEntry> entriesByPackageName = resourceTables.stream().flatMap(ResourcesUtils::entries).filter(resourcePredicate).collect(groupingBySortedKeys(entry -> entry.getPackage().getPackageName()));
    for (String packageName : entriesByPackageName.keySet()) {
        printStream.printf("Package '%s':%n", packageName);
        entriesByPackageName.get(packageName).forEach(entry -> printEntry(entry, printValues));
        printStream.println();
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPath(javax.xml.xpath.XPath) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) XmlProtoNode(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoNode) XPathResolver(com.android.tools.build.bundletool.xml.XPathResolver) CollectorUtils.groupingBySortedKeys(com.android.tools.build.bundletool.model.utils.CollectorUtils.groupingBySortedKeys) XPathResult(com.android.tools.build.bundletool.xml.XPathResolver.XPathResult) XPathExpression(javax.xml.xpath.XPathExpression) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) XmlUtils(com.android.tools.build.bundletool.xml.XmlUtils) ImmutableList(com.google.common.collect.ImmutableList) XmlProtoToXmlConverter(com.android.tools.build.bundletool.xml.XmlProtoToXmlConverter) Document(org.w3c.dom.Document) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) ResourceTable(com.android.aapt.Resources.ResourceTable) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) XmlNamespaceContext(com.android.tools.build.bundletool.xml.XmlNamespaceContext) ZipException(java.util.zip.ZipException) ConfigValue(com.android.aapt.Resources.ConfigValue) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) Predicate(java.util.function.Predicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) IOException(java.io.IOException) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) UncheckedIOException(java.io.UncheckedIOException) XPathFactory(javax.xml.xpath.XPathFactory) XmlProtoPrintUtils(com.android.tools.build.bundletool.model.utils.xmlproto.XmlProtoPrintUtils) XmlNode(com.android.aapt.Resources.XmlNode) JsonFormat(com.google.protobuf.util.JsonFormat) BundleConfig(com.android.bundle.Config.BundleConfig) ResourcesUtils(com.android.tools.build.bundletool.model.utils.ResourcesUtils) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) Optional(java.util.Optional) ZipUtils(com.android.tools.build.bundletool.model.utils.ZipUtils) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) InputStream(java.io.InputStream) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ZipFile(java.util.zip.ZipFile) ResourcesUtils(com.android.tools.build.bundletool.model.utils.ResourcesUtils) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ResourceTable(com.android.aapt.Resources.ResourceTable)

Example 9 with ResourceTableEntry

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

the class ArchivedApksGenerator method getArchivedResourceTable.

private Optional<ResourceTable> getArchivedResourceTable(AppBundle appBundle, BundleModule bundleModule, AndroidManifest archivedManifest) throws IOException {
    if (!bundleModule.getResourceTable().isPresent()) {
        return Optional.empty();
    }
    ImmutableSet<ResourceId> referredResources = new ResourceAnalyzer(appBundle).findAllAppResourcesReachableFromManifest(archivedManifest);
    ResourceTable archivedResourceTable = ResourcesUtils.filterResourceTable(bundleModule.getResourceTable().get(), /* removeEntryPredicate= */
    entry -> !referredResources.contains(entry.getResourceId()), /* configValuesFilterFn= */
    ResourceTableEntry::getEntry);
    return Optional.of(archivedResourceTable);
}
Also used : ResourceAnalyzer(com.android.tools.build.bundletool.splitters.ResourceAnalyzer) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ResourceTable(com.android.aapt.Resources.ResourceTable)

Example 10 with ResourceTableEntry

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

the class DumpCommand method parseResourcePredicate.

private Predicate<ResourceTableEntry> parseResourcePredicate() {
    if (getResourceId().isPresent()) {
        return entry -> entry.getResourceId().getFullResourceId() == getResourceId().get().intValue();
    }
    if (getResourceName().isPresent()) {
        String resourceName = getResourceName().get();
        Matcher matcher = RESOURCE_NAME_PATTERN.matcher(resourceName);
        if (!matcher.matches()) {
            throw InvalidCommandException.builder().withInternalMessage("Resource name must match the format '<type>/<name>', e.g. 'drawable/icon'.").build();
        }
        return entry -> entry.getType().getName().equals(matcher.group("type")) && entry.getEntry().getName().equals(matcher.group("name"));
    }
    return entry -> true;
}
Also used : PrintStream(java.io.PrintStream) Arrays(java.util.Arrays) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) ParsedFlags(com.android.tools.build.bundletool.flags.ParsedFlags) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Matcher(java.util.regex.Matcher) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Flag(com.android.tools.build.bundletool.flags.Flag) Function.identity(java.util.function.Function.identity) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) AutoValue(com.google.auto.value.AutoValue) FlagDescription(com.android.tools.build.bundletool.commands.CommandHelp.FlagDescription) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) FilePreconditions.checkFileExistsAndReadable(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileExistsAndReadable) Path(java.nio.file.Path) CommandDescription(com.android.tools.build.bundletool.commands.CommandHelp.CommandDescription) Matcher(java.util.regex.Matcher)

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