XCFramework简介
XCFramework是XCode11中提出的一个新特性,是由XCode创建的一个可分发的二进制包,它包含了 framework 或 library 的一个或多个变体,因此可以在多个平台(iOS、macOS、tvOS、watchOS) 上使用,包括模拟器。XCFramework 可以是静态的,也可以是动态的。xcframework 的好处就是用 Xcode 发布的时候,Xcode 会自动选用正确的指令集 Frameworks,省去了手动移除动态库中的模拟器指令集的工作。
作用
- 包含模拟器和真机
- 包含多个平台:xcframework 支持所有的苹果平台,iOS,macOS,tvOS,watchOS,iPadOS,carPlayOS
- 可以包含其他第三方的库
- 可以不用设置复杂的头文件路径
- 可以在库里面设置权限
- 二进制的稳定性
创建包含iOS真机、模拟器和MacOS的XCFramework
为所需平台归档
iOS模拟器
xcodebuild -workspace "$WORKSPACE_NAME.xcworkspace"\
-scheme "$FRAMEWORK_NAME" -configuration "Release"\
-sdk "iphonesimulator"\
-arch i386 -arch x86_64 \
-derivedDataPath "build/derivedData"\
-archivePath "./build/simulator.xcarchive"\
SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
iOS真机
xcodebuild -workspace "$WORKSPACE_NAME.xcworkspace"\
-scheme "$FRAMEWORK_NAME"\
-arch armv7 -arch armv7s -arch arm64 \
-configuration "Release"\
-sdk "iphoneos"\
-derivedDataPath "build/derivedData"\
-archivePath "./build/ios.xcarchive"\
SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
MacOS
xcodebuild -workspace "$WORKSPACE_NAME.xcworkspace"\
-scheme "$FRAMEWORK_NAME"\
-configuration "Release"\
-destination 'platform=macOS,arch=x86_64,variant=Mac Catalyst'\
-sdk "$MAC_OS"\
-derivedDataPath "build/derivedData"\
-archivePath "./build/catalyst.xcarchive"\
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES SUPPORTS_MACCATALYST=YES SKIP_INSTALL=NO clean
由.a静态库创建Framework
iOS模拟器
# 创建simulator framework
PRODUCT_DIR=Products
BUILD_PRODUCTS_PATH="./build/derivedData/Build/Products"
SIMULATOR_FRAMEWORK_PATH="${BUILD_PRODUCTS_PATH}/Release-iphonesimulator/libBuglyOAUpgrade.a"
SIMULATOR_HEADERS="${BUILD_PRODUCTS_PATH}/Release-iphonesimulator/include/${FRAMEWORK_NAME}"
SIMULATOR_FRAMEWORK="${PRODUCT_DIR}/${FRAMEWORK_NAME}/simulator/${FRAMEWORK_NAME}.framework"
mkdir -p ${SIMULATOR_FRAMEWORK}/Headers
lipo -create ${SIMULATOR_FRAMEWORK_PATH} -output ${SIMULATOR_FRAMEWORK}/${FRAMEWORK_NAME}
cp -R ${SIMULATOR_HEADERS}/* ${SIMULATOR_FRAMEWORK}/Headers/
iOS真机
# 创建真机 framework
PRODUCT_DIR=Products
BUILD_PRODUCTS_PATH="./build/derivedData/Build/Products"
IOS_FRAMEWORK_PATH="${BUILD_PRODUCTS_PATH}/Release-iphoneos/libBuglyOAUpgrade.a"
IOS_HEEADERS="${BUILD_PRODUCTS_PATH}/Release-iphoneos/include/${FRAMEWORK_NAME}"
IPHONE_FRAMEWORK="${PRODUCT_DIR}/${FRAMEWORK_NAME}/iphoneos/${FRAMEWORK_NAME}.framework"
mkdir -p ${IPHONE_FRAMEWORK}/Headers
lipo -create ${IOS_FRAMEWORK_PATH} -output ${IPHONE_FRAMEWORK}/${FRAMEWORK_NAME}
cp -R ${IOS_HEEADERS}/* ${IPHONE_FRAMEWORK}/Headers/
MacOS
# 创建mac-catalyst framework
PRODUCT_DIR=Products
BUILD_PRODUCTS_PATH="./build/derivedData/Build/Products"
CATALYST_FRAMEWORK_PATH="${BUILD_PRODUCTS_PATH}/Release-maccatalyst/libBuglyOAUpgrade.a"
CATALYST_HEADERS="${BUILD_PRODUCTS_PATH}/Release-maccatalyst/include/${FRAMEWORK_NAME}"
CATALYST_FRAMEWORK="${PRODUCT_DIR}/${FRAMEWORK_NAME}/catalyst/${FRAMEWORK_NAME}.framework"
mkdir -p ${CATALYST_FRAMEWORK}/Headers
lipo -create ${CATALYST_FRAMEWORK_PATH} -output ${CATALYST_FRAMEWORK}/${FRAMEWORK_NAME}
cp -R ${CATALYST_HEADERS}/* ${CATALYST_FRAMEWORK}/Headers/
创建xcframework
xcodebuild -create-xcframework \
-framework ${SIMULATOR_FRAMEWORK} \
-framework ${IPHONE_FRAMEWORK} \
-framework ${CATALYST_FRAMEWORK} \
-output "./$FRAMEWORK_NAME.xcframework"
zip -r XC_FRAMEWORK.zip "./$FRAMEWORK_NAME.xcframework"
网友评论