From e3da419e5b0c004dc6376a522a6dfd2d1a1737a3 Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Tue, 5 Jan 2021 16:55:48 +0000 Subject: [PATCH] utils/android: Switch to using the lxml module Using dexdump from versions 30.0.1-30.0.3 of Android build tools does not produce valid XML caused by certain APKs Use the lxml module for parsing xml as it is more robust and better equipped to handle errors in input. --- devlib/utils/android.py | 8 ++++++-- setup.py | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/devlib/utils/android.py b/devlib/utils/android.py index 1f21fee..90be900 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -29,10 +29,11 @@ import sys import tempfile import time import uuid -import xml.etree.ElementTree import zipfile from collections import defaultdict +from io import StringIO +from lxml import etree try: from shlex import quote @@ -228,7 +229,10 @@ class ApkInfo(object): command = [dexdump, '-l', 'xml', extracted] dump = self._run(command) - xml_tree = xml.etree.ElementTree.fromstring(dump) + # Dexdump from build tools v30.0.X does not seem to produce + # valid xml from certain APKs so ignore errors and attempt to recover. + parser = etree.XMLParser(encoding='utf-8', recover=True) + xml_tree = etree.parse(StringIO(dump), parser) package = next((i for i in xml_tree.iter('package') if i.attrib['name'] == self.package), None) diff --git a/setup.py b/setup.py index 84bf3df..96262e1 100644 --- a/setup.py +++ b/setup.py @@ -92,6 +92,7 @@ params = dict( 'numpy; python_version>="3"', 'pandas<=0.24.2; python_version<"3"', 'pandas; python_version>"3"', + 'lxml', # More robust xml parsing ], extras_require={ 'daq': ['daqpower>=2'],