Creating a Universal Static Library in Xcode for Device and Simulator

Creating a Universal Static Library in Xcode for Device and Simulator

28 February 2025 Stephan Petzl Leave a comment Xcode

Building a universal static library that includes both the iOS device and simulator architectures can be a challenging task. Apple’s Xcode does not provide a straightforward method to accomplish this, and the documentation on this topic is sparse. This guide aims to simplify the process by offering a step-by-step method to create a universal static library using a script within Xcode.

Solution Overview

The most effective solution involves using a script that automatically builds the necessary binaries for both the device and simulator, and then merges them into a single universal library. This method ensures compatibility across different iOS versions and architectures.

Steps to Create a Universal Static Library

  1. Create a new static library project in Xcode.
  2. Select your target in the project navigator.
  3. In the “Build Settings” tab, set “Build Active Architecture Only” to “NO” for all configurations.
  4. Navigate to the “Build Phases” tab and add a new “Run Script Build Phase”.
  5. Copy and paste the following script into the script box:

  #!/bin/bash
  set -e
  set -o pipefail

  SDK_VERSION=$(echo ${SDK_NAME} | grep -o '\\d\\{1,2\\}\\.\\d\\{1,2\\}')
  if [ ${PLATFORM_NAME} = "iphonesimulator" ]
  then
    OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
  else
    OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
  fi

  if [ "true" == ${ALREADYINVOKED:-false} ]
  then
    echo "RECURSION: I am NOT the root invocation, so I'm NOT going to recurse"
  else
    export ALREADYINVOKED="true"
    xcodebuild -configuration "${CONFIGURATION}" -project "${PROJECT_NAME}.xcodeproj" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO
    CURRENTCONFIG_DEVICE_DIR=${SYMROOT}/${CONFIGURATION}-iphoneos
    CURRENTCONFIG_SIMULATOR_DIR=${SYMROOT}/${CONFIGURATION}-iphonesimulator
    CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
    rm -rf "${CREATING_UNIVERSAL_DIR}"
    mkdir "${CREATING_UNIVERSAL_DIR}"
    xcrun -sdk iphoneos lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
  fi
  

Installation Instructions

  • Ensure your Xcode project is set up correctly with “Build Active Architecture Only” turned off.
  • Add headers in the “Copy Headers” phase if necessary.
  • Run the build and locate your universal library in the specified output directory.

Enhancements with Repeato

While the script method provides a robust solution for creating universal static libraries, managing and testing these libraries can be further optimized using Repeato. As a no-code test automation tool, Repeato allows you to automate the testing process for iOS applications efficiently. It supports data-driven and keyword-driven testing, which can significantly streamline your testing workflow.

Repeato’s ability to execute command line scripts and integrate with various testing frameworks makes it a versatile choice for developers looking to enhance their test automation capabilities. With Repeato, you can ensure that your universal libraries function correctly across different devices and simulators, thus reducing the time and effort required for manual testing.

For more information on how Repeato can assist in your testing endeavors, visit our homepage or check out our documentation for detailed guides and resources.

Like this article? there’s more where that came from!