Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Last active December 16, 2020 19:44
Show Gist options
  • Save lalkrishna/74a5ab98938bf6ff882275dc137903d8 to your computer and use it in GitHub Desktop.
Save lalkrishna/74a5ab98938bf6ff882275dc137903d8 to your computer and use it in GitHub Desktop.
Creating Universal Framework with Bitcode support

Better method introduced by apple since Xcode 11 - xcframework

https://gist.github.com/lalkrishna/c1b33cba4dbacddbc1ecc9d619877c42


  • Project settings -> Select Framework Target -> Build settings add -fembed-bitcode in Other C Flags

  • click + Button -> Add User-Defined Setting Key: BITCODE_GENERATION_MODE value: bitcode

  • Set Build Active Architectures Only to Yes in Build settings.

Steps for Building Universal Framework:

  • Build framework for Simulator OS (Select simulator as target device) and iPhone OS(Any iOS Device (arm64)). Select Framework scheme -> Build for Profiling (Release)
  • Project Navigator -> Open 'Products' folder -> Right click show in Finder.
  • I Just copied Release-iphoneos and Release-iphonesimulator folders to desktop.
  • Use lipo to aggregate both these architectures (I just duplicated Release-iphoneos folder and renamed it to Release-Universal
lipo -create /Users/username/Desktop/Lib/Release-iphoneos/MyFramework.framework/MyFramework /Users/username/Desktop/Lib/Release-iphonesimulator/MyFramework.framework/MyFramework -output /Users/username/Desktop/Lib/Release-Universal/MyFramework.framework/MyFramework

  • User .framework file in Release-Universal folder.

In Client side Project:

  • Select Build Phases -> Click + Button -> New Run Script Phase Add below code.
# Type a script or drag a script file from your workspace to insert its path.
# skip if we run in debug
if [ "$CONFIGURATION" == "Debug" ]; then
echo "Skip frameworks cleaning in debug version"
exit 0
fi

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"

# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
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"

done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment