Skip to content

Instantly share code, notes, and snippets.

@moyhdezzamawl
Created May 4, 2021 21:15
Show Gist options
  • Save moyhdezzamawl/1dc22aba49decf54288d2bc33a977747 to your computer and use it in GitHub Desktop.
Save moyhdezzamawl/1dc22aba49decf54288d2bc33a977747 to your computer and use it in GitHub Desktop.

iOS App Size Report and Size Analysis

When you start a new project, the App size it's not really a concern, but when it starts growing and you start adding new features, it can become chaotic. Most large companies face the issue of hitting the app size limit.

This means that you can't download the app using mobile data, which becomes a big problem because the user cannot download the app when they need it, and maybe they will forget to download it later or find another option.

Even though Apple removed the app size limit in iOS 13, whenever you try to download a large app, you get a warning, and the user now has to consider if it’s worth having an app that is taking a big chunk of their precious device storage.

Also, applications with smaller sizes have the biggest user retention rate.

Now, how do you keep the app size small? First, you need to set a starting point, a Size Report that measures the app download and installation sizes. As Apple tells us here, none of the binaries you create for debugging or uploading to the App Store from within Xcode are suitable for measuring our app size.

For example, you cannot use any of the following binaries:

  • The APP file (the app bundle).
  • The XCARCHIVE bundle that you create when you archive our app.
  • The IPA file that you upload to App Store Connect.

Those binaries contain resources and files that are not part of the bundles that the users download from the App Store, for example, DSYM files for crash reporting.

Creating an App Size Report

The most accurate way of knowing the final app size is through App Store Connect in the App metadata section. That’s because when you upload the binary to App Store Connect, Apple performs additional processing on your app’s binaries, adding DRM to prevent app piracy and then recompressing the binaries.

Xcode also has built-in reporting tools that can create App Size Reports for you. These reports can't tell you the final size of the app because of the additional processing to your app's binaries, but it's the closest estimate that you can get.

To create the App Size Report using Xcode:

  1. Archive the app.
  2. Export the archived app using an Ad Hoc, Development, or Enterprise certificate.
  3. In the sheet for setting the development distribution options, choose “All compatible device variants” for app thinning, and enable Rebuild from Bitcode.
  4. Sign your app and export it to your Mac.

To create the App Size Report using the Xcode command line tools:

  1. Archive the app running the following command: https://gist.github.com/d205e233e18dc46ac7371fdc487ce9ba

  2. Create an exportOptionsPlist that looks similar to this: https://gist.github.com/4c2a8950d9d24b3364e5e7a728604986

  3. Export the archived app. https://gist.github.com/e8a3a18365c3de1ab50f61413980afba

NOTE: These commands have plenty of options, and you should adapt them to your needs.

Both methods create a folder with a universal IPA file and Thinned IPA files for each variant of our app.

The folder also contains an App Size Report file named App Thinning Size Report.txt this report lists the compressed (download) and uncompressed (install) sizes for each of our IPA's files.

Creating an App Size Analysis

Now that you have a starting point, you need to know what can be improved to reduce the app's size. Here's where a size analysis is necessary.

You need to know what makes your app so big, remove unnecessary files, and identify our largest dependencies.

For that, you need the IPA file that you created previously and follow these steps:

  1. Change the extension of the IPA file to ZIP.

NOTE: An IPA file is just a ZIP archive that has a particular structure when unzipped.

  1. Unzip the file to show the app’s bundle inside the Payloads directory. You can either open the ZIP file in Finder or run unzip -lv /path/to/your/app.zip in the command line.

  2. Right-click the app's bundle, and choose Show Package Contents.

  3. Look throughout all the files and ensure that you are not adding unwanted files to your target, such as your app's README.md file, and remove any unused files like images or assets from the target/project.

Easy, right? But what if you want to know how large a dependency is? Or maybe how much of the app size the UI is taking?.

To do this, provide the swift executable with the IPA to automate the analysis process with the output of the unzip command instead of the contents of the IPA. The output looks similar to this (but a lot bigger):

First, you create a swift executable:

https://gist.github.com/e7c51b8d9d5cb2714fe42b1a073bcfab

Now on Sources/SizeAnalysis, you create another directory named Models in which you will add three files:

FileCategory.swift This will help us to categorize the type of each file.

FileData.swift Here you will save the data of each file.

SizeAnalysisData.swift Here you will save all the output data.

These models will help us to parse the unzip output into manageable models.

Now you need to create the parser. For that, you have to create a new file named SizeAnalysis.swift. Here you will change the file extension from .ipa to .zip. Then you will unzip it and save the output on another file. You will read this file line by line, populating our models. Finally, you just create a function that returns a string containing the size analysis pretty printed.

Here are the contents of the files that you created previously:

<script src="https://gist.github.com/moyhdezzamawl/e5cae644a8ec096b72e7a9dc5655f47b.js"></script>

Just provide the path of the ipa as an argument and run the project, and you should get an output that looks like this:

https://gist.github.com/52c4a24925a94cdcbefc1fe376c82c71

This example is a generic size analysis project, you can modify it to your needs. You can also save the information somewhere to compare different versions and know where those extra MB come from on every update.

Here you can find the source code.

Reducing Your App's Size

Doing Basic Optimization to Reduce Your App's Size

Doing Advanced Optimization to Further Reduce Your App's Size

The Ultimate Guide To Swift Executables 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment