iOS利用cocoapods-pachager打包静态库

一、为什么要使用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 适配 -

其他错误:

  • 错误1
[!] The 'Pods-XXX' target has transitive dependencies that include statically linked binaries: (/Users/...)

解决:在 podspec文件中添加

s.static_framework = true
  • 错误2
fatal error: /Applications/Xcode 12.0.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: Pods/build/package.a and Pods/build-sim/package.a have the same architectures (arm64) and can't be in the same fat output file

解决:前往文件夹/Library/Ruby/Gems/2.6.0/gems/cocoapods-packager-1.5.0/lib/cocoapods-packager,找到pod_utils.rb代开,在33行添加以下代码

config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
  • 错误3
[!] Unable to find a specification for `xxx` depended upon by `xxx`

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.

解决:更新一下repo

pod repo update
// 或
pod install --repo-update
  • 错误4
[!] The 'xxx' target has transitive dependencies that include statically linked binaries: (/../../xxx.framework)

解决:在当前xxx.podspec中添加s.static_framework = true;

  • 错误5
    pod trunk push --allow-warnings时出现一下错误
- NOTE  | [iOS] xcodebuild:  Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 10.0, but the range of supported deployment target versions is 11.0 to 16.2.99. (in target 'FGChatKit-pod' from project 'Pods')

解决:就在.podspec中添加一下代码

# 排除arm64架构
  s.pod_target_xcconfig = {
      'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
  }
  s.user_target_xcconfig = {
      'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64'
  }

你可能感兴趣的:(iOS利用cocoapods-pachager打包静态库)