1234from pathlib import Path5import sys6import xml.etree.ElementTree as ET78ANDROID_NS = "{http://schemas.android.com/apk/res/android}"9ROOT = Path(__file__).resolve().parents[1]10INVENTORY = ROOT / "docs" / "android-release-manifest-inventory.txt"11MERGED = ROOT / "app" / "build" / "intermediates" / "merged_manifest" / "release" / "processReleaseMainManifest" / "AndroidManifest.xml"121314def inventory(path: Path) -> str:15 root = ET.parse(path).getroot()16 lines = []17 for permission in sorted(root.findall("uses-permission"), key=lambda node: node.get(ANDROID_NS + "name", "")):18 lines.append(f"permission|{permission.get(ANDROID_NS + 'name', '')}")19 application = root.find("application")20 if application is None:21 raise SystemExit("release manifest has no application")22 for child in sorted(application, key=lambda node: (node.tag, node.get(ANDROID_NS + "name", ""))):23 name = child.get(ANDROID_NS + "name", "")24 exported = child.get(ANDROID_NS + "exported", "")25 permission = child.get(ANDROID_NS + "permission", "")26 lines.append(f"component|{child.tag}|{name}|exported={exported}|permission={permission}")27 for intent_filter in child.findall("intent-filter"):28 actions = sorted(action.get(ANDROID_NS + "name", "") for action in intent_filter.findall("action"))29 categories = sorted(category.get(ANDROID_NS + "name", "") for category in intent_filter.findall("category"))30 lines.append(f"intent-filter|{child.tag}|{name}|actions={','.join(actions)}|categories={','.join(categories)}")31 return "\n".join(lines) + "\n"323334if len(sys.argv) != 2:35 raise SystemExit(f"usage: {Path(sys.argv[0]).name} check|write")36if sys.argv[1] == "write":37 INVENTORY.write_text(inventory(MERGED))38elif sys.argv[1] == "check":39 actual = inventory(MERGED)40 expected = INVENTORY.read_text()41 if actual != expected:42 raise SystemExit("merged release manifest differs from checked-in inventory")43else:44 raise SystemExit(f"unknown command: {sys.argv[1]}")