Xcode8 下 Framework 打包(二)自动打包脚本创建过程

1.新建打包脚本target

Targets 选择+ --> Cross-platform 选择Aggregate -->Next


Xcode8 下 Framework 打包(二)自动打包脚本创建过程_第1张图片
图例1.png

2.target设置

1.添加target依赖

Target Dependencies 选中需要打包的framework + 选择New Run Script Phase 出现 Run Scirpt

2.设置脚本路径

可以在命令行里设置
也可以直接将脚本粘贴在这里

Xcode8 下 Framework 打包(二)自动打包脚本创建过程_第2张图片
图例2.png

这里有一个问题,暂时没搞清楚,这个脚本路径使用相对路径就会在编译的时候提示找不到脚本.

这里使用的脚本

 
#!/bin/sh
 
#  ios-build-framework-script.sh
set -e
set +u
### Avoid recursively calling this script.
if [[ $UF_MASTER_SCRIPT_RUNNING ]]
then
exit 0
fi
set -u
export UF_MASTER_SCRIPT_RUNNING=1
### Constants.
UF_TARGET_NAME=${PROJECT_NAME}
FRAMEWORK_VERSION="1.0"
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
IPHONE_DEVICE_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphoneos
IPHONE_SIMULATOR_BUILD_DIR=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator
FRAMEWORK_TARGET=xxxx

### Functions
## List files in the specified directory, storing to the specified array.
#
# @param $1 The path to list
# @param $2 The name of the array to fill
#
##
list_files ()
{
filelist=$(ls "$1")
while read line
do
eval "$2[\${#$2[*]}]=\"\$line\""
done <<< "$filelist"
}
### Take build target.
if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]
then
SF_SDK_PLATFORM=${BASH_REMATCH[1]} # "iphoneos" or "iphonesimulator".
else
echo "Could not find platform name from SDK_NAME: $SDK_NAME"
exit 1
fi
### Build simulator platform. (i386, x86_64)
echo "========== Build Simulator Platform =========="
echo "===== Build Simulator Platform: i386 ====="
xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" CONFIGURATION_BUILD_DIR="${IPHONE_SIMULATOR_BUILD_DIR}/i386" SYMROOT="${SYMROOT}" ARCHS='i386' VALID_ARCHS='i386' $ACTION
echo "===== Build Simulator Platform: x86_64 ====="
xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" CONFIGURATION_BUILD_DIR="${IPHONE_SIMULATOR_BUILD_DIR}/x86_64" SYMROOT="${SYMROOT}" ARCHS='x86_64' VALID_ARCHS='x86_64' $ACTION
### Build device platform. (armv7, arm64)
echo "========== Build Device Platform =========="
echo "===== Build Device Platform: armv7 ====="
xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphoneos BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}"  CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/armv7" SYMROOT="${SYMROOT}" ARCHS='armv7 armv7s' VALID_ARCHS='armv7 armv7s' $ACTION
echo "===== Build Device Platform: arm64 ====="
xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -configuration "${CONFIGURATION}" -sdk iphoneos BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" CONFIGURATION_BUILD_DIR="${IPHONE_DEVICE_BUILD_DIR}/arm64" SYMROOT="${SYMROOT}" ARCHS='arm64' VALID_ARCHS='arm64' $ACTION
### Build device platform. (arm64, armv7)
echo "========== Build Universal Platform =========="
## Copy the framework structure to the universal folder (clean it first).
rm -rf "${UNIVERSAL_OUTPUTFOLDER}"
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
## Copy the last product files of xcodebuild command.
cp -R "${IPHONE_DEVICE_BUILD_DIR}/arm64/${FRAMEWORK_TARGET}.framework" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework"
### Smash them together to combine all architectures.
lipo -create  "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/i386/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/x86_64/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/armv7/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/arm64/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}" -output "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}"
### Create standard structure for framework.
#
# If we don't have "Info.plist -> Versions/Current/Resources/Info.plist", we may get error when use this framework.
#
# MyFramework.framework
# |-- MyFramework -> Versions/Current/MyFramework
# |-- Headers -> Versions/Current/Headers
# |-- Resources -> Versions/Current/Resources
# |-- Info.plist -> Versions/Current/Resources/Info.plist
# `-- Versions
#     |-- A
#     |   |-- MyFramework
#     |   |-- Headers
#     |   |   `-- MyFramework.h
#     |   `-- Resources
#     |       |-- Info.plist
#     |       |-- MyViewController.nib
#     |       `-- en.lproj
#     |           `-- InfoPlist.strings
#     `-- Current -> A
#
echo "========== Create Standard Structure =========="
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Versions/${FRAMEWORK_VERSION}/"
mv "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Versions/${FRAMEWORK_VERSION}/"
mv "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Headers" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Versions/${FRAMEWORK_VERSION}/"
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Resources"
declare -a UF_FILE_LIST
list_files "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/" UF_FILE_LIST
for file_name in "${UF_FILE_LIST[@]}"
do
if [[ "${file_name}" == "Info.plist" ]] || [[ "${file_name}" =~ .*\.lproj$ ]]
then
mv "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/${file_name}" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Resources/"
fi
done
mv "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Resources" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Versions/${FRAMEWORK_VERSION}/"
ln -sfh "Versions/Current/Resources/Info.plist" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Info.plist"
ln -sfh "${FRAMEWORK_VERSION}" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Versions/Current"
ln -sfh "Versions/Current/${FRAMEWORK_TARGET}" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/${FRAMEWORK_TARGET}"
ln -sfh "Versions/Current/Headers" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Headers"
ln -sfh "Versions/Current/Resources" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_TARGET}.framework/Resources"
### Open the universal folder.
open "${UNIVERSAL_OUTPUTFOLDER}"

脚本中只有一个地方需要单独设置 FRAMEWORK_TARGET 为需要打包Framework 的名称

Xcode8 下 Framework 打包(二)自动打包脚本创建过程_第3张图片
图例3.png

3.编译

Scheme 中选中脚本Target,设备选中 generic iOS Device (待验证)。 编译成功之后会编译目录会自动打开,根据当前的模式,生成的相应的Debug/Release-universal 目录下的 xxx.framework 即为最终的Framework 支持多种运行环境

lipo -info xxx 
Architectures in the fat file: xxx are: i386 armv7 armv7s x86_64 arm64 

完毕

你可能感兴趣的:(Xcode8 下 Framework 打包(二)自动打包脚本创建过程)