iOS之路

制作静态库并支持Bitcode

2017-11-13  本文已影响282人  Junetaurus

制作.a文件

新建项目,选择'Cocoa Touch Static Library'
添加静态库所需要的源代码
选择需要公开的 .h 文件, .m文件会自动编译到 .a 文件中
分别用模拟器和真机Build项目,然后右击Products下面的.a文件'Show In Finder'

//合并.a文件命令(注意空格)

lipo -create Debug-iphoneos/xxx.a Debug-iphonesimulator/xxx.a -output xx/xxx.a

lipo -create Release-iphoneos/xxx.a Release-iphonesimulator/xxx.a -output xx/xxx.a

//查看.a的类型

lipo –info xxx.a
i386 armv7 x86_64 arm64 

.a支持Bitcode

Bitcode是什么?

官方文档App Thinning (iOS, tvOS, watchOS)一节中,找到了下面这样一个定义:Bitcode is an intermediate representation of a compiled program. Apps you upload to iTunes Connect that contain bitcode will be compiled and linked on the App Store. Including bitcode will allow Apple to re-optimize your app binary in the future without the need to submit a new version of your app to the store.
大概意思就是:Bitcode是编译程序的一种中间形式的代码。你上传到iTunes Connect的应用程序将被编译并链接到App Store上。包括bitcode将允许苹果在未来重新优化你的应用程序二进制文件,而不需要向商店提交新版本的应用程序。

支持Bitcode
//针对于静态链接库.a文件  (armv7 i386 x86_64 arm64)(注意空格)
otool -arch armv7 -l xxxx.a | grep __bitcode | wc -l 
otool -arch i386 -l xxxx.a | grep __bitcode | wc -l 
otool -arch x86_64 -l xxxx.a | grep __bitcode | wc -l
otool -arch arm64 -l xxxx.a | grep __bitcode | wc -l 
otool -arch armv7 -l /Users/ang/Desktop/libANGDataCollection_1.8/libANGDataCollection.a | grep __bitcode | wc -l 
       4

shell脚本自动合成

if [ "${ACTION}" = "build" ]
then

#要build的target名
target_Name=${PROJECT_NAME}
    echo "target_Name=${target_Name}"

#build之后的文件夹路径
build_DIR=${SRCROOT}/build
    echo "build_DIR=${build_DIR}"

#真机build生成的头文件的文件夹路径
DEVICE_DIR_INCLUDE=${build_DIR}/Release-iphoneos/include/${PROJECT_NAME}
    echo "DEVICE_DIR_INCLUDE=${DEVICE_DIR_INCLUDE}"

#真机build生成的.a文件路径
DEVICE_DIR_A=${build_DIR}/Release-iphoneos/lib${PROJECT_NAME}.a
    echo "DEVICE_DIR_A=${DEVICE_DIR_A}"

#模拟器build生成的.a文件路径
SIMULATOR_DIR_A=${build_DIR}/Release-iphonesimulator/lib${PROJECT_NAME}.a
    echo "SIMULATOR_DIR_A=${SIMULATOR_DIR_A}"

#目标文件夹路径
INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}
    echo "INSTALL_DIR=${INSTALL_DIR}"

#目标头文件文件夹路径
INSTALL_DIR_Headers=${SRCROOT}/Products/${PROJECT_NAME}/Headers
    echo "INSTALL_DIR_Headers=${INSTALL_DIR_Headers}"

#目标.a路径
INSTALL_DIR_A=${SRCROOT}/Products/${PROJECT_NAME}/lib${PROJECT_NAME}.a
    echo "INSTALL_DIR_A=${INSTALL_DIR_A}"

#判断build文件夹是否存在,存在则删除
if [ -d "${build_DIR}" ]
then
rm -rf "${build_DIR}"
fi

#判断目标文件夹是否存在,存在则删除该文件夹
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
#创建目标文件夹
mkdir -p "${INSTALL_DIR}"

#build之前clean一下
xcodebuild -target ${target_Name} clean

#模拟器build
xcodebuild -target ${target_Name} -configuration Release -sdk iphonesimulator

#真机build
xcodebuild -target ${target_Name} -configuration Release -sdk iphoneos

#复制头文件到目标文件夹
cp -R "${DEVICE_DIR_INCLUDE}" "${INSTALL_DIR_Headers}"

#合成模拟器和真机.a包
lipo -create "${DEVICE_DIR_A}" "${SIMULATOR_DIR_A}" -output "${INSTALL_DIR_A}"

#打开目标文件夹
open "${INSTALL_DIR}"

fi
上一篇 下一篇

猜你喜欢

热点阅读