在本篇文章中主要来记录使用Xcode12来制作静态库的过程,及出现的问题。静态库分为两种.framework
文件和.a
文件,在本文中,我们以.framework
为例。
制作静态库
1,新建framework
工程,Language
一栏我们选择Objective-C
。
2,在工程中分别新建一个OC
类和Swift
类
OC类: LYPerson:
@interface LYPerson : NSObject
+ (void) run;
@end
#import "LYPerson.h"
@implementation LYPerson
+(void)run {
NSLog(@"正在跑步......");
}
@end
Swift类:Calculator
import Foundation
public class Calculator {
public static func add(_ sum1: Int, _ sum2: Int) -> Int {
return sum1 + sum2;
}
}
swift class
需要指定类为public class
,其方法也应为public
,才可以被外界引用。-
OC class
需要将暴露给外界使用的类设置为public
将
暴露的OC类
倒入库的头文件LYKitDemo
中
//! Project version number for LYKitDemo.
FOUNDATION_EXPORT double LYKitDemoVersionNumber;
//! Project version string for LYKitDemo.
FOUNDATION_EXPORT const unsigned char LYKitDemoVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <LYKitDemo/PublicHeader.h>
#import "LYPerson.h"
整体目录结构如下
3,将项目的Build Configuration
设置为 Release
4,编译 simulator
静态库
选择任意一个模拟器
,按下 command + B
进行编译,编译成功后,在 Finder 中查看 LYKitDemo.framework
。
5,查看
framework
支持的架构lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo
结果如下
Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo are: i386 x86_64 arm64
- 在
Xcode12
,通过模拟器编译出来的静态库支持i386,x86_64,arm64
架构。
6,编译真机
静态库,选择Any iOS Device
,按照步骤4中的步骤,在操作一边。并查看
同样使用
lipo -info
命令查看其支持的架构
admin@localhost ~/Desktop lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo
Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo are: armv7 arm64
- 用真机编译出来的静态库,支持
armv7 和 arm64
架构。
7, 将 模拟器
和真机
下的静态库进行合并
使用:lipo -create 静态库1的地址 静态库2的地址 -output 输出路径
admin@localhost ~/Desktop lipo -create "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo" "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo" -output "/Users/admin/Desktop/IPA包/LYKitDemo"
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo and /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo have the same architectures (arm64) and can't be in the same fat output file
在合并的时候发生了错误❌have the same architectures (arm64) and can't be in the same fat output file
,是因为两个文件都支持arm64
架构,我们需要重新编译一个不包含 arm64架构的模拟器静态库
。
我们修改 Build Setting
中的 Excluded Architectures
选项,将其值变为arm64
。
再次编译,并查看其信息:
lipo -info /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo
Architectures in the fat file: /Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo are: i386 x86_64
这样模拟器下的静态库只有 i386 和 x86_64
架构,我们再次将模拟器和真机下的静态库进行合并:
lipo -create "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphoneos/LYKitDemo.framework/LYKitDemo" "/Users/admin/Library/Developer/Xcode/DerivedData/LYKitDemo-anioropnglgdfcdumxqlsqwhnusk/Build/Products/Release-iphonesimulator/LYKitDemo.framework/LYKitDemo" -output "/Users/admin/Desktop/IPA包/LYKitDemo"
这样我们能合并成功
查看新的静态库的信息
admin@localhost ~/Desktop lipo -info /Users/admin/Desktop/IPA包/LYKitDemo
Architectures in the fat file: /Users/admin/Desktop/IPA包/LYKitDemo are: armv7 i386 x86_64 arm64
8,使用 Run Script 合并完整的静态库信息
8.1 在 Build Setting中新增 RunScript 选项
并将以下脚本信息写入
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 "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
#ditto "${DEVICE_DIR}/Headers" "${INSTALL_DIR}/Headers"
lipo -create "${DEVICE_DIR}/${PROJECT_NAME}" "${SIMULATOR_DIR}/${PROJECT_NAME}" -output "${INSTALL_DIR}/${PROJECT_NAME}"
#open "${DEVICE_DIR}"
open "${SRCROOT}/Products"
fi
编译工程,就会得到合并后的静态库同时支持 模拟器和真机
的静态库。
9,将x86
架构下的swiftModule
拷贝至 modules
文件中。
modules
的文件结构如下:
常见错误
dyld: Library not loaded: @rpath/LYKitDemo.framework/LYKitDemo
Referenced from: /Users/admin/Library/Developer/CoreSimulator/Devices/39CF7B52-BCEE-4BB2-AF78-CCB581E06F91/data/Containers/Bundle/Application/CB27BBCF-D80B-44A9-A88D-7116E18C7CA9/LYCalculatorTest.app/LYCalculatorTest
Reason: image not found
需要将静态库由Do Not Embed
变为 Embed & Sign
have the same architectures (arm64) and can't be in the same fat output file
两个静态库包含了同一种架构,需要去除一个
Could not find module 'LYKitDemo' for target 'x86_64-apple-ios-simulator'; found: arm64, armv7-apple
这是因为静态库中,没有 x86
的 swiftmodule
文件。我们使用命令合并完两个静态库后,需要手动将 swiftmodule
文件合并到一起。
最后附上本文相关Demo: LYKitDemo :静态库工程,LYCalculatorTest:静态库使用工程。