use of com.android.aapt.Resources.FileReference in project bundletool by google.
the class ResourceAnalyzer method findAllReferencedAppResources.
private ImmutableSet<ResourceId> findAllReferencedAppResources(Item item) {
switch(item.getValueCase()) {
case REF:
if (item.getRef().getId() != 0) {
return ImmutableSet.of(ResourceId.create(item.getRef().getId()));
}
// we don't need to consider it.
break;
case FILE:
FileReference fileRef = item.getFile();
if (!fileRef.getType().equals(FileReference.Type.PROTO_XML)) {
return ImmutableSet.of();
}
ZipPath xmlResourcePath = ZipPath.create(fileRef.getPath());
try (InputStream is = appBundle.getBaseModule().getEntry(xmlResourcePath).get().getContent().openStream()) {
XmlNode xmlRoot = XmlNode.parseFrom(is);
return findAllReferencedAppResources(xmlRoot);
} catch (InvalidProtocolBufferException e) {
throw CommandExecutionException.builder().withInternalMessage("Error parsing XML file '%s'.", xmlResourcePath).withCause(e).build();
} catch (IOException e) {
throw new UncheckedIOException(String.format("Failed to parse file '%s' in base module.", xmlResourcePath), e);
}
default:
break;
}
return ImmutableSet.of();
}
use of com.android.aapt.Resources.FileReference in project bundletool by google.
the class ResourceAnalyzerTest method transitive_item_xmlFileWithResourceReference.
@Test
public void transitive_item_xmlFileWithResourceReference() throws Exception {
// AndroidManifest --> 0x7f010001 (xml file) --> 0x7f020002 (string)
XmlNode manifest = AndroidManifest.create(xmlNode(xmlElement("manifest", xmlNode(xmlElement("application", xmlResourceReferenceAttribute(NO_NAMESPACE_URI, "attr_pointing_to_xml", /* attrResourceId= */
0x999999, /* valueResourceId= */
0x7f010001)))))).getManifestRoot().getProto();
XmlNode embeddedXmlFile = AndroidManifest.create(xmlNode(xmlElement("root", xmlResourceReferenceAttribute(ANDROID_NAMESPACE_URI, "name", /* attrResourceId= */
0x999999, /* valueResourceId= */
0x7f020002)))).getManifestRoot().getProto();
ResourceTable resourceTable = resourceTable(pkg(0x7f, "com.test.app", type(0x01, "file", entry(0x0001, "xml_file", fileReference("res/xml/embedded.xml", FileReference.Type.PROTO_XML, DEFAULT_CONFIG))), type(0x02, "string", entry(0x0002, "name_str", value("hello", DEFAULT_CONFIG)), entry(0x0099, "not_referenced", value("", DEFAULT_CONFIG)))));
AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(manifest).setResourceTable(resourceTable).addFile("res/xml/embedded.xml", embeddedXmlFile.toByteArray())).build();
ImmutableSet<ResourceId> resourceIds = new ResourceAnalyzer(appBundle).findAllAppResourcesReachableFromBaseManifest();
assertThat(resourceIds).containsExactly(ResourceId.create(0x7f010001), ResourceId.create(0x7f020002));
}
Aggregations