iOS利用cocoapods-pachager打包静态库

931 阅读2分钟

一、为什么要使用cocoapods-packager

为了解决SDK中引入的第三方库和接入项目中引入的第三方库冲突问题。

CocoaPods Packager在打包静态库的过程中会自动将引入的第三方库的符号加上前缀(Name-Mangling)。如果我们引入的第三方库也是一个静态库,那这个自动改名就无法生效了。

  • 安装 cocoapods-packager
 sudo gem install cocoapods-packager

二、制作pod库

1.cd到你想存放代码的目录
2.创建pod库
pod lib create FGCallKit

如果报一下错误fatal: unable to access 'https://github.com/CocoaPods/pod-template.git/': HTTP/2 stream 1 was not closed cleanly before end of the underlying stream

就执行

git config --global http.version HTTP/1.1

过程中会有一个问题页面,可参考如下的填写:

# 平台
What platform do you want to use?? [ iOS / macOS ]
 > iOS

# 语言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC

# 是否需要创建demo,一般选择Yes
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

# 测试框架
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

# 视图测试
Would you like to do view based testing? [ Yes / No ]
 > No

# 类前缀
What is your class prefix?
 > FG

三、打包静态库

1.打包前先做一下验证

cd到工程所在的目录下

# 本地验证
pod lib lint  FGCallKit.podspec --allow-warnings --use-libraries --verbose

# 远程验证(可选)
pod spec lint  FGCallKit.podspec --allow-warnings --use-libraries --verbose
2.提交代码、添加tag
add .

git commit -m '0.1.0'

# 这里的tag必须和.podspec文件中配置的s.version一致
git tag 0.1.0

git push origin 0.1.0
3.打包成.framework
pod package FGCallKit.podspec --force --no-mangle

这里的参数说明:

// 强制覆盖之前生成的文件
--force
// 不使用name-mangling技术,也就是自动改类名等符号
--no-mangle
// 生成静态的framework
--embedded
// 生成静态.a
--library
// 生成动态framework
--dynamic
// 使用本地文件
--local
// 生成动态framework的时候需要这个BundleId来签名
--bundle-identifier
// 不包含依赖的符号表,也就是不把依赖的第三方库打包进去
--exclude-deps
// 生成debug还是release的库,默认是release
--configuration=Release 
// 如果你的pod库有subspec,那么加上这个命名表示只给某个或几个subspec生成二进制库
--subspecs=subspec1,subspec2
// 默认是CocoaPods的Specs仓库,如果你的项目有私有的source,就可以通过这个参数来设置
--spec-sources=private,https://github.com/CocoaPods/Specs.git

如果报错:

error: The armv7 architecture is deprecated. You should update your ARCHS build setting to remove the armv7 architecture. (in target 'Pods-packager' from project 'Pods')`

参考Cocoapods Packager + Xcode14 适配 - 简书

其他错误:

[!] The 'Pods-XXX' target has transitive dependencies that include statically linked binaries: (/Users/...)
在 podspec文件中添加
s.static_framework = true