xcode 如何在多个框架子项目中使用 CocoaPods

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41294652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:40:42  来源:igfitidea点击:

How to use CocoaPods with multiple Framework subprojects

iosxcodedependenciescocoapods

提问by Allen Hsu

First of all, I've turned on use_framework! in Podfile.

首先,我打开了use_framework!在 Podfile 中。

Assume the main project is MAIN_APP, and two subprojects are FRAMEWORK_A and FRAMEWORK_B.

假设主项目是MAIN_APP,两个子项目是FRAMEWORK_A和FRAMEWORK_B。

MAIN_APP requires FRAMEWORK_A and FRAMEWORK_B, and FRAMEWORK_B requires FRAMEWORK_A as well.

MAIN_APP 需要 FRAMEWORK_A 和 FRAMEWORK_B,而 FRAMEWORK_B 也需要 FRAMEWORK_A。

All projects/targets are using CocoaPods to manage third party libraries.

所有项目/目标都使用 CocoaPods 来管理第三方库。

For now, my Podfile looks like:

现在,我的 Podfile 看起来像:

target :MAIN_APP do
    project 'MAIN_APP'
    pod 'PodA'
end

target :FRAMEWORK_A do
    project 'FRAMEWORK_A'
    pod 'PodB'
end

target :FRAMEWORK_B do
    project 'FRAMEWORK_B'
    pod 'PodC'
end

I manually added FRAMEWORK_A to build settings of FRAMEWORK_B, and both FRAMEWORK_A and FRAMEWORK_B to build settings of MAIN_APP.

我手动添加了 FRAMEWORK_A 来构建 FRAMEWORK_B 的设置,以及 FRAMEWORK_A 和 FRAMEWORK_B 来构建 MAIN_APP 的设置。

All code compiles well, but when running the MAIN_APP crashes because it cannot load Framework of PodB.

所有代码都编译得很好,但是在运行 MAIN_APP 时崩溃了,因为它无法加载 PodB 的框架。

I know I can manually add PodB to MAIN_APP and FRAMEWORK_B as well, but is it possible to define this kind of target dependency in Podfile?

我知道我也可以手动将 PodB 添加到 MAIN_APP 和 FRAMEWORK_B,但是是否可以在 Podfile 中定义这种目标依赖项?

Btw, when pod install, I got the warning:

顺便说一句,当pod install我收到警告时:

[!] The Podfile contains framework targets, for which the Podfile does not contain host targets (targets which embed the framework).

If this project is for doing framework development, you can ignore this message. Otherwise, add a target to the Podfile that embeds these frameworks to make this message go away (e.g. a test target).

[!] Podfile 包含框架目标,Podfile 不包含宿主目标(嵌入框架的目标)。

如果这个项目是做框架开发的,可以忽略这条信息。否则,将目标添加到嵌入这些框架的 Podfile 中,以使此消息消失(例如,测试目标)。

As I know, I can use nested target for host targets like:

据我所知,我可以将嵌套目标用于主机目标,例如:

target :FRAMEWORK_A
    target :MAIN_APP
    end
end

So CocoaPods will setup MAIN_APP to use FRAMEWORK_A and inherit pod dependencies from FRAMEWORK_A. But seems I cannot do it with multiple dependencies like:

因此 CocoaPods 将设置 MAIN_APP 以使用 FRAMEWORK_A 并从 FRAMEWORK_A 继承 pod 依赖项。但似乎我无法使用多个依赖项来做到这一点,例如:

target :FRAMEWORK_A
    target :MAIN_APP
    end
end
target :FRAMEWORK_B
    target :MAIN_APP
    end
end

Because target :MAIN_APP cannot be declared twice.

因为 target :MAIN_APP 不能被声明两次。

Is there any better solutions instead of defining pod dependencies as a function in Podfile and include in all target?

有没有更好的解决方案,而不是将 pod 依赖项定义为 Podfile 中的一个函数并包含在所有目标中?

采纳答案by iwasrobbed

I think you can also get around this by just making FrameworkAand FrameworkBinto local (static library) pods and it will de-duplicate everything for you and integrate it into the host app properly.

我认为您也可以通过制作FrameworkAFrameworkB放入本地(静态库)pod来解决此问题,它会为您删除所有内容并将其正确集成到主机应用程序中。

Examples: https://github.com/rob-keepsafe/PodFrameworksIssue

示例:https: //github.com/rob-keepsafe/PodFrameworksIssue

  • masterbranch shows duplicate classes and umbrella frameworks like you have
  • dedupedbranch makes the internal dynamic frameworks into local pods (as static libs) to de-dupe and still link in the dependencies
  • master分支显示像你一样的重复类和伞形框架
  • deduped分支使内部动态框架成为本地 pod(作为静态库)以重复数据删除并仍然链接依赖项

回答by Johannes Fahrenkrug

This is a great question and I've struggled with a similar situation. This is my PodFile:

这是一个很好的问题,我也遇到过类似的情况。这是我的 PodFile:

platform :ios, '8.0'

workspace 'mygreatapp.xcworkspace'

project 'app/MyGreatApp/MyGreatApp.xcodeproj'
project 'platform/MyGreatFramework/MyGreatFramework.xcodeproj'

abstract_target 'This can say whatever you want' do

    target 'MyGreatApp' do
        project 'app/MyGreatApp/MyGreatApp.xcodeproj'
        pod 'AFNetworking', '~> 2.6.0'
        pod 'PromiseKit', '~> 1.5'
        pod 'PromiseKit/Join'
        pod 'KVOController', '~> 1.0'
        pod 'FLAnimatedImage', '~> 1.0'
        pod 'Crashlytics', '~> 3.3'
        pod 'SSZipArchive'
    end

    target 'MyGreatAppTests' do
        project 'app/MyGreatApp/MyGreatApp.xcodeproj'
        pod 'OCMock', '~> 3.1'
    end

    target 'MyGreatFramework' do
        project 'platform/MyGreatFramework/MyGreatFramework.xcodeproj'
        pod 'SSZipArchive'
    end

    target 'MyGreatFrameworkTests' do
        project 'platform/MyGreatFramework/MyGreatFramework.xcodeproj'
        pod 'OCMock', '~> 3.1'
    end

    post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
      end
    end
end

As you can see I'm not using frameworks and I use an abstract_targetto group it all together. I wish these kinds of dependencies were easier to do in CocoaPods. I know this doesn't really answer your question but it might be helpful nonetheless.

如您所见,我没有使用框架,而是使用 anabstract_target将它们组合在一起。我希望在 CocoaPods 中更容易实现这些类型的依赖项。我知道这并不能真正回答你的问题,但它可能会有所帮助。

回答by Tim Kofoed

I'm not entirely sure your issue is the same as mine, but I'm going to leave my solution here just-in-case someone has a similar issue.

我不完全确定你的问题和我的一样,但我会把我的解决方案留在这里以防万一有人有类似的问题。

I have a project with multiple sub-projects I use to modularize my code (and potentially prepare to extract to my own private pods). I had the issue of one importing an external pod to one of the sub-projects, and receiving a dyld error due to a "missing image".

我有一个包含多个子项目的项目,用于模块化我的代码(并可能准备提取到我自己的私有 Pod)。我遇到了将外部 pod 导入其中一个子项目的问题,但由于“缺少图像”而收到 dyld 错误。

I found this Medium article from which I concluded that I had to always include the pods in the main project for the sub-projects to be able to find them. Neither of my external pods are used in the main project. ( https://medium.com/@akfreas/how-to-use-cocoapods-with-your-internal-ios-frameworks-192aa472f64b) (I'm probably not writing the podfile as correctly or efficiently as I could, but this seems to fix my issue)

我发现了这篇 Medium 文章,我从中得出的结论是,我必须始终将 pod 包含在主项目中,以便子项目能够找到它们。我的外部 Pod 都没有在主项目中使用。(https://medium.com/@akfreas/how-to-use-cocoapods-with-your-internal-ios-frameworks-192aa472f64b)(我可能没有尽可能正确或有效地编写 podfile,但是这似乎解决了我的问题)

My podfile is therefore as follows:

因此,我的 podfile 如下:

abstract_target "RandomName" do

    target "MainProject" do
        inherit! :complete
        workspace './MainProject.xcodeproj'
        pod 'Moya', '~> 13.0'
        pod 'KeychainSwift', '~> 17.0'
    end

    target "ModuleA" do
      project './ModuleA/ModuleA.xcodeproj'
      workspace './ModuleA/ModuleA.xcodeproj'
      pod 'Moya', '~> 13.0'
    end

    target "ModuleB" do
      project './ModuleB/ModuleB.xcodeproj'
      workspace './ModuleB/ModuleB.xcodeproj'
      pod 'KeychainSwift', '~> 17.0'
    end
end