You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CocoaPods单个target中如何同时使用静态与动态framework?

解决方案

方案说明

你当前Podfile中已经引入了cocoapods-pod-linkage插件,该插件原生支持为单个Pod指定不同的链接类型,刚好可以满足你部分库用动态链接、部分用静态链接的需求,不需要拆分调用use_frameworks!配置。

修改方案(两种可选)

方案1:批量指定链接类型(无需修改原有pod声明)

直接在target定义末尾通过插件提供的批量配置方法指定链接规则即可,完整Podfile如下:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

plugin 'cocoapods-binary'
plugin 'cocoapods-pod-linkage'

def install_dynamic_library
  pod 'R.swift'
  pod 'Alamofire'
  pod 'Nuke'
  pod 'Parchment', '~> 3.0'
  pod 'SnapKit', '~> 5.0.0'
  pod 'Kanna', '~> 5.2.2'
  pod 'ImageViewer'
  pod 'PanModal'
  pod 'SideMenu'
  pod 'Toast-Swift', '~> 5.0.1'
  pod 'youtube-ios-player-helper', '~> 1.0.2'
  pod 'SwiftDate'
  pod "URLEmbeddedView"
  pod 'ActiveLabel'
end

def install_static_library
  pod 'GoogleMobileAdsMediationFacebook'
  pod 'Google-Mobile-Ads-SDK'
  pod 'Firebase/Analytics', '~> 8.1.0'
  pod 'Firebase/Messaging'
  pod 'Firebase/Auth'
  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.1.0'
  pod 'FirebaseFirestoreSwift', '8.2.0-beta'
  pod 'Firebase/Storage'
end

target :'MyApp' do
  use_frameworks!
  install_dynamic_library
  install_static_library
  
  # 批量指定需要动态链接的库
  set_linkage :dynamic, [
    'R.swift', 'Alamofire', 'Nuke', 'Parchment', 'SnapKit', 
    'Kanna', 'ImageViewer', 'PanModal', 'SideMenu', 'Toast-Swift',
    'youtube-ios-player-helper', 'SwiftDate', 'URLEmbeddedView', 'ActiveLabel'
  ]
  # 剩余所有库默认用静态链接
  set_default_linkage :static
end

方案2:单独为每个Pod指定链接类型

在每个pod声明后加:linkage参数显式指定链接规则,完整Podfile如下:

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

plugin 'cocoapods-binary'
plugin 'cocoapods-pod-linkage'

def install_dynamic_library
  pod 'R.swift', :linkage => :dynamic
  pod 'Alamofire', :linkage => :dynamic
  pod 'Nuke', :linkage => :dynamic
  pod 'Parchment', '~> 3.0', :linkage => :dynamic
  pod 'SnapKit', '~> 5.0.0', :linkage => :dynamic
  pod 'Kanna', '~> 5.2.2', :linkage => :dynamic
  pod 'ImageViewer', :linkage => :dynamic
  pod 'PanModal', :linkage => :dynamic
  pod 'SideMenu', :linkage => :dynamic
  pod 'Toast-Swift', '~> 5.0.1', :linkage => :dynamic
  pod 'youtube-ios-player-helper', '~> 1.0.2', :linkage => :dynamic
  pod 'SwiftDate', :linkage => :dynamic
  pod "URLEmbeddedView", :linkage => :dynamic
  pod 'ActiveLabel', :linkage => :dynamic
end

def install_static_library
  pod 'GoogleMobileAdsMediationFacebook', :linkage => :static
  pod 'Google-Mobile-Ads-SDK', :linkage => :static
  pod 'Firebase/Analytics', '~> 8.1.0', :linkage => :static
  pod 'Firebase/Messaging', :linkage => :static
  pod 'Firebase/Auth', :linkage => :static
  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.1.0', :linkage => :static
  pod 'FirebaseFirestoreSwift', '8.2.0-beta', :linkage => :static
  pod 'Firebase/Storage', :linkage => :static
end

target :'MyApp' do
  use_frameworks!
  install_dynamic_library
  install_static_library
end

生效操作

修改完成后执行以下命令重新安装依赖:

  • pod deintegrate 清理原有Pods配置
  • pod install 重新生成依赖配置

注意事项

  • 全局仍需保留use_frameworks!声明,插件会基于此覆盖单个Pod的链接规则
  • 如果静态库包含Swift代码,需要在项目Build Settings中把Always Embed Swift Standard Libraries设置为YES,避免运行时崩溃
  • 若遇到个别库编译报错,可单独调整该库的linkage参数适配

内容的提问来源于stack exchange,提问作者Kou Yamamoto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 01:21:03