How to make framework in CocoaPods that has multiple targets?

Vas*_*ily 16 ios cocoapods swift carthage swift-package-manager

I write a framework and I like to divide framework to small separate submodules (targets). Apple provides a cool description for thing I want to achieve with CocoaPods:

Targets are the basic building blocks of a package. A target can define a module or a test suite. Targets can depend on other targets in this package, and on products in packages which this package depends on.

I could easily do it with Swift Package Manager:

    targets: [
        .target(name: "Network"),
        .target(name: "Service", dependencies: ["Network"])
    ],
Run Code Online (Sandbox Code Playgroud)

I could use import Network in a Service target and it's cool because they are separate modules, logic is clear.

How to achieve it in CocoaPods and Carthage (I write a framework, not the final application)?

What I've tried:

Subspec

I tried to use subspecs:

  s.subspec 'Service' do |ss|
    ss.dependency 'MyFramework/Network'
    ss.source_files = 'Sources/Service/**/*.swift'
  end

  s.subspec 'Network' do |ss|
    ss.source_files = 'Sources/Network/**/*.swift'
  end
Run Code Online (Sandbox Code Playgroud)

它不像我想要的那样工作,因为 CocoaPods 只是将所有文件合并到一个框架中(只是将其划分为单独的文件夹),因此:

  1. 我收到命名空间冲突。
  2. 致命错误,当我尝试import Network里面Service,因为没有Network经过目标pod install。所以我不能在 Swift Package Manager 中使用那个框架。MyFramework正如我之前提到的,CocoaPods 只是将所有内容合并到一个目标。

单独的 repos/pods

这是解决方案,但很难维护多个单独的 git 存储库并进行单独的提交和推送。我想将所有内容都保存在一个回购中。

Art*_*tch 2

我非常确定 CocoaPods 没有能力为一个 podspec 创建单独的目标。

正如您上面提到的,您可以创建单独的 podspec,但在一个存储库中,例如RxSwift。然后,在开发时,您可以使用一些包含所有 podspec 的示例项目。您不需要管理不同的存储库。

另外,您可以尝试创建包含不同模块的模块映射,例如:

framework module Something {
    framework module Core { ... }
    framework module Toolbox { ... }
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后在 podspec 中分配它,例如:

Pod::Spec.new do |s|
  s.module_map = "#{s.name}/#{s.name}.modulemap"
...
Run Code Online (Sandbox Code Playgroud)

但我对此并不确定。我只尝试了存储库中的第一种方法RxSwift