tags:

views:

228

answers:

1

I'm trying to figure out how to get the size of an installed app.
What's already failed:
- new File('/data/app/some.apk') - reports incorrect size
- PackageManager.getPackageSizeInfo(String packageName, IPackageStatsObserver observer) - is @hide and relies on some obscure IPackageStatsObserver for result so I can't call it via reflection.

+2  A: 

Unfortunately there is currently no official way to do that. However, you can call the PackageManager's hidden getPackageSize method if you import the PackageStats and IPackageStatsObserver AIDLs into our project and generate the stubs. You can then use reflection to invoke getPackageSize:

PackageManager pm = getPackageManager();

Method getPackageSizeInfo = pm.getClass().getMethod(
    "getPackageSizeInfo", String.class, IPackageStatsObserver.class);

getPackageSizeInfo.invoke(pm, "com.android.mms",
    new IPackageStatsObserver.Stub() {

        @Override
        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
            throws RemoteException {

            Log.i(TAG, "codeSize: " + pStats.codeSize);
        }
    });

That's obviously a big hack and should not be used for public applications.

Josef