Obtaining app storage details via ADB


Question

TL;DR:



On-device, we can navigate to Settings › Apps, and for each app separately can call up details on storage used: by the app itself, by its data, by its cache. Is it possible to obtain those details from the command line, ideally via ADB – and without having root access?



In detail:



I've already searched the web, but found no information on this. I've already tried walking the package list (pm list packages), obtaining the path to the APK file (adb shell pm path $pkgname | awk -F':' '{ print $2 } '), and getting the file size of that (adb shell stat $path | grep Size) – but that has a series of disadvantages:




  • it only gives me the size of the APK (for updated system apps, only of the latest update)

  • even if scripted, it takes ~2s per app to obtain even that. As today's devices already ship with far more than 100 system apps, it would take "ages" even if only a few user apps were installed (almost 6 minutes on an almost virgin Wiko Sunny 3 where I just tried that)

  • details on data and cache are completely missing



Ideally, Android has already somewhere collected those details; it doesn't take that long via the GUI. Maybe there's some database one could query without having root privileges – or some XML to parse, or some shell command I missed?



PS: if you know about a root-only solution, that'd be "better than nothing". Please indicate that in a comment then so I can come back and ask you to make that an answer if there are no non-root solutions.


Answer

My first instinct to dig out such information which is often available via programming route is to go through dumps of various system services. One such service is diskstats whose only purpose is to dump the status of the disk. Looking into the java code of DiskStatsService.java for various Android releases, I noticed that the information you are looking for is available since Android Oreo. Among other things, the dump shows all the package names, their package sizes, their app data size, and their cache sizes. That information is also dumped into the file /data/system/diskstats_cache.json but this file cannot be accessed without root access though.



The dump is pretty ugly so I wrote a bash script to show data neatly. Run this script in your Linux.



Here's the script (and it does not require root access too):




#!/bin/bash

USRDIR="$HOME"
F_DISK_STATS="$USRDIR"/diskstats.txt
F_PKG_NAMES="$USRDIR"/package_names.txt
F_PKG_SIZE="$USRDIR"/app_pkg_sizes.txt
F_DAT_SIZE="$USRDIR"/app_data_sizes.txt
F_CACHE_SIZE="$USRDIR"/app_cache_sizes.txt

# ADVISE: do a check whether ADB is working fine or not
adb shell dumpsys diskstats > "$F_DISK_STATS"

# Separating data into relevant named files
sed -n '/Package Names:/p' "$F_DISK_STATS" | sed -e 's/,/\n/g' -e 's/"//g' -e 's/.*\[//g' -e 's/\].*//g' > "$F_PKG_NAMES"
sed -n '/App Sizes:/p' "$F_DISK_STATS" | sed -e 's/,/\n/g' -e 's/.*\[//g' -e 's/\].*//g' > "$F_PKG_SIZE"
sed -n '/App Data Sizes:/p' "$F_DISK_STATS" | sed -e 's/,/\n/g' -e 's/.*\[//g' -e 's/\].*//g' > "$F_DAT_SIZE"
sed -n '/Cache Sizes:/p' "$F_DISK_STATS" | sed -e 's/,/\n/g' -e 's/.*\[//g' -e 's/\].*//g' > "$F_CACHE_SIZE"

# Printing package names and their sizes
ttl_apps=$(wc -l "$F_PKG_NAMES" | cut -d ' ' -f1)
count=1
while [ $count -le $ttl_apps ]; do
pkg=$(sed -n "${ count } p" "$F_PKG_NAMES")
pkg_size=$(sed -n "${ count } p" "$F_PKG_SIZE")
dat_size=$(sed -n "${ count } p" "$F_DAT_SIZE")
csh_size=$(sed -n "${ count } p" "$F_CACHE_SIZE")
echo -e "Pkg Name: $pkg"
echo -e "\t Pkg_size=$pkg_size bytes"
echo -e "\t Data_size=$dat_size bytes"
echo -e "\t Cache_size=$csh_size bytes"
echo -e "\t Total_size=$(($pkg_size + $dat_size + $csh_size)) bytes\n"
count=$(( $count + 1));
done


Output when tested on OnePlus 6 (Android 9):




Pkg Name: org.xbmc.kodi
Pkg_size=144297984 bytes
Data_size=55771136 bytes
Cache_size=54341632 bytes
Total_size=254410752 bytes

Pkg Name: rikka.appops
Pkg_size=10330112 bytes
Data_size=1740800 bytes
Cache_size=163840 bytes
Total_size=12234752 bytes

Pkg Name: com.a0soft.gphone.aSpotCat
Pkg_size=16228352 bytes
Data_size=1101824 bytes
Cache_size=163840 bytes
Total_size=17494016 bytes

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes