Framework开发看我就够了第二回:静态库、动态库,脚本生成

上回讲到framework的创建和使用,这回我们来讲讲静态库和动态库。各位看官往下看。

1. 什么是静态库和动态库?

静态库:链接时完整地拷贝至执行文件中,被多次使用就有多份冗余拷贝。

动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存。

我们一开始创建的默认framework就是动态的,我们可以在Build settings->mach-O Type里修改static library来改成静态库。


截屏2020-05-25 下午2.22.39.png
截屏2020-05-25 下午2.24.07.png

我们改成static library后运行,然后从products里把framework拷贝到之前那个sdk文件夹,因为是静态库,demo里配置改成Do Not Embed,运行demo我们可以看到也能成功打印。


截屏2020-05-25 下午2.33.48.png
2. 脚本编写

但是每次从products去拷贝framework有点麻烦,有没有办法可以自动生成framework到我们项目指定的文件下底下呢?有,xcode里我们可以编写脚本来实现。

1.首先我们先新建一个target。


截屏2020-05-25 下午3.36.05.png
截屏2020-05-25 下午3.38.30.png

2.然后添加一个run script phase


截屏2020-05-25 下午3.39.29.png

3.添加依赖


截屏2020-05-25 下午3.49.54.png

4.脚本编写,我们先编写个静态库的脚本


截屏2020-05-25 下午3.54.16.png

上一下代码

# Sets the target folders and the final framework product.
FMK_NAME=TestFramework

# 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}/../sdk/framework/${FMK_NAME}.framework

# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework

SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework

# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphoneos clean

xcodebuild -configuration "Release" -target "TestFramework" -sdk iphonesimulator clean

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphoneos build

xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "Release" -target "TestFramework" -sdk iphonesimulator build

# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi

mkdir -p "${INSTALL_DIR}"

cp -R "${DEVICE_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 "${WRK_DIR}"

5.最后,我们运行脚本target,可以看到在项目sdk文件夹下生成了个framework


截屏2020-05-25 下午3.57.26.png

6.动态库和静态库前面步骤都一样,除了脚本不一样,就直接上脚本代码

# Sets the target folders and the final framework product.
FMK_NAME=TestFramework

# 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}/../sdk/${FMK_NAME}.framework

# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework

# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -configuration "Release" -target "TestFramework" -sdk iphoneos 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}/"

# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"

rm -r "${WRK_DIR}"

请不要吝啬你们的小❤️❤️

你可能感兴趣的:(Framework开发看我就够了第二回:静态库、动态库,脚本生成)