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;
};
}