yanchenko yanchenko - 5 months ago 35
Android Question

Getting installed app size

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.

Answer

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.