Android Package Size

April 8, 2010, 12:28 p.m.

Unfortunately there is currently no public api to retrieve size information about application packages. However, building on a technique I wrote about in the past, we can use the PackageManager's hidden getPackageSize method to retrieve an instance of PackageStats that includes cache, code and data size information.

First of all our application will need the GET_PACKAGE_SIZE permission:

<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>

Using ADT we can import the relevant AIDLs (PackageStats and IPackageStatsObserver) into our project and have ADT generate stubs. With the stubs generated, we can simply use reflection to call the hidden getPackageSize method and retrieve the PackageStats instance in the stub's onGetStatsCompleted callback method:

PackageManager pm = getPackageManager();

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

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

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

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

While this works it is obviously a big hack to work around a missing piece of Android API. Reaching past the SDK is never a good idea, especially for public applications.