Skip to content
DeveloperMemos

Getting the Size of an APK File Programmatically

APK, Android, Programming1 min read

In Android development, understanding the size of your APK (Android Application Package) file can be crucial for various reasons. Whether you are optimizing app performance, analyzing storage requirements, or simply providing users with information about the space your app will occupy on their devices, obtaining the APK file size programmatically is a valuable skill. In this article, we'll dive into methods for extracting the size of an APK file using Android programming.

Obtaining APK File Size

The size of an APK file is essential information that can be fetched programmatically. This can be especially useful when you want to display the size to users before they download or install an application. You can also use this information for internal logging or analytics purposes within your app.

Using PackageManager to Retrieve APK Size

One way to programmatically get the size of an APK file is by utilizing the PackageManager class in Android. The PackageManager class provides information about all the installed packages on a device, including your own application's package. By accessing the ApplicationInfo of your package, you can determine the source dir which points to the path of your APK file, and then retrieve the size using standard Java file manipulation techniques.

1try {
2 val packageInfo = packageManager.getPackageInfo(packageName, 0)
3 val appInfo = packageInfo.applicationInfo
4 val apkPath = appInfo.sourceDir
5 val apkFile = File(apkPath)
6 val apkSize = apkFile.length()
7 // Do something with apkSize
8} catch (e: PackageManager.NameNotFoundException) {
9 e.printStackTrace()
10}

By following this approach, you can access the size of your APK file programmatically within your Android application.

Displaying the Size

Once you have obtained the size of the APK file, you might want to present it to the user or make use of it within your application. For instance, you can display the size in a settings screen, provide it as part of the app description, or even use it to dynamically adjust how your app functions based on available device storage.


Understanding how to programmatically obtain the size of an APK file is a valuable skill for Android developers. Whether it's for informing users, managing app resources more effectively, or other purposes, being able to access this information within your app can be highly beneficial. By leveraging the PackageManager class and file manipulation techniques, you can seamlessly integrate APK file size retrieval into your Android applications.