|
| 1 | +package com.tencent.shadow.core.manifest_parser |
| 2 | + |
| 3 | +import org.w3c.dom.Document |
| 4 | +import org.w3c.dom.Element |
| 5 | +import org.w3c.dom.Node |
| 6 | +import java.io.File |
| 7 | +import javax.xml.parsers.DocumentBuilderFactory |
| 8 | + |
| 9 | +typealias ManifestMap = Map<String, Any> |
| 10 | + |
| 11 | +/** |
| 12 | + * 读取xml格式的Manifest到内存Map中 |
| 13 | + */ |
| 14 | +class AndroidManifestReader { |
| 15 | + /** |
| 16 | + * 读取入口方法 |
| 17 | + * |
| 18 | + * @param xmlFile com.android.build.gradle.tasks.ManifestProcessorTask任务的输出文件, |
| 19 | + * 一般位于apk工程的build/intermediates/merged_manifest目录中。 |
| 20 | + */ |
| 21 | + fun read(xmlFile: File): ManifestMap { |
| 22 | + val manifest = readXml(xmlFile).documentElement |
| 23 | + val application = readApplication(manifest) |
| 24 | + val globalAttributes = readGlobalAttributes(manifest, application) |
| 25 | + val components = readComponents(application) |
| 26 | + return globalAttributes.plus(components) |
| 27 | + } |
| 28 | + |
| 29 | + private fun readXml(xmlFile: File): Document { |
| 30 | + try { |
| 31 | + val documentBuilderFactory = DocumentBuilderFactory.newDefaultInstance() |
| 32 | + val documentBuilder = documentBuilderFactory.newDocumentBuilder() |
| 33 | + return documentBuilder.parse(xmlFile)!! |
| 34 | + } catch (e: Exception) { |
| 35 | + throw RuntimeException("xml应该是AGP生成的合法文件,所以不兼容任何xml读取错误", e) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private fun readApplication(manifest: Element): Element? { |
| 40 | + val elements = manifest.getElementsByTagName(AndroidManifestKeys.application) |
| 41 | + return if (elements.length == 1) { |
| 42 | + val node = elements.item(0) |
| 43 | + assert(node.nodeType == Node.ELEMENT_NODE) |
| 44 | + elements.item(0) as Element |
| 45 | + } else { |
| 46 | + null |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 读取Manifest中那些唯一的属性 |
| 52 | + */ |
| 53 | + private fun readGlobalAttributes(manifest: Element, application: Element?): Map<String, Any> { |
| 54 | + val globalAttributes = mutableMapOf<String, Any>() |
| 55 | + |
| 56 | + fun manifestAttribute(name: String) { |
| 57 | + globalAttributes[name] = manifest.getAttribute(name) |
| 58 | + } |
| 59 | + |
| 60 | + fun applicationAttribute(name: String) { |
| 61 | + if (application != null) { |
| 62 | + val attribute = application.getAttribute(name) |
| 63 | + if (attribute.isNotEmpty()) { |
| 64 | + globalAttributes[name] = attribute |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + manifestAttribute(AndroidManifestKeys.`package`) |
| 70 | + listOf( |
| 71 | + AndroidManifestKeys.name, |
| 72 | + AndroidManifestKeys.theme, |
| 73 | + AndroidManifestKeys.appComponentFactory, |
| 74 | + ).forEach(::applicationAttribute) |
| 75 | + return globalAttributes |
| 76 | + } |
| 77 | + |
| 78 | + private fun readComponents(application: Element?) = |
| 79 | + listOf( |
| 80 | + AndroidManifestKeys.activity to ::parseActivity, |
| 81 | + AndroidManifestKeys.service to ::parseService, |
| 82 | + AndroidManifestKeys.receiver to ::parseReceiver, |
| 83 | + AndroidManifestKeys.provider to ::parseProvider, |
| 84 | + ).map { (componentKey, parseMethod) -> |
| 85 | + val componentArray = parseComponents(application, componentKey, parseMethod) |
| 86 | + componentKey to componentArray |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + private fun parseComponents( |
| 91 | + application: Element?, |
| 92 | + componentKey: String, |
| 93 | + parseFunction: (Element) -> ComponentMap |
| 94 | + ): Array<ComponentMap> { |
| 95 | + if (application == null) { |
| 96 | + return emptyArray() |
| 97 | + } |
| 98 | + val nodeList = application.getElementsByTagName(componentKey) |
| 99 | + val length = nodeList.length |
| 100 | + val collectionList = mutableListOf<ComponentMap>() |
| 101 | + for (i in 0 until length) { |
| 102 | + val node = nodeList.item(i) |
| 103 | + assert(node.nodeType == Node.ELEMENT_NODE) |
| 104 | + val map = parseFunction(node as Element) |
| 105 | + collectionList.add(map) |
| 106 | + } |
| 107 | + return collectionList.toTypedArray() |
| 108 | + } |
| 109 | + |
| 110 | + private fun parseActivity(element: Element): ComponentMap { |
| 111 | + val activityMap = parseComponent(element).toMutableMap() |
| 112 | + |
| 113 | + listOf( |
| 114 | + AndroidManifestKeys.theme, |
| 115 | + AndroidManifestKeys.configChanges, |
| 116 | + AndroidManifestKeys.windowSoftInputMode, |
| 117 | + ).forEach { attributeKey -> |
| 118 | + activityMap.putAttributeIfNotNull(element, attributeKey) |
| 119 | + } |
| 120 | + return activityMap |
| 121 | + } |
| 122 | + |
| 123 | + private fun parseService(element: Element): ComponentMap { |
| 124 | + return parseComponent(element) |
| 125 | + } |
| 126 | + |
| 127 | + private fun parseReceiver(element: Element): ComponentMap { |
| 128 | + val receiverMap = parseComponent(element).toMutableMap() |
| 129 | + |
| 130 | + val receiverActions = parseReceiverActions(element) |
| 131 | + if (receiverActions.isNotEmpty()) { |
| 132 | + receiverMap[AndroidManifestKeys.action] = receiverActions |
| 133 | + } |
| 134 | + |
| 135 | + return receiverMap |
| 136 | + } |
| 137 | + |
| 138 | + private fun parseReceiverActions(receiverElement: Element): List<String> { |
| 139 | + val intentFilters = receiverElement.getElementsByTagName(AndroidManifestKeys.`intent-filter`) |
| 140 | + val collectionList = mutableListOf<String>() |
| 141 | + for (i in 0 until intentFilters.length) { |
| 142 | + val intentFilter = intentFilters.item(i) |
| 143 | + assert(intentFilter.nodeType == Node.ELEMENT_NODE) |
| 144 | + val actions = (intentFilter as Element).getElementsByTagName(AndroidManifestKeys.action) |
| 145 | + for (j in 0 until actions.length) { |
| 146 | + val action = actions.item(j) |
| 147 | + assert(action.nodeType == Node.ELEMENT_NODE) |
| 148 | + val actionName = (action as Element).getAttribute(AndroidManifestKeys.name) |
| 149 | + collectionList.add(actionName) |
| 150 | + } |
| 151 | + } |
| 152 | + return collectionList |
| 153 | + } |
| 154 | + |
| 155 | + private fun parseProvider(element: Element): ComponentMap { |
| 156 | + val providerMap = parseComponent(element).toMutableMap() |
| 157 | + |
| 158 | + providerMap.putAttributeIfNotNull(element, AndroidManifestKeys.authorities) |
| 159 | + |
| 160 | + return providerMap |
| 161 | + } |
| 162 | + |
| 163 | + private fun parseComponent(element: Element): ComponentMap { |
| 164 | + val componentName = element.getAttribute(AndroidManifestKeys.name) |
| 165 | + return mapOf( |
| 166 | + AndroidManifestKeys.name to componentName |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + private fun MutableComponentMap.putAttributeIfNotNull( |
| 171 | + componentElement: Element, |
| 172 | + attributeKey: String) { |
| 173 | + if (componentElement.hasAttribute(attributeKey)) { |
| 174 | + this[attributeKey] = componentElement.getAttribute(attributeKey) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments