Meet Mergeable Libraries

Sanju Naik
4 min readJun 29, 2023

--

Apple announced mergeable libraries in WWDC-2023 to bring the best of both Static and Dynamic frameworks. In this article, we will explore what mergeable libraries mean and how we as developers can take advantage of them.

Before we delve into mergeable libraries, let’s have a quick recap of dynamic and static frameworks.

Dynamic Frameworks

Symbols from Dynamic frameworks are not included in your app binary during compilation. Instead, they are listed under the Frameworks folder inside your app bundle. The dynamic linker (dyld) loads these frameworks during app launch, which incurs a cost on App launch time.

Static Libraries

Static libraries, on the other hand, are statically built and linked into your app binary during compilation. This means they do not have any overhead during app launch or runtime but they do increase build time (link time).

The Dilemma:

Due to the cost associated with dynamic frameworks during app launch, the common recommendation has been to link most of the dependencies statically to ensure a quick app launch and provide a delightful user experience.

However, this approach shifts the cost to developers. When most dependencies are linked statically, it increases build time (link-time) and adds a constant link time even to incremental builds. This becomes a bottleneck in the build cycle, especially when there are a significant number of dependencies.

The Solution — Mergeable Libraries

Why not have both a delightful launch time for users and a faster linking time for developers? This is exactly what mergeable libraries offer — the ability to choose both options.

With mergeable libraries, your dependencies will behave as dynamic frameworks during development (Debug mode) and as static libraries/frameworks during release.

Now Let's understand how to enable this feature:

You need Xcode 15 Beta to enable this feature. There are 2 types of mergeable options: Automatic & manual. Automatic is quite straightforward and needs a few steps whereas manual needs some extra steps, will cover only the Automatic option in this blog post.

Automatic merging

To enable Automatic merging search for `MERGED_BINARY_TYPE` in your App target build settings and Set it to Automatic .

Once the above option is selected it would merge all of the direct dependencies which are dynamic frameworks into the App binary during release mode.

Direct dependencies are the ones listed under the Link Binary with Libraries` phase

IMP — Make sure to remove your framework from Embed Frameworks otherwise, it treats them as the normal dynamic frameworks.

Once you configure the above settings you are all set to use mergeable libraries.

Debug Mode

In Debug mode the dependencies are built as dynamic frameworks & are put in a new directory called ReexportedBinaries in the App bundle, this is how it looks

Release Mode

In Release mode, the dependencies are merged into the main App binary and produce one single App binary same as static linking, hence no cost on App launch time, this is how the bundle looks:

There is a slight overhead on App size in release mode because mergeable libraries contain extra metadata & that gets added to the app binary, good news is, this can be stripped using the linker option -no_exported_symbols . [I could not get this linker option working maybe because it's still in Beta].

Conclusion:

Mergeable libraries provide a solution to the trade-off between app launch time and build time. By leveraging the benefits of both static and dynamic frameworks, developers can optimize the user experience without sacrificing their own productivity.

By adopting mergeable libraries, developers can reduce build time bottlenecks and improve the efficiency of the development process. It’s an exciting advancement that opens up new possibilities for app development on Apple platforms.

Further Reading :

Sample Project on GitHub — https://github.com/sanju-naik/MergeableLib-Demo
WWDC Video — https://developer.apple.com/videos/play/wwdc2023/10268/
Apple Documentation — https://developer.apple.com/documentation/xcode/configuring-your-project-to-use-mergeable-libraries

--

--