App size-reduction. Optimizing builds for lesser sizes
By: Manoj Aher
Tags: Xcode, iOS, Mobile, Mobile App Development, iOS App Development

Did you know that compared to the binary you uploaded and the final size of your app after it’s approved for the App Store may end up being slightly larger. This size increases when the App Store performs additional processing on your ipa by adding DRM to prevent app piracy & again recompressing the ipa.

Image by Pixabay

TL;DR

While building an Enterprise application we faced a problem of keeping the application size below then specified by the apple cellular download limit of 200 MB. This caused issues for our clients as most of the client download/update the app from their cellular network.

Keeping that in mind we worked on by keeping our app size below 100 MB. But as time went by the app went from nearly equal to 100 MB to 120 MB and now to 142 MB. We saw with each monthly release the app size increased by ~20–25MB. With this rate, we would be way above 200 MB in a few months.

Reduce app size, but HOW?

Here are some tips which may help you reduce your app size

1. Optimize your download and installation sizes

Before you start optimizing your app, you first need to measure its download and installation size.

Get your app_name.ipa file (i.e Adhoc distribution ipa)

file (i.e Adhoc distribution ipa) Rename the file app_name.ipa to app_name.zip

the file to Unzip the renamed file (terminal command “unzip PATH_TO_FOLDER/{name of your zipped file}.zip”)

the renamed file (terminal command “unzip PATH_TO_FOLDER/{name of your zipped file}.zip”) The extracted folder is Payload, open the folder and right-click on the app_name and select “Show Package Contents”

on the and select “Show Package Contents” The package contains all your resources (images, xib’s, storyboard’s, fonts, etc.)

Find out all the resources that you do not require and purge them

To find the unused resources I rely heavily on this great tool

Image by LSUnusedResources

2. Use asset catalogs and bitcode

Ensure that all images, pdf and the assets with relevant metadata to indicate which devices the asset is for (iPad, iPhone, Universal, etc..) which ensures that image is not downloaded for all device types.

Does your app support versions prior to iOS 10. If not, then you can probably stop adding 1x images to the asset catalog? All the devices after the iPhone 4 and iPad 2 use 2x images. Here is a link to verify if you need 1x resources

As per Apple documentation : Bitcode is an Apple technology that enables you to recompile your app to reduce its size. The recompilation happens when you upload your app to App Store Connect or export it for Ad Hoc, Development, or Enterprise distribution.

By enabling the bitcode you ensure that all the performance/optimization done in the LLVM framework will recompile your app without asking you to upload the app again which will reduce the size of your app in future LLVM updates.

3. Check Your Target’s Build Settings for Release Builds

Ensure that you specify the optimization level for the Release configuration is set to Fastest, Smallest [-Os] , which can make your compiled binary very small.

Learn more about setting the Release configuration here

4. On-Demand resources

Do you have content that can be loaded as required?

Download the resources (Video, Images, Game Content, etc..) as and when required. You can create asset packs and club all your resources that can be downloaded at later times. Asset packs are not included as a part of your app hence this does not increase the size of your app. These Asset pack can be downloaded as and when required.

We have Videos and PDF that are large and are downloaded as per personas as these files are not used by everyone. Check out this On-Demand Resources Guide to learn more about on-demand resources.

The list shows supported on-demand resource types and indicates whether those types are included in the target as a file or an asset catalog.

Image from On-Demand Resources Guide

5. Frameworks/Pods

If you unarchive the ipa mentioned in Point 1, you will see the included libraries have a good amount of size. I figured out some libraries were taking around 15 MB. And I was using only a part of the library that I would have easily implemented in 2 days.

I have used a lot of libraries including my favorite library alamofire for more than 5 projects. I could have easily created my own network library where the use cases included mostly GET calls. Similarly, I was using more libraries which only satisfied limited use cases but contributed a lot to size.

Do you have these kinds of libraries? Review the list and see which one is taking a lot of space with limited functionality that you use and can you build the same in lesser time?

6. App thinning

As per Apple documentation : App thinning is a technology that ensures that an app’s IPA file only contains resources and code that’s necessary to run the app on a particular device. This ensures that the app downloaded on devices will have only required resources.

The apps downloaded from App-Store are already thinned. So the assets that are not required for the user device will not be downloaded. That means my iPhone X will download only 3x images, which by default speed ups my download and reduce the app size

7. Reducing the Size of App Updates

Does my app download the whole app at every update?

No, instead of downloading the whole app, the App Store maintains an update package. It creates an optimized package that contains only the content that has changed between multiple versions of your app. Avoiding any unnecessary modifications to files, storyboards, nibs, and images can help you to a great extent to reduce the size of your app’s update package.

HEY! But iOS 13 allows downloading on the cellular data. Why should I care about the app size?

With iOS 13 you can download the app above 200 MB.