Save time with XCFrameworks

Make your CI and your build times go faster.

Konstantinos Nikoloutsos
3 min readMar 5, 2024

Intro 📦

Have you ever thought how you can decrease the build time of your application? Have you ever wondered why you have to build something again and again instead of build it once and distribute it? I did have this thoughts in my head until recently that I learned about XCFrameworks.

XCFrameworks is a supported file by Xcode, hence the name, which allows distributing pre-compiled code. Developers can easily generate and use XCFrameworks in XCode by just dragging it inside the Framework and libraries in project target settings general tab.

Adding a xcframework to a test target.

💡 XCFramework is also a nice way to ship a closed source product to your clients. I remember in the past integrating the famous bitly URL shortener which did not want me to look at how their algorithm works.

It is worth remembering that the .xcframework essentially it’s a container of different .framework files for different platforms including simulators. It is usually depicted with a blue suitcase where the .framework is represented as a yellow one.

Illustration of how axcframework is composed of

Use case:

SwiftMacro has a dependency to swift-syntax which takes some time to build. By using xcframeworks this guy Šimon Java ended up making it 9x faster (see here). So are you still not convinced to use XCFrameworks on your project?

How to create your XCFrameworks:

Apple provides with a detailed guide on how you can create your own XCFramework. Personally I find it much easier to use a community tool which is called swift-create-xcframework.

⚠️ This tool will not work with xcode15 so please use this fork which resolves the issue at bsneed/xcode15_fix branch. After cloning install it by running make install.

Recently I created a XCFramework for swift-snapshot-testing by running the following command:

swift-create-xcframework /
--output xcframeworks-debug /
--platform ios /
--xc-setting ENABLE_TESTING_SEARCH_PATHS=YES

📙 The ENABLE_TESTING_SEARCH_PATHS is used because snapshot testing has a dependency to XCTest and without it the build fails.

The swift-create-xcframework essentially is a wrapper on top of xcodebuild that streamlines this creation process. For your interest, under the hood it calls xcodebuild archive .. .

Result of running the above command. As you can see 2 different folders are generated. One for simulator and one for physical device.

Then I dragged it into the test target Framework & Librariesand that’s it 🎉
No reason to build it again. Imagine how much time we saved from our CI and fellow programmers on our team.

:

Quick tips for those you made it too far 🎁 :

🎁 Did you know that SPM packages can ship binary code with XCFrameworks? See the WWDC20 video about it here .

🎁 Also did you know that it is recommended for xcframeworks to be signed to avoid an attacker injecting malicious code in your app? Learn more in WWDC23 video here.

If you liked this article don’t forget to leave a 👏 and share your opinion in the comments!

--

--