如何快速用上Swift静态库详解

时间:2022-09-20 19:32:48

前言

Swift 支持静态库打包已经有一段时间了,CocoaPods 也提供了 static_framework 参数。然而大部分的第三方依赖都没有及时更新。

本文给出个相对方便一些的方案用上静态库,个人感觉在依赖不那么复杂的时候成本还是比较低的。

效果如下:

如何快速用上Swift静态库详解

示例地址:UseStaticFramework

想办法为每一个 pod 添加 static_framework 是关键。

直接修改 podspec 不太现实,因为 CocoaPods 并没有提供相关的接口。但是当一个 pod 指定 podspec 地址时,这个 podspec 会被保存在本地。

如果 pod 没有更新,pod install 将直接从本地获取 pod 信息,这就为我们修改 pod 提供了可能。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
target 'UseStaticFramework' do
 pod 'RxSwift', :git => 'https://github.com/ReactiveX/RxSwift.git'
end
 
pre_install do |installer|
 installer.sandbox.specifications_root.children.each do |podspec|
  if podspec.extname() == '.json'
   edit_pod_spec podspec
  end
 end
end
 
def edit_pod_spec(file)
 code = File.read(file)
 json = JSON.parse(code)
 json['static_framework'] = true
 File.write(file, JSON.generate(json))
end

在 Podfile 中添加以上代码,执行两次 bundle exec pod install 即可将依赖 RxSwift 变成静态库。相比单独建一个 Specs 方便很多了,特别是在 RxSwift 有更新时,我们也无需增加成本,执行 bundle exec pod update 即可。

有些依赖稍微麻烦些,比如 RxCocoa 。就目前来看,Swift 静态库似乎还不能混编,好在 RxCocoa 支持 SPM,在 SPM 中有一个 RxCocoaRuntime 依赖。

创建一个 RxCocoaRuntime.podspec 使用,再调整一下 RxCocoa 的 podspec 即可,注意添加 SWIFT_PACKAGE 编译标记:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pod 'RxCocoa', :git => 'https://github.com/ReactiveX/RxSwift.git'
pod 'RxCocoaRuntime', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/RxCocoaRuntime.podspec'
def edit_pod_spec(file)
 code = File.read(file)
 json = JSON.parse(code)
 json['static_framework'] = true
 if json['name'] == 'RxCocoa'
  json['xcconfig'] = {
   :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"'
  }
  json['source_files'] = ['RxCocoa/RxCocoa.swift', 'RxCocoa/Common/**/*.{swift}', 'RxCocoa/Traits/**/*.{swift}', 'RxCocoa/Foundation/**/*.{swift}', 'RxCocoa/Runtime/**/*.{swift}', 'Platform/**/*.swift']
  json['preserve_paths'] = ['RxCocoa/RxCocoa.h', 'RxCocoa/*.swift', 'RxCocoa/Common/**/*.{swift,h,m}', 'RxCocoa/Traits/**/*.{swift,h,m}', 'RxCocoa/Foundation/**/*.{swift,h,m}', 'RxCocoa/Runtime/**/*.{swift,h,m}', 'Platform/**/*.swift']
  json['dependencies'] = {
   :RxSwift => '~> 4.1',
   :RxCocoaRuntime => '~> 4.1'
  }
 end
 File.write(file, JSON.generate(json))
end

执行两次 bundle exec pod install,完成。

Apollo 这种也能搞,稍微麻烦一些,有些代码没有引入 UIKit,最终导致按照上面的方案编译不过去。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pod 'SQLite.swift', :git => 'https://github.com/stephencelis/SQLite.swift.git'
pod 'SQLiteObjc', :podspec => 'https://raw.githubusercontent.com/DianQK/UseStaticFramework/master/SQLiteObjc.podspec'
 
pod 'Apollo', :git => 'https://github.com/apollographql/apollo-ios.git'
pod 'Apollo/SQLite', :git => 'https://github.com/apollographql/apollo-ios.git'
# edit_pod_spec
if json['name'] == 'SQLite.swift'
 json['xcconfig'] = {
  :OTHER_SWIFT_FLAGS => '$(inherited) "-D" "SWIFT_PACKAGE"'
 }
 json['dependencies'] = {
  :SQLiteObjc => '~> 0.11.4'
 }
 json['subspecs'] = [{
  :name => 'standard',
  :source_files => 'Sources/{SQLite,SQLiteObjc}/**/*.{swift}',
  :exclude_files => 'Sources/**/Cipher.swift',
  :library => 'sqlite3'
 }]
end
 
post_install do |installer|
 %w(Pods/Apollo/Sources/ApolloSQLite/*.swift).flat_map { |x| Dir.glob(x) }.each do |file|
 code = File.read(file)
 unless code.include? "import UIKit"
  FileUtils.chmod("+w", file)
  File.write(file, "import UIKit\n" + code)
 end
 end
end

给这些没添加 import UIKit 代码补上就行了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://blog.dianqk.org/2018/03/08/use-static-framework/