private static void addService(XmlDocument document, XmlElement application ) { // <service // android:name="com.android.tools.fd.runtime.InstantRunService" // android:exported="true"/> Element service = document.getXml().createElement(SdkConstants.TAG_SERVICE); setAndroidAttribute(service, SdkConstants.ATTR_NAME, BOOTSTRAP_INSTANT_RUN_SERVICE); // Export it so we can start it with a shell command from adb. setAndroidAttribute(service, SdkConstants.ATTR_EXPORTED, SdkConstants.VALUE_TRUE); application.getXml().appendChild(service); }
/** * Removes the android namespace from an element recursively. * * @param element the element */ private void clearNodeNamespaces(Element element) { String androidPrefix = XmlUtils.lookupNamespacePrefix(element, SdkConstants.ANDROID_URI); String name = element.getNodeName(); int colonIdx = name.indexOf(':'); if (colonIdx != -1) { String prefix = name.substring(0, colonIdx); if (prefix.equals(androidPrefix)) { String newName = name.substring(colonIdx + 1); getXml().renameNode(element, null, newName); } } NodeList childrenNodeList = element.getChildNodes(); for (int i = 0; i < childrenNodeList.getLength(); i++) { Node n = childrenNodeList.item(i); if (n instanceof Element) { clearNodeNamespaces((Element) n); } } } }
@NonNull private static XmlElement createOrGetElement( @NonNull ActionRecorder actionRecorder, @NonNull XmlDocument document, @NonNull ManifestModel.NodeTypes nodeType, @NonNull String message) { Element manifest = document.getXml().getDocumentElement(); NodeList nodes = manifest.getElementsByTagName(nodeType.toXmlName()); if (nodes.getLength() == 0) { nodes = manifest.getElementsByTagNameNS( SdkConstants.ANDROID_URI, nodeType.toXmlName()); } if (nodes.getLength() == 0) { // create it first. Element node = manifest.getOwnerDocument().createElement(nodeType.toXmlName()); manifest.appendChild(node); XmlElement xmlElement = new XmlElement(node, document); Actions.NodeRecord nodeRecord = new Actions.NodeRecord( Actions.ActionType.INJECTED, new SourceFilePosition(xmlElement.getSourceFile(), SourcePosition.UNKNOWN), xmlElement.getId(), message, NodeOperationType.STRICT); actionRecorder.recordNodeAction(xmlElement, nodeRecord); return xmlElement; } else { return new XmlElement((Element) nodes.item(0), document); } }
/** * Returns a pretty string representation of this document. */ @NonNull public String prettyPrint() { return XmlPrettyPrinter.prettyPrint( getXml(), XmlFormatPreferences.defaults(), XmlFormatStyle.get(getRootNode().getXml()), null, /* endOfLineSeparator */ false /* endWithNewLine */); }
return Optional.absent(); Element elementNS = getXml().createElement(nodeType.toXmlName());