Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting installed app size

Tags:

android

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.

like image 918
yanchenko Avatar asked Nov 27 '09 00:11

yanchenko


People also ask

How do I find out the size of an app?

After you've released your app on a production track, here's where you can see your app's download and install sizes: Open Play Console and go to the App size page (Quality > Android vitals > App size). At the top right of the screen, you can filter the page data by App download size or App size on device.

Why is my APK size so big?

One of the simple ways to make your APK smaller is to reduce the number and size of the resources it contains. In particular, you can remove resources that your app no longer uses, and you can use scalable Drawable objects in place of image files.

What is the APK size?

Overview. Each time you upload an APK using the Google Play Console, you have the option to add one or two expansion files to the APK. Each file can be up to 2GB and it can be any format you choose, but we recommend you use a compressed file to conserve bandwidth during the download.


2 Answers

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.

  • Android Package Size
  • Using AIDL with Eclipse and ADT
  • APK Piracy: Using private code & resources in Android
like image 186
Josef Pfleger Avatar answered Oct 21 '22 16:10

Josef Pfleger


You can do it simplier by gettting path to apk file, and checking its lenght:

final PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = pm.getApplicationInfo(appInfo.getPackage(), 0);
File file = new File(applicationInfo.publicSourceDir);
int size = file.length();
like image 32
krawiec Avatar answered Oct 21 '22 16:10

krawiec