use of com.android.tools.build.bundletool.model.ResourceId 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.ResourceId in project bundletool by google.
the class ResourceTableValidator method checkResourceIdsAreUnique.
private static void checkResourceIdsAreUnique(ImmutableList<BundleModule> modules) {
HashSet<ResourceId> usedResourceIds = newHashSet();
for (BundleModule module : modules) {
ResourceTable resourceTable = module.getResourceTable().orElse(ResourceTable.getDefaultInstance());
ResourcesUtils.entries(resourceTable).forEach(resourceTableEntry -> {
boolean foundDuplicate = !usedResourceIds.add(resourceTableEntry.getResourceId());
if (foundDuplicate) {
throw InvalidBundleException.builder().withUserMessage("Duplicate resource id (%s).", resourceTableEntry.getResourceId().toString()).build();
}
});
}
}
use of com.android.tools.build.bundletool.model.ResourceId in project bundletool by google.
the class ScreenDensityResourcesSplitterTest method lowestDensityConfigsPinnedToMaster_mixedConfigsInSameDensityBucket.
@Test
public void lowestDensityConfigsPinnedToMaster_mixedConfigsInSameDensityBucket() throws Exception {
BundleModule testModule = new BundleModuleBuilder("testModule").addFile("res/drawable-ldpi/image.jpg").addFile("res/drawable-ldpi-v21/image.jpg").addFile("res/drawable-ldpi-v24/image.jpg").addFile("res/drawable-xxhdpi/image.jpg").addFile("res/drawable-xxhdpi-v21/image.jpg").addFile("res/drawable-xxhdpi-v24/image.jpg").setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addFileResourceForMultipleConfigs("drawable", "image", ImmutableMap.<Configuration, String>builder().put(mergeConfigs(LDPI), "res/drawable-ldpi/image.jpg").put(mergeConfigs(LDPI, sdk(21)), "res/drawable-ldpi-v21/image.jpg").put(mergeConfigs(LDPI, sdk(24)), "res/drawable-ldpi-v24/image.jpg").put(mergeConfigs(XXXHDPI), "res/drawable-xxhdpi/image.jpg").put(mergeConfigs(XXHDPI, sdk(21)), "res/drawable-xxhdpi-v21/image.jpg").put(mergeConfigs(XXHDPI, sdk(24)), "res/drawable-xxhdpi-v24/image.jpg").build()).build()).setManifest(androidManifest("com.test.app")).build();
// 0x7f010000 is the "drawable/image" resource.
Predicate<ResourceId> pinnedLowDensityResourcesPredicate = resourceId -> resourceId.getFullResourceId() == 0x7f010000;
ScreenDensityResourcesSplitter splitter = new ScreenDensityResourcesSplitter(BundleToolVersion.getCurrentVersion(), NO_RESOURCES_PINNED_TO_MASTER, pinnedLowDensityResourcesPredicate, /* pinLowestBucketOfStylesToMaster= */
false);
ImmutableCollection<ModuleSplit> densitySplits = splitter.split(ModuleSplit.forResources(testModule));
ModuleSplit masterSplit = densitySplits.stream().filter(split -> split.isMasterSplit()).collect(onlyElement());
assertThat(extractPaths(masterSplit.getEntries())).containsExactly("res/drawable-ldpi/image.jpg", "res/drawable-ldpi-v21/image.jpg", "res/drawable-ldpi-v24/image.jpg");
ImmutableList<ModuleSplit> configSplits = densitySplits.stream().filter(split -> !split.isMasterSplit()).collect(toImmutableList());
assertThat(configSplits).isNotEmpty();
for (ModuleSplit configSplit : configSplits) {
assertThat(extractPaths(configSplit.getEntries())).containsNoneOf("res/drawable-ldpi/image.jpg", "res/drawable-ldpi-v21/image.jpg", "res/drawable-ldpi-v24/image.jpg");
}
}
use of com.android.tools.build.bundletool.model.ResourceId in project bundletool by google.
the class ResourceAnalyzerTest method emptyManifest.
@Test
public void emptyManifest() throws Exception {
XmlNode manifest = androidManifest("com.app");
ResourceTable resourceTable = resourceTable(pkg(0x7f, "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();
}
use of com.android.tools.build.bundletool.model.ResourceId in project bundletool by google.
the class ResourceAnalyzerTest method transitive_compoundValue_attribute.
@Test
public void transitive_compoundValue_attribute() throws Exception {
// AndroidManifest --> 0x7f01000 (attr) --> 0x7f020002 (string)
// |-> 0x7f020003 (string)
XmlNode manifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(xmlElement("application", xmlResourceReferenceAttribute(NO_NAMESPACE_URI, "xml_attribute_confusingly_pointing_to_a_resource_attribute", /* attrResourceId= */
0x999999, /* valueResourceId= */
0x7f010001)))))).getManifestRoot().getProto();
ResourceTable resourceTable = resourceTable(pkg(0x7f, "com.test.app", type(0x01, "attr", entry(0x0001, "some_attr", compoundValueAttrWithResourceReferences(0x7f020002, 0x7f020003))), type(0x02, "string", entry(0x0002, "str1", value("hello", DEFAULT_CONFIG)), entry(0x0003, "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(0x7f020002), ResourceId.create(0x7f020003));
}
Aggregations