Skip to content

Instantly share code, notes, and snippets.

@Terrance
Created March 10, 2024 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Terrance/75c56f80dcf98e38e02e010e59e574b1 to your computer and use it in GitHub Desktop.
Save Terrance/75c56f80dcf98e38e02e010e59e574b1 to your computer and use it in GitHub Desktop.
Script to list Android apps installed, grouped by installer, shown per-user (e.g. work profile). Intended to be ran via Termux.
#!/usr/bin/env python3
from collections import defaultdict
import subprocess
def main():
dumpsys = subprocess.run(("/system/bin/dumpsys", "package", "packages"), capture_output=True)
owners = defaultdict(set)
installs = defaultdict(dict)
debugs = {}
for line in dumpsys.stdout.decode().splitlines():
if line.startswith(" Package ["):
package = line.split("[")[1].split("]")[0]
elif line.startswith(" installerPackageName="):
owner = line.split("=")[1]
owners[owner].add(package)
elif line.startswith(" flags="):
flags = line.split("[", 1)[1].rsplit("]", 1)[0].split()
debugs[package] = "DEBUGGABLE" in flags
elif line.startswith(" User "):
uid, meta = line[9:].split(": ", 1)
flags = dict(flag.split("=") for flag in meta.split())
if flags["installed"] == "true":
installs[package][uid] = flags["enabled"] in ("0", "1")
for owner, packages in sorted(owners.items()):
print("{}:".format(owner))
for package in sorted(packages):
install = installs[package]
state = ", ".join("{}{}".format(uid, "" if enabled else " disabled") for uid, enabled in install.items())
print(" {} ({}{})".format(package, "debug, " if debugs.get(package) else "", state))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment