package_version raises InvalidVersion when APK versionName is null or empty
Problem: Calling d.info (or any action that triggers the internal version check) throws packaging.version.InvalidVersion: Invalid version: ''
Root cause: In init.py, _package_version() returns packaging.version.parse(m.group('name') if m else ""). If the Android package manager returns an empty string ("") or the literal string "null" for the version, parse("") raises an exception.
Suggested fix (tested and working): Modify _package_version in uiautomator2/init.py line 735:
The fix i made:
def _package_version(self, package_name: str) -> Optional[packaging.version.Version]: # First check if the package is actually installed if self.shell(['pm', 'path', package_name]).exit_code != 0: return None
# Package exists – get its version info
dump_output = self.shell(['dumpsys', 'package', package_name]).output
m = re.compile(r'versionName=(?P<name>[\d.]+)').search(dump_output)
# If version is found, parse it; otherwise return a dummy version to avoid crashes
ver = m.group('name') if m else ""
return packaging.version.parse(ver) if ver else packaging.version.Version("0.0.0")my environment:
uiautomator2 version: 2.16.14
Python version: 3.11
Device: Samsung A05 ( runs Android R/sdk 30+)
Source: openatx/uiautomator2