欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

xcode9 Framework制作自动打包脚本自动build版本

程序员文章站 2022-04-12 21:42:19
...

前言

网上关于Framework制作的教程数不胜数,这里我只是自己制作framework的过程记录下来,使用的是Xcode9环境。


正文

1. 首先新建一个项目,选择Cocoa touch Framework

xcode9 Framework制作自动打包脚本自动build版本

2. 起名字创建工程,然后将要封装成framework的代码拖入工程

xcode9 Framework制作自动打包脚本自动build版本

3. 设置Build Settings

    (1). bitcode设置

        xcode9 Framework制作自动打包脚本自动build版本

    (2). Linking设置

        将Link With Standard Libraries关闭(用不到就关闭吧,否则也许会不兼容),Mach-O Type设为Static Library,framework可以是动态库也可以是静态库,设置为静态库可以直接在引入工程中使用要加other linker -ObjC -all_load, 对于动态库则需要在工程中导入Embedded Binaries否则会报Reason: image not found。

    xcode9 Framework制作自动打包脚本自动build版本

    动态库的使用注意:

    xcode9 Framework制作自动打包脚本自动build版本


4. 合并模拟器版本和真机版本

lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"

使用以上命令手工合并framework

下面我们使用脚本自动合并framework,步骤如下:

    (1). 创建新的aggregate,随便起名吧

    xcode9 Framework制作自动打包脚本自动build版本

    (2). 简单脚本配置

xcode9 Framework制作自动打包脚本自动build版本

# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
FMK_NAME=${PROJECT_NAME}
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WORK_DIR=build
DEVICE_DIR=${WORK_DIR}/${CONFIGURATION}-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WORK_DIR}/${CONFIGURATION}-iphonesimulator/${FMK_NAME}.framework
# -configuration ${CONFIGURATION} "Release"
# Clean and Building both architectures.
xcodebuild -configuration “Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
cp -R "${SIMULATOR_DIR}/" "${INSTALL_DIR}/"
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"
rm -r "${WORK_DIR}"
open "${INSTALL_DIR}"

(3). 带版本控制脚本配置

想要获得版本信息就要先设置info.plist文件,在build Settings中设置,当然是在你创建的aggregate中设置

xcode9 Framework制作自动打包脚本自动build版本

这时我们就可以通过PlistBuddy获得plist中的值,也可以设置。

# Sets the target folders and the final framework product.
# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNAME
# 例如: FMK_NAME = "MyFramework"
FMK_NAME=${PROJECT_NAME}

# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
#${SDK_NAME}
#SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.{3}$')
#/${DYLIB_CURRENT_VERSION}
#/${CURRENT_PROJECT_VERSION}

#版本获取
echo "${PROJECT_DIR}/${INFOPLIST_FILE}"
buildNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber="$buildNumber"
echo "buildNumber = $buildNumber"

shortVersion=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
shortVersion="$shortVersion"
echo "shortVersion = $shortVersion"

#自增build版本
buildVersion=$(echo ${buildNumber} | grep -o -E '\d{1,3}$')
buildVersion=$(($buildVersion + 1))
buildVersion=".$buildVersion"
echo "buildVersion = $buildVersion"

#设置build版本
newBuildNumber="$shortVersion$buildVersion"
echo "newBuildNumber = $newBuildNumber"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $newBuildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

INSTALL_DIR=${SRCROOT}/Products/${buildNumber}/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WORK_DIR=build
DEVICE_DIR=${WORK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WORK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
# -configuration ${CONFIGURATION} "Release"
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
cp -R "${SIMULATOR_DIR}/" "${INSTALL_DIR}/"
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"
rm -r "${WORK_DIR}"
rm -rf "${INSTALL_DIR}/_CodeSignature"
rm -rf "${INSTALL_DIR}/Info.plist"

open "${INSTALL_DIR}"

相关标签: framework