前言
前面的两遍文章分别介绍了制作真机上面的静态库和动态库,但是问题又来了,真机上面的库是不能在模拟器上面运行的,是直接编译不过的,那么有没有一种办法,既可以在真机上面运行也能在模拟器上面运行?那我可以肯定的告诉你,是绝对可以的。下面我就带你一步一步的制作。 制作真机上面的静态库 制作真机上面的动态库
制作通用静态库
在我制作真机上面的静态库的基础上,我这边新建了一个StaticLibrary来演示制作。
在之前的基础上分别选择真机和模拟器运行,然后Show in Finder 以此生成两种类型的.a文件。如下截图

接下来就是:合并模拟器(release-iphonesimulator)和真机(release-iphoneos)下的.a,以便支持真机和模拟器,合并后的.a大小大约是不合并的2倍左右。
首先我新建一个文件夹,命名为SDK,将上截图中的 Debug-iphoneos 和 Debug-iphonesimulator文件拷贝进SDK文件夹中,然后打开终端应用,准备执行如下命令
一、在终端输入:lipo -create
二、将debug-iphonesimulator下的.a拖进终端,打个空格
三、继续将debug-iphoneos 下.a 拖进终端,打个空格
四、继续输入:-output ,打个空格
五、输入合并后的.a所要放的文件路径,并且命名.a文件(我这里是Static.a),最后回车
最终的命令如下:
lipo -create /Users/lengqiuhui/Desktop/SDK/Debug-iphonesimulator/libStaticLibrary.a /Users/lengqiuhui/Desktop/SDK/Debug-iphoneos/libStaticLibrary.a -output /Users/lengqiuhui/Desktop/Value/Static.a 回车
截图如下:

PS:我这里的 /Users/lengqiuhui/Desktop/SDK/Debug-iphonesimulator/libStaticLibrary.a 是模拟器.a文件在本机的路径
/Users/lengqiuhui/Desktop/SDK/Debug-iphoneos/libStaticLibrary.a 是真机.a文件在本机的路径 /Users/lengqiuhui/Desktop/Value是生成最终合并的文件路径,Static.a 是合并后的.a文件名字
合并后的最终的结果如下:

最后检查合并后的Static.a文件是不是满足所有要求,依然在终端进行以下操作:
一、在终端输入:lipo -info
二、将合并后的.a拖进终端 ,回车
最终的命名如下:lipo-info /Users/lengqiuhui/Desktop/Value/Static.a 回车

从终端信息可以看出:只支持 i386 和 arm64 处理器的指令集,
最后只需要将制作的 Static.a 文件和之前漏出的头文件放入到项目中既可,再也不用担心源码暴漏或别人随意修改你的代码了,不过如果有图片资源,图片资源是不能打进.a的,需要在外面添加的哦。
最终效果如下截图:
模拟器:

真机:

制作通用动态库
在我制作真机上面的动态库的基础上,首先新建一个Aggregate Target 我这里命名为:GeneralWithDynamic。 File——New——Target——Cross-platform——Aggregate 如下截图

添加script到新建的Target,然后在script中添加如下信息

# 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.
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 "${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}/"
# 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}"
open "${INSTALL_DIR}"


添加信息之后,点击运行刚刚新建的GeneralWithDynamic,如果没有异常的话,会自动弹出生成的Framework文件, 然后查看Framework支持哪些架构,在终端输入:lipo -info 然后将生成后的Framework拖进终端 ,回车(和上面查看静态库支持哪些架构是一样的),最后将这个Framework添加到项目中运行即可,如下截图

最后就是测试使用动态库,我已经在上一篇文章中写明,地址如下:制作和使用动态库
最后:上面所诉的就是制作Debug(调试)下的通用静态库和动态库,如果需要制作Release(发布)下的,只需要通过下面截图将Debug改为Release,运行后生成对应的Release文件,其他步骤和上面一致即可。

制作发布后的静态库中的.a文件支持命令集如下:

网友评论