Search in sources :

Example 11 with ResourceId

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

the class ResourceAnalyzerTest method attributeWithCompiledItem_nonRef_ignored.

@Test
public void attributeWithCompiledItem_nonRef_ignored() throws Exception {
    XmlNode manifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(xmlElement("application", // Attributes of all possible types, except for ref.
    ImmutableList.of(// str
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "str", Item.newBuilder().setStr(String.getDefaultInstance()).build()), // raw_str
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "raw_str", Item.newBuilder().setRawStr(RawString.getDefaultInstance()).build()), // styled_str
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "styled_str", Item.newBuilder().setStyledStr(StyledString.getDefaultInstance()).build()), // file
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "file", Item.newBuilder().setFile(FileReference.getDefaultInstance()).build()), // id
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "id", Item.newBuilder().setId(Id.getDefaultInstance()).build()), // prim
    xmlCompiledItemAttribute(NO_NAMESPACE_URI, "prim", Item.newBuilder().setPrim(Primitive.getDefaultInstance()).build()))))))).getManifestRoot().getProto();
    ResourceTable resourceTable = resourceTable(pkg(0x0f, "com.test.app", type(0x01, "string", entry(0x0099, "not_referenced", value("", DEFAULT_CONFIG)))));
    AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(manifest).setResourceTable(resourceTable)).build();
    ImmutableSet<ResourceId> resourceIds = new ResourceAnalyzer(appBundle).findAllAppResourcesReachableFromBaseManifest();
    assertThat(resourceIds).isEmpty();
}
Also used : RawString(com.android.aapt.Resources.RawString) Value(com.android.aapt.Resources.Value) String(com.android.aapt.Resources.String) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) RunWith(org.junit.runner.RunWith) Primitive(com.android.aapt.Resources.Primitive) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ResourcesTableFactory.resourceTable(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable) Symbol(com.android.aapt.Resources.Attribute.Symbol) ANDROID_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI) ManifestProtoUtils.xmlCompiledItemAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlCompiledItemAttribute) Item(com.android.aapt.Resources.Item) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ImmutableList(com.google.common.collect.ImmutableList) Theories(org.junit.experimental.theories.Theories) ResourcesTableFactory.reference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.reference) ResourcesTableFactory.entry(com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry) Attribute(com.android.aapt.Resources.Attribute) ResourcesTableFactory.pkg(com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg) ResourcesTableFactory.type(com.android.tools.build.bundletool.testing.ResourcesTableFactory.type) ResourceTable(com.android.aapt.Resources.ResourceTable) Nullable(javax.annotation.Nullable) StyledString(com.android.aapt.Resources.StyledString) ManifestProtoUtils.xmlNode(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlNode) ImmutableSet(com.google.common.collect.ImmutableSet) Style(com.android.aapt.Resources.Style) ResourcesTableFactory.value(com.android.tools.build.bundletool.testing.ResourcesTableFactory.value) ConfigValue(com.android.aapt.Resources.ConfigValue) Reference(com.android.aapt.Resources.Reference) Test(org.junit.Test) NO_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI) ManifestProtoUtils.xmlElement(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlElement) Truth.assertThat(com.google.common.truth.Truth.assertThat) ResourcesTableFactory.fileReference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference) CompoundValue(com.android.aapt.Resources.CompoundValue) Id(com.android.aapt.Resources.Id) FileReference(com.android.aapt.Resources.FileReference) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ManifestProtoUtils.xmlResourceReferenceAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlResourceReferenceAttribute) XmlNode(com.android.aapt.Resources.XmlNode) AppBundle(com.android.tools.build.bundletool.model.AppBundle) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) XmlNode(com.android.aapt.Resources.XmlNode) AppBundle(com.android.tools.build.bundletool.model.AppBundle) ResourceId(com.android.tools.build.bundletool.model.ResourceId) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 12 with ResourceId

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

the class ResourceAnalyzerTest method transitive_compoundValue_style.

@Test
public void transitive_compoundValue_style() throws Exception {
    // AndroidManifest
    // --> 0x7f010001 (app_theme) --> 0x7f010002 (parent_theme) --> 0x7f020001 (string)
    // |-> 0x7f020002 (string)
    // |-> 0x7f020003 (string)
    XmlNode manifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(xmlElement("application", xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, "theme", /* attrResourceId= */
    0x999999, /* valueResourceId= */
    0x7f010001)))))).getManifestRoot().getProto();
    ResourceTable resourceTable = resourceTable(pkg(0x7f, "com.test.app", type(0x01, "theme", entry(0x0001, "app_theme", compoundValueStyleWithResourceReferences(/* parentId= */
    0x7f010002, /* referencedResourceIds= */
    new int[] { 0x7f020002, 0x7f020003 })), entry(0x0002, "parent_theme", compoundValueStyleWithResourceReferences(/* parentId= */
    null, /* referencedResourceIds= */
    new int[] { 0x7f020001 }))), type(0x02, "string", entry(0x0001, "parent_theme_str", value("hello", DEFAULT_CONFIG)), entry(0x0002, "app_theme_str1", value("hello", DEFAULT_CONFIG)), entry(0x0003, "app_theme_str2", value("hello", DEFAULT_CONFIG)), entry(0x0099, "not_referenced", value("", DEFAULT_CONFIG)))));
    AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(manifest).setResourceTable(resourceTable)).build();
    ImmutableSet<ResourceId> resourceIds = new ResourceAnalyzer(appBundle).findAllAppResourcesReachableFromBaseManifest();
    assertThat(resourceIds).containsExactly(ResourceId.create(0x7f010001), ResourceId.create(0x7f010002), ResourceId.create(0x7f020001), ResourceId.create(0x7f020002), ResourceId.create(0x7f020003));
}
Also used : RawString(com.android.aapt.Resources.RawString) Value(com.android.aapt.Resources.Value) String(com.android.aapt.Resources.String) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) RunWith(org.junit.runner.RunWith) Primitive(com.android.aapt.Resources.Primitive) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ResourcesTableFactory.resourceTable(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable) Symbol(com.android.aapt.Resources.Attribute.Symbol) ANDROID_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.ANDROID_NAMESPACE_URI) ManifestProtoUtils.xmlCompiledItemAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlCompiledItemAttribute) Item(com.android.aapt.Resources.Item) ResourceId(com.android.tools.build.bundletool.model.ResourceId) ImmutableList(com.google.common.collect.ImmutableList) Theories(org.junit.experimental.theories.Theories) ResourcesTableFactory.reference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.reference) ResourcesTableFactory.entry(com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry) Attribute(com.android.aapt.Resources.Attribute) ResourcesTableFactory.pkg(com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg) ResourcesTableFactory.type(com.android.tools.build.bundletool.testing.ResourcesTableFactory.type) ResourceTable(com.android.aapt.Resources.ResourceTable) Nullable(javax.annotation.Nullable) StyledString(com.android.aapt.Resources.StyledString) ManifestProtoUtils.xmlNode(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlNode) ImmutableSet(com.google.common.collect.ImmutableSet) Style(com.android.aapt.Resources.Style) ResourcesTableFactory.value(com.android.tools.build.bundletool.testing.ResourcesTableFactory.value) ConfigValue(com.android.aapt.Resources.ConfigValue) Reference(com.android.aapt.Resources.Reference) Test(org.junit.Test) NO_NAMESPACE_URI(com.android.tools.build.bundletool.model.AndroidManifest.NO_NAMESPACE_URI) ManifestProtoUtils.xmlElement(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlElement) Truth.assertThat(com.google.common.truth.Truth.assertThat) ResourcesTableFactory.fileReference(com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference) CompoundValue(com.android.aapt.Resources.CompoundValue) Id(com.android.aapt.Resources.Id) FileReference(com.android.aapt.Resources.FileReference) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ManifestProtoUtils.xmlResourceReferenceAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.xmlResourceReferenceAttribute) XmlNode(com.android.aapt.Resources.XmlNode) AppBundle(com.android.tools.build.bundletool.model.AppBundle) Configuration(com.android.aapt.ConfigurationOuterClass.Configuration) XmlNode(com.android.aapt.Resources.XmlNode) AppBundle(com.android.tools.build.bundletool.model.AppBundle) ResourceId(com.android.tools.build.bundletool.model.ResourceId) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 13 with ResourceId

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

the class LocaleConfigXmlInjector method injectLocaleConfigXml.

private static ModuleSplit injectLocaleConfigXml(ModuleSplit split, XmlNode xmlNode) {
    ResourceInjector resourceInjector = ResourceInjector.fromModuleSplit(split);
    ResourceId resourceId = resourceInjector.addResource(XML_TYPE_NAME, createXmlEntry());
    ModuleEntry localesConfigEntry = addLocalesConfigEntry(xmlNode);
    return split.toBuilder().setResourceTable(resourceInjector.build()).setEntries(ImmutableList.<ModuleEntry>builder().addAll(split.getEntries()).add(localesConfigEntry).build()).setAndroidManifest(split.getAndroidManifest().toEditor().setLocaleConfig(resourceId.getFullResourceId()).save()).build();
}
Also used : ResourceId(com.android.tools.build.bundletool.model.ResourceId) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ResourceInjector(com.android.tools.build.bundletool.model.ResourceInjector)

Example 14 with ResourceId

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

the class SplitsXmlInjector method injectSplitsXml.

private static ModuleSplit injectSplitsXml(ModuleSplit split, XmlNode xmlNode) {
    ZipPath resourcePath = getUniqueResourcePath(split);
    ResourceInjector resourceInjector = ResourceInjector.fromModuleSplit(split);
    ResourceId resourceId = resourceInjector.addResource(XML_TYPE_NAME, createXmlEntry(resourcePath));
    return split.toBuilder().setResourceTable(resourceInjector.build()).setEntries(ImmutableList.<ModuleEntry>builder().addAll(split.getEntries()).add(ModuleEntry.builder().setPath(resourcePath).setContent(ByteSource.wrap(xmlNode.toByteArray())).build()).build()).setAndroidManifest(split.getAndroidManifest().toEditor().addMetaDataResourceId(METADATA_KEY, resourceId.getFullResourceId()).save()).build();
}
Also used : ResourceId(com.android.tools.build.bundletool.model.ResourceId) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ResourceInjector(com.android.tools.build.bundletool.model.ResourceInjector)

Example 15 with ResourceId

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

the class ScreenDensityResourcesSplitter method getResourceTableForDefaultSplit.

/**
 * Returns a resource table for master split.
 *
 * <p>It will be stripped of any entries claimed by the config splits.
 */
private ResourceTable getResourceTableForDefaultSplit(ModuleSplit split, ImmutableMultimap<ResourceId, ConfigValue> claimedConfigs) {
    checkArgument(split.getResourceTable().isPresent(), "Expected the split to contain Resource Table.");
    ResourceTable.Builder prunedTable = split.getResourceTable().get().toBuilder();
    for (Package.Builder packageBuilder : prunedTable.getPackageBuilderList()) {
        for (Type.Builder typeBuilder : packageBuilder.getTypeBuilderList()) {
            List<Entry> newEntries = new ArrayList<>();
            for (Entry entry : typeBuilder.getEntryList()) {
                ResourceId resourceId = ResourceId.create(packageBuilder, typeBuilder, entry);
                ImmutableList<ConfigValue> allConfigsExceptClaimed = entry.getConfigValueList().stream().filter(configValue -> !claimedConfigs.containsEntry(resourceId, configValue)).collect(toImmutableList());
                Entry.Builder newEntry = entry.toBuilder().clearConfigValue().addAllConfigValue(allConfigsExceptClaimed);
                if (newEntry.getConfigValueCount() > 0) {
                    // if everything was claimed we skip the entry.
                    newEntries.add(newEntry.build());
                }
            }
            typeBuilder.clearEntry().addAllEntry(newEntries);
        }
    }
    return prunedTable.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) ConfigValue(com.android.aapt.Resources.ConfigValue) ArrayList(java.util.ArrayList) Type(com.android.aapt.Resources.Type) ResourceTableEntry(com.android.tools.build.bundletool.model.ResourceTableEntry) Entry(com.android.aapt.Resources.Entry) ResourceId(com.android.tools.build.bundletool.model.ResourceId) Package(com.android.aapt.Resources.Package) ResourceTable(com.android.aapt.Resources.ResourceTable)

Aggregations

ResourceId (com.android.tools.build.bundletool.model.ResourceId)20 ResourceTable (com.android.aapt.Resources.ResourceTable)17 ConfigValue (com.android.aapt.Resources.ConfigValue)16 Configuration (com.android.aapt.ConfigurationOuterClass.Configuration)15 Item (com.android.aapt.Resources.Item)15 ImmutableList (com.google.common.collect.ImmutableList)15 ImmutableSet (com.google.common.collect.ImmutableSet)15 Value (com.android.aapt.Resources.Value)14 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)14 ResourcesTableFactory.entry (com.android.tools.build.bundletool.testing.ResourcesTableFactory.entry)14 ResourcesTableFactory.fileReference (com.android.tools.build.bundletool.testing.ResourcesTableFactory.fileReference)14 ResourcesTableFactory.pkg (com.android.tools.build.bundletool.testing.ResourcesTableFactory.pkg)14 ResourcesTableFactory.resourceTable (com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTable)14 ResourcesTableFactory.type (com.android.tools.build.bundletool.testing.ResourcesTableFactory.type)14 Truth.assertThat (com.google.common.truth.Truth.assertThat)14 Test (org.junit.Test)14 Theories (org.junit.experimental.theories.Theories)14 RunWith (org.junit.runner.RunWith)14 CompoundValue (com.android.aapt.Resources.CompoundValue)10 Attribute (com.android.aapt.Resources.Attribute)9