Background
Before Android O, in order to get an app size, you had to have a special permission for it, and use a hidden API.
The problem
Such a solution won't work anymore, but instead, Google provides an official API that's less granular: queryStatsForPackage , which returns StorageStats object.
However, I can't find any information of how to use it.
As opposed to the hidden API, which only required the package name of the app, this one also requires "String volumeUuid" and "UserHandle user".
The questions
- How do I provide those parameters?
- What do they mean? Is each of the volumeUuid refer to a different storage (build in storage and real sd-card, for example), and UserHandle is for each user?
- If #2 is correct, should I really call each of them for each possible value, to get the real total storage the app takes? If so, how?
Pass null as the VolumeUuid, to get values for the default internal storage, or get specific volumes via the StorageManager system service.
You can get your own UserHandle via Process.myUserHandle(). Other users of the system occur when you give someone a guest login on your device, or when there is an Android for Work profile installed on the device. I don't think there is a simple way to enumerate all the users that have access to the device.
OK, I've found out how to use the new API. I've prepared a small sample of this, fetching some information about the volume storage and of a specific app (this time of the Play Store, but you can change it if needed) :
manifest file should have this:
An alternative approach for getting UUID.
To get UUIDs, you can also use StorageManager.getUuidForPath(File path) function. As to the path, use Context.getFilesDir() and Context.getExternalFilesDirs() to get all possibles directories that an app data will be saved to.
Please note that different path may return the same UUID, so you have to use a hash set to collect unique UUIDs