Xcode 12.3: Building for iOS Simulator, but the linked and embedded framework was built for iOS + iOS Simulator

111k views Asked by At

I have an app using a linked and embedded custom framework. The app built properly for iOS devices and simulators until Xcode 12.2. Starting from Xcode 12.3 however, I'm getting the following error:

Building for iOS Simulator, but the linked and embedded framework 'My.framework' was built for iOS + iOS Simulator.

The framework is built for both devices and simulators (as the error actually says) and merged using lipo, so it should be able to run everywhere without issues.

Am I missing something here? Is there a relevant change in Xcode 12.3?

4

There are 4 answers

7
Alex Shubin On BEST ANSWER

I'm afraid that this is actually the correct error and the framework shouldn't contain iOS and iOS Simulator code at the same time. Apple tries to force us to use XCFrameworks for this purpose. They started it in Xcode 11 and just tightened up the restrictions.

The only correct way to resolve this is to rebuild the framework as an XCFramework. Which is easy to do:

xcrun xcodebuild -create-xcframework \
    -framework /path/to/ios.framework \
    -framework /path/to/sim.framework \
    -output combined.xcframework

You can start with a combined .framework. Make two copies of the framework, and use lipo to remove the slices from the binary that are associated with a different SDK.

It is based on the original answer from Apple here.

My particular case is that I'm getting this error using Rome, which produces these frameworks (a possible solution is here). Also, a lot of struggling is going on on the Carthage side.

4
Paul Bancarel On

In addition to mistahenry's answer, you can handle this automatically in your project with this workaround.

  1. Set your Universal framework that does not work in Xcode 12.3 to Do not embed (in GeneralFrameworks, Libraries and Embedded Content)

  2. Add this "new run script phase" in "Build Phases":

    FRAMEWORK_APP_PATH="${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}"
    
    # 1. Copying FRAMEWORK to FRAMEWORK_APP_PATH
    find "$SRCROOT" -name '*.framework' -type d | while read -r FRAMEWORK
    do
    if [[ $FRAMEWORK == *"MY_WONDERFUL_UNIVERSAL_FRAMEWORK.framework" ]]
    then
        echo "Copying $FRAMEWORK into $FRAMEWORK_APP_PATH"
        cp -r $FRAMEWORK "$FRAMEWORK_APP_PATH"
    fi
    done
    # 2. Loops through the frameworks embedded in the application and removes unused architectures.
    find "$FRAMEWORK_APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
    do
    if [[ $FRAMEWORK == *"MY_WONDERFUL_UNIVERSAL_FRAMEWORK.framework" ]]
    then
    
        echo "Strip invalid archs on: $FRAMEWORK"
        FRAMEWORK_EXECUTABLE_NAME=$(/usr/libexec/PlistBuddy -c "Print CFBundleExecutable" "$FRAMEWORK/Info.plist")
        FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
        echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
        EXTRACTED_ARCHS=()
        for ARCH in $ARCHS
        do
        echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
        lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
        EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
        done
        echo "Merging extracted architectures: ${ARCHS}"
        lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
        rm "${EXTRACTED_ARCHS[@]}"
        echo "Replacing original executable with thinned version"
        rm "$FRAMEWORK_EXECUTABLE_PATH"
        mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
        codesign --force --sign ${EXPANDED_CODE_SIGN_IDENTITY} ${OTHER_CODE_SIGN_FLAGS:-} --preserve-metadata=identifier,entitlements $FRAMEWORK_EXECUTABLE_PATH
    else
        echo "Ignored strip on: $FRAMEWORK"
    fi
    done
    
  • Replace MY_WONDERFUL_UNIVERSAL_FRAMEWORK by the name of your framework and be sure that it is located at SRCROOT
7
mistahenry On

I have a framework with a universal binary that contains x86_64 and arm64 which I merge with lipo with a custom script at framework build time. I encountered this same issue for Xcode 12.3 and have created a work around for now. Hopefully this will get fixed in Xcode quickly, but until then, one quick fix would be to thin the architectures and use the framework that you need.

Please see my answer here on how to start producing .xcframeworks which is the long term solution for framework authors

For instance, let's assume I'm in a terminal in the working directory where my universal framework some_framework.framework is. If I want to run on an actual physical device, I execute the following command:

lipo -thin arm64 some_framework.framework/some_framework -output some_framework

With the above command, you extract the arm64 binary. Afterwards, replace the current some_framework.framework/some_framework with the newly generated arm64 only binary

mv some_framework some_framework.framework

If you have a universal framework built only from Objective-C sources, your job is done. But if you've got Swift code too, then you would need to update some_framework.framework/Modules/some_framework.swiftmodule so that it does not contain any references to architectures that are not arm64.

You would follow a similar process for running on the simulator except that you need x86_64. I'm currently now maintaining two versions of my framework until this is fixed. Whenever I switch between the simulator and the device, I simply switch out which framework is in my project.

8
Srikanth On

You have to exclude device architectures while building for simulator and while building for the device you have to exclude simulator's architectures.

To do that, navigate to Build Settings of your project -> Excluded Architectures -> Select the configuration(Debug/Release/Etc...) -> Tap + -> Any iOS Simulator SDK -> Add arm64, arm64e, armv7

Similarly, add x86_64, i386 to Any iOS SDK.

enter image description here

PS: You can check all the architectures which are present in your framework by running file <path_to_framework_binary> or lipo -info <path_to_framework_binary>.

Ex. file /Users/srikanth.kv/MyProject/MyLibrary.framework/MyLibrary