def add_uses_libraries(doc, new_uses_libraries, required): """Add additional <uses-library> tags Args: doc: The XML document. May be modified by this function. new_uses_libraries: The names of libraries to be added by this function. required: The value of android:required attribute. Can be true or false. Raises: RuntimeError: Invalid manifest """ manifest = parse_manifest(doc) elems = get_children_with_tag(manifest, 'application') application = elems[0] if len(elems) == 1 else None if len(elems) > 1: raise RuntimeError('found multiple <application> tags') elif not elems: application = doc.createElement('application') indent = get_indent(manifest.firstChild, 1) first = manifest.firstChild manifest.insertBefore(doc.createTextNode(indent), first) manifest.insertBefore(application, first) indent = get_indent(application.firstChild, 2) last = application.lastChild if last is not None and last.nodeType != minidom.Node.TEXT_NODE: last = None for name in new_uses_libraries: if find_child_with_attribute(application, 'uses-library', android_ns, 'name', name) is not None: # If the uses-library tag of the same 'name' attribute value exists, # respect it. continue ul = doc.createElement('uses-library') ul.setAttributeNS(android_ns, 'android:name', name) ul.setAttributeNS(android_ns, 'android:required', str(required).lower()) application.insertBefore(doc.createTextNode(indent), last) application.insertBefore(ul, last) # align the closing tag with the opening tag if it's not # indented if application.lastChild.nodeType != minidom.Node.TEXT_NODE: indent = get_indent(application.previousSibling, 1) application.appendChild(doc.createTextNode(indent))
def AddLoggingParent(android_manifest, logging_parent_value): """Add logging parent as an additional <meta-data> tag. Args: android_manifest: A string representing AndroidManifest.xml logging_parent_value: A string representing the logging parent value. Raises: RuntimeError: Invalid manifest Returns: A path to modified AndroidManifest.xml """ doc = minidom.parse(android_manifest) manifest = parse_manifest(doc) logging_parent_key = 'android.content.pm.LOGGING_PARENT' elems = get_children_with_tag(manifest, 'application') application = elems[0] if len(elems) == 1 else None if len(elems) > 1: raise RuntimeError('found multiple <application> tags') elif not elems: application = doc.createElement('application') indent = get_indent(manifest.firstChild, 1) first = manifest.firstChild manifest.insertBefore(doc.createTextNode(indent), first) manifest.insertBefore(application, first) indent = get_indent(application.firstChild, 2) last = application.lastChild if last is not None and last.nodeType != minidom.Node.TEXT_NODE: last = None if not find_child_with_attribute(application, 'meta-data', android_ns, 'name', logging_parent_key): ul = doc.createElement('meta-data') ul.setAttributeNS(android_ns, 'android:name', logging_parent_key) ul.setAttributeNS(android_ns, 'android:value', logging_parent_value) application.insertBefore(doc.createTextNode(indent), last) application.insertBefore(ul, last) last = application.lastChild if last and last.nodeType != minidom.Node.TEXT_NODE: indent = get_indent(application.previousSibling, 1) application.appendChild(doc.createTextNode(indent)) with tempfile.NamedTemporaryFile(delete=False) as temp: write_xml(temp, doc) return temp.name
def add_logging_parent(doc, logging_parent_value): """Add logging parent as an additional <meta-data> tag. Args: doc: The XML document. May be modified by this function. logging_parent_value: A string representing the logging parent value. Raises: RuntimeError: Invalid manifest """ manifest = parse_manifest(doc) logging_parent_key = 'android.content.pm.LOGGING_PARENT' elems = get_children_with_tag(manifest, 'application') application = elems[0] if len(elems) == 1 else None if len(elems) > 1: raise RuntimeError('found multiple <application> tags') elif not elems: application = doc.createElement('application') indent = get_indent(manifest.firstChild, 1) first = manifest.firstChild manifest.insertBefore(doc.createTextNode(indent), first) manifest.insertBefore(application, first) indent = get_indent(application.firstChild, 2) last = application.lastChild if last is not None and last.nodeType != minidom.Node.TEXT_NODE: last = None if not find_child_with_attribute(application, 'meta-data', android_ns, 'name', logging_parent_key): ul = doc.createElement('meta-data') ul.setAttributeNS(android_ns, 'android:name', logging_parent_key) ul.setAttributeNS(android_ns, 'android:value', logging_parent_value) application.insertBefore(doc.createTextNode(indent), last) application.insertBefore(ul, last) last = application.lastChild # align the closing tag with the opening tag if it's not # indented if last and last.nodeType != minidom.Node.TEXT_NODE: indent = get_indent(application.previousSibling, 1) application.appendChild(doc.createTextNode(indent))