Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PackageStats gives negative Values

I am using PackageStats and on printing the PackageStats's codeSize, cacheSize, dataSize, some data is coming as negative.

A few questions:

  • How is this possible?
  • What are the scenarios when codeSize could be negative considering the apk size is around 50MB?
  • Any other ways which can be reliably used to extract above information?

Also, for Android N it gives me "NoSuchMethodException".So,

  • Is it removed for Android N or is there some way to use it?
  • Any alternatives that could help me calculate the above sizes?

Code:

PackageManager packageManager = context.getPackageManager();
Method myUserId = UserHandle.class.getDeclaredMethod("myUserId");
int userID = (Integer) myUserId.invoke(packageManager);

Method getPackageSizeInfo = packageManager.getClass().getDeclaredMethod("getPackageSizeInfo", String.class, int.class,
IPackageStatsObserver.class);
getPackageSizeInfo.invoke(packageManager, context.getPackageName(), userID, new IPackageStatsObserver.Stub() {

    @Override
    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
        long codeSize = pStats.codeSize / MB; //long MB = 1024*1024;
        long cacheSize = pStats.cacheSize / MB;
        long dataSize = pStats.dataSize / MB;
        long appSize = codeSize + cacheSize + dataSize;
    };
}
like image 507
thepace Avatar asked Aug 23 '16 07:08

thepace


1 Answers

just tried on API 23/24 and can just instance it.

these external* properties might refer to the SD card.

    PackageStats stats = new PackageStats(context.getPackageName());
    long codeSize  = stats.codeSize + stats.externalCodeSize;
    long dataSize  = stats.dataSize + stats.externalDataSize;
    long cacheSize = stats.cacheSize + stats.externalCacheSize;
    long appSize   = codeSize + dataSize + cacheSize;
like image 191
Martin Zeitler Avatar answered Nov 17 '22 19:11

Martin Zeitler