在APP项目中使用的静态库有两种,一是.a静态库,另一种是framework静态库。下面分布介绍这2中静态库的创建过程,以及通过脚本工具做自动化打包的2种方式。
.a静态库生成
.a文件
1.创建一个Static Libaray工程
2.将项目文件和资源文件(切图,xib等)导入到SDK制作工程中
3.在Build Phases项目下,添加New Header Phases, 在Header阶段内添加静态库里的.h头文件,把需要暴露外面的放到public下面(包括:SDK的集成h文件和包含的内部h文件)。
bundle文件
4.静态库打包bundle文件,这里把静态库中需要的png,xib打包进去。在Targets下点击“+”,增加新的bundle target。从macOS下现在Bundle工程。因为iOS下没有这个项目
5.选中bundle target项目下的Build Phases Tab项 ,然后选择Copy Bundle Resources, 把SDK项目里的xib, png放到其中。
6.选中bundle target项目下的Build Settings Tab项, 
设置Base SDK 为 iOS, 
设置COMBINE_HIDPI_IMAGES为NO,否则Bundle中的图片会变成tiff格式,
设置Skip install为YES,
删除Installation Directory对应的值

7.运行xcode项目,生成产物bundle文件和.a文件, 默认在目录~/Library/Developer/Xcode/DerivedData下

8.从中复制出libStaticLib.a文件,StaticLibResource.bundle,和所有暴露的.h头文件导入到项目中使用(在移动到APP项目时,.a和.bundle文件会自动添加到General->Frameworks,Libraries,and Embedded Content)。
9.合并静态库真机和模拟器文件
lipo -create XXX/模拟器.a路径 XXX/真机.a路径 -output 合并后的文件名称.a
lipo -create \
~/Library/Developer/Xcode/DerivedData/StaticLib-hhthxcjblbfkvzcguwyduhlkkvei/Build/Products/Debug-iphoneos/libStaticLib.a \
~/Library/Developer/Xcode/DerivedData/StaticLib-hhthxcjblbfkvzcguwyduhlkkvei/Build/Products/Debug-iphonesimulator/libStaticLib.a \
-output ~/Desktop/app/libStaticLib.a

10.APP中对资源文件的读取要指定对应的bundle,写名bundle的名称。

 
framework静态库生成
如果APP项目和SDK项目都使用了pod第三方库,那么podfile文件设置如下:
# App项目的Podfile文件
target 'StaticFramework' do
# Comment the next line if you don't want to use dynamic frameworks
    use_frameworks!
    # Pods for StaticFramework
    pod 'FMDB'
end


# SDK项目的Podfile文件
target 'StaticFramework' do
# Comment the next line if you don't want to use dynamic frameworks
    use_frameworks!
    # Pods for StaticFramework
    pod 'FMDB'
end

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end
1.创建一个Static Framework工程
2.将项目文件和资源文件(切图,xib等)导入到Framework制作工程中
3.Build Phases阶段 修改
移动Header阶段内添加静态库里的.h头文件,把需要暴露外面的放到public下面(包括:SDK的集成h文件和包含的内部h文件)。
检查Copy Bundle Resources 阶段,查看png,xib等资源文件是否在里面,
查看Compile Sources中编译.m文件是否都在里面,
查看Link Binary With Libaray下需要链接的其他静态库库是否都在里面。
4.Build Settings 修改, 搜索linking进行链接过程设置
Dead Code Stripping 设置为NO,完全包含framework里的代码,拒绝剪裁,修改。
Link With Standard Libraries 设置为关闭,避免重复链接
Mach-O Type设置为Static Library framework可为动态库也可为静态库,对于用户来说只能选择静态库。
Build Active Architecture only 设置为Yes, 使其编译时只生成当前机器的架构。

5.framework合并

lipo -create xxxx/ProjectName.framework/ProjcetName xxxx/ProjectName.framework/ProjcetName -output xxxx/ProjectName.framework
lipo -create \
~/Library/Developer/Xcode/DerivedData/StaticFramework-cjzeucukluzhhscxruhakzuohbtp/Build/Products/Debug-iphoneos/StaticFramework.framework/StaticFramework \
~/Library/Developer/Xcode/DerivedData/StaticFramework-cjzeucukluzhhscxruhakzuohbtp/Build/Products/Debug-iphonesimulator/StaticFramework.framework/StaticFramework \
~/Library/Developer/Xcode/DerivedData/StaticFramework-cjzeucukluzhhscxruhakzuohbtp/Build/Products/Release-iphoneos/StaticFramework.framework/StaticFramework \
~/Library/Developer/Xcode/DerivedData/StaticFramework-cjzeucukluzhhscxruhakzuohbtp/Build/Products/Release-iphonesimulator/StaticFramework.framework/StaticFramework \
-output ~/Desktop/app/StaticFramework.framework

 

在SDK项目的target中加脚本做framework半自动打包
由于每次合并都要打开终端,执行lipo命令是很麻烦的,这里使用xcode提供的工具,编译阶段执行脚本,自动进行合并。
在SDK项目的target中加脚本做framework自动合并,不同架构的framework要自己手动运行项目生成。
在SDK项目下,依次进行点击Project -> Targets -> Build Phases + -> New Run Script Phases
# Type a script or drag a script file from your workspace to insert its path.
if [ "${ACTION}" = "build" ]
then
    INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}.framework
    
    DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework
    
    SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework
    
    # 如果真机包或模拟包不存在,则退出合并
    if [ ! -d "${DEVICE_DIR}" ] || [ ! -d "${SIMULATOR_DIR}" ]
    then
        exit 0
    fi
    
    # 如果合并包已经存在,则替换
    if [ -d "${INSTALL_DIR}" ]
    then
        rm -rf "${INSTALL_DIR}"
    fi

    mkdir -p "${INSTALL_DIR}"
    
    cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
    
    # 使用lipo命令将其合并成一个通用framework 
    # 最后将生成的通用framework放置在工程根目录下新建的Products目录下 
    lipo -create "${DEVICE_DIR}/${PROJECT_NAME}" "${SIMULATOR_DIR}/${PROJECT_NAME}" -output "${INSTALL_DIR}/${PROJECT_NAME}"
    
    #合并完成后打开目录
    open "${SRCROOT}/Products"

fi
 
通过新建aggregate合计工程做framework脚本完全自动合并
1.创建aggregate项目,添加Targets下面的“+”, 选择other -> aggregate项目,进行创建。
2.在aggregate项目下,依次进行点击Project -> Targets -> Build Phases + -> New Run Script Phases, 在里面添加执行脚本
3.运行项目,生成framework
# 取得项目名字(get project name)
FMK_NAME=${PROJECT_NAME}
# 取得生成的静态库文件路径 (get framework path)
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
# 设置真机和模拟器生成的静态库路径 (set devcie framework and simulator framework path)
WRK_DIR=${BUILD_ROOT}
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
# 模拟器和真机编译 (device and simulator build)
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos clean build
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator clean build
# 删除临时文件 (delete temp file)
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
# 拷贝真机framework文件到生成路径下 (copy device file to product path)
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
# 合并生成,替换真机framework里面的二进制文件,并且打开 (merger and open)
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"
echo "${DEVICE_DIR}/${FMK_NAME}"
echo "${SIMULATOR_DIR}/${FMK_NAME}"
rm -rf "${WRK_DIR}"
echo "${INSTALL_DIR}"

 

参考文章: