问题描述
首先,我打开了use_framework!在Podfile中。
First of all, I've turned on use_framework! in Podfile.
假设主项目是MAIN_APP,两个子项目是FRAMEWORK_A和FRAMEWORK_B。
Assume the main project is MAIN_APP, and two subprojects are FRAMEWORK_A and FRAMEWORK_B.
MAIN_APP要求FRAMEWORK_A和FRAMEWORK_B以及FRAMEWORK_B也需要FRAMEWORK_A。
MAIN_APP requires FRAMEWORK_A and FRAMEWORK_B, and FRAMEWORK_B requires FRAMEWORK_A as well.
所有项目/目标都使用CocoaPod来管理第三方库。
All projects/targets are using CocoaPods to manage third party libraries.
目前,我的Podfile如下所示:
For now, my Podfile looks like:
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
我手动添加了FRAMEWORK_A以构建FRAMEWORK_B的设置,并使用FRAMEWORK_A和FRAMEWORK_B来构建MAIN_APP的设置。
I manually added FRAMEWORK_A to build settings of FRAMEWORK_B, and both FRAMEWORK_A and FRAMEWORK_B to build settings of MAIN_APP.
所有代码编译良好,但在运行时MAIN_APP崩溃,因为它无法加载PodB的框架。
All code compiles well, but when running the MAIN_APP crashes because it cannot load Framework of PodB.
我知道我也可以手动将PodB添加到MAIN_APP和FRAMEWORK_B,但是可以在Podfile中定义这种目标依赖吗?
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?
Btw,当时pod install
,我收到警告:
Btw, when pod install
, I got the warning:
如果此项目用于框架开发,则可以忽略此消息。否则,将目标添加到嵌入这些框架的Podfile中以使此消息消失(例如测试目标)。
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).
作为我知道,我可以使用嵌套目标作为主机目标,如:
As I know, I can use nested target for host targets like:
target :FRAMEWORK_A
target :MAIN_APP
end
end
因此CocoaPods将设置MAIN_APP以使用FRAMEWORK_A并从FRAMEWORK_A继承pod依赖项。但似乎我不能用多个依赖项来做到这一点:
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:
target :FRAMEWORK_A
target :MAIN_APP
end
end
target :FRAMEWORK_B
target :MAIN_APP
end
end
因为target:MAIN_APP不能被声明两次。
Because target :MAIN_APP cannot be declared twice.
是否有更好的解决方案,而不是将pod依赖项定义为Podfile中的函数并包含在所有目标?
Is there any better solutions instead of defining pod dependencies as a function in Podfile and include in all target?
推荐答案
这是一个很好的问题,我也遇到过类似情况。这是我的PodFile:
This is a great question and I've struggled with a similar situation. This is my 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
正如您所看到的,我没有使用框架,而是使用 abstract_target
将它们组合在一起。我希望这些依赖在CocoaPods中更容易实现。我知道这并没有真正回答你的问题,但它可能会有所帮助。
As you can see I'm not using frameworks and I use an abstract_target
to 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.
这篇关于如何将CocoaPods与多个Framework子项目一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!