背景

我们小组需要持续向其他公司或部门提供一套IM(即时通讯)SDK,为了隐藏源码实现,就需要将它打包为静态库(.a/.framework)的形式。该项目使用cocoapods管理,依赖许多私有库,并且私有库又依赖私有库、开源库等,这些私有库又会涉及频繁更新,每次打包静态库时都需要保证是最新的代码,并且还要解决各个库的依赖问题。在这种情况下,怎么才能方便快速的打包静态库呢?幸好 cocoapods 已经给我们提供了一个打包插件:Cocoapods-package,这让打包静态库的任务变得简单许多。

第一步:安装 cocoapods-packager

sudo gem install cocoapods-packager

第二步:实例

  • 制作一个静态库,格式是.a
  • 制作pod库demo, 依赖 上面一步的 .a库
  • 第三库打包静态库,也就是给业务方的私有库或者动态,但是这里只掩饰静态库,需要可以私信我

2.1 制作一个静态库,格式是.a

由于制作static library is so easy ! ,这里推荐一篇文档 [xcode制作私有库步骤](https://www.jianshu.com/p/2f2778a6fd7c

2.2 制作pod库demo, 依赖 上面一步的 .a库

-2.2.1

pod lib create  CalculatorPodsSDK

上面的指令创建一个pod 库工程,名字为 CalculatorPodsSDK ,前提是你需要一个pod repo ,这里不做演示,我的这个demo github地址:https://github.com/GE-GAO-ZHAO/GGZLibCalculatorSDK.git
-2.2.2 配置podspec文件 ,我的如下:

#
# Be sure to run `pod lib lint CalculatorPodsSDK.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'CalculatorPodsSDK'
  s.version          = '1.0.2'
  s.summary          = 'A short description of CalculatorPodsSDK.'

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
  static libary test
  DESC

  s.homepage         = 'https://github.com/GE-GAO-ZHAO/GGZLibCalculatorSDK'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'gegaozhao' => 'gegaozhao1126@gmail.com' }
  s.source           = { :git => 'https://github.com/GE-GAO-ZHAO/GGZLibCalculatorSDK.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.subspec 'CalculatorSDKHeader' do |calculatorSDKHeader|
    calculatorSDKHeader.source_files = 'CalculatorPodsSDK/Classes/CalculatorSDKHeader/*.h'
    calculatorSDKHeader.public_header_files = 'CalculatorPodsSDK/Classes/CalculatorSDKHeader/*.h'
  end

  s.vendored_libraries = 'CalculatorPodsSDK/Classes/libCalculatorSDK.a'

  # s.frameworks = 'UIKit', 'MapKit'
  s.frameworks = 'Foundation', 'UIKit'
  # s.dependency 'AFNetworking', '~> 2.3'

  s.requires_arc = true
  
end

2.3 第三库打包静态库,也就是给业务方的私有库或者动态,但是这里只掩饰静态库,需要可以私信我

-2.3.1 验证

pod lib lint --allow-warnings --use-libraries --verbose 需要本地验证通过
pod spec lint --allow-warnings --use-libraries --verbose  需要远程验证通过

-2.3.2 打包

pod package CalculatorPodsSDK.podspec --force --library --no-manglepo

打包问题:

-2.3.4 pod package 指令打包常用的指令集合

//强制覆盖之前已经生成过的二进制库 
--force

//生成静态.framework 
--embedded

//生成静态.a 
--library

//生成动态.framework 
--dynamic

//动态.framework是需要签名的,所以只有生成动态库的时候需要这个BundleId 
--bundle-identifier

//不包含依赖的符号表,生成动态库的时候不能包含这个命令,动态库一定需要包含依赖的符号表。 
--exclude-deps

//表示生成的库是debug还是release,默认是release。--configuration=Debug 
--configuration


--no-mangle
//表示不使用name mangling技术,pod package默认是使用这个技术的。我们能在用pod package生成二进制库的时候会看到终端有输出Mangling symbols和Building mangled framework。表示使用了这个技术。
//如果你的pod库没有其他依赖的话,那么不使用这个命令也不会报错。但是如果有其他依赖,不使用--no-mangle这个命令的话,那么你在工程里使用生成的二进制库的时候就会报错:Undefined symbols for architecture x86_64。

--subspecs

//如果你的pod库有subspec,那么加上这个命名表示只给某个或几个subspec生成二进制库,--subspecs=subspec1,subspec2。生成的库的名字就是你podspec的名字,如果你想生成的库的名字跟subspec的名字一样,那么就需要修改podspec的名字。 
这个脚本就是批量生成subspec的二进制库,每一个subspec的库名就是podspecName+subspecName。

--spec-sources
//一些依赖的source,如果你有依赖是来自于私有库的,那就需要加上那个私有库的source,默认是cocoapods的Specs仓库。--spec-sources=private,https://github.com/CocoaPods/Specs.git。 可以跟多个,多个逗号隔开

-2.3.5 打包会在工程的podspec同级生成目标文件,如下,提供给业务方即可,也可以把文档给业务方:


截屏2020-07-01 下午4.40.51.png

常见问题

问题1: xcode12及以上版本打包出现静态库arm64冲突问题
原因: xcode12苹果新增了模拟器的arm64架构和真机的arm64冲突了
修复方式: 如果是手动合并出错在Xcode里进行架构忽略即可,忽略方式网上一大推,就是忽略模拟器架构即可; 如果你是用pod packets打包出错,在pod spec文件里忽略arm64 即可