我正在尝试为命令行工具构建RxSwift,但与使用iOS App相比,它要困难得多。
我创建了一个新的命令行项目,并用pod
安装了RxSwift
$ cat Podfile
# Podfile
use_frameworks!
target 'HelloRx' do
pod 'RxSwift', '~> 4.0'
end
$ pod --version
1.5.3
XCode 10.1
打开工作区(.xcworkspace)后,没有添加任何代码,项目构建良好,但运行时崩溃:
dyld: Library not loaded: @rpath/RxAtomic.framework/Versions/A/RxAtomic
Referenced from: /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx
Reason: image not found
动态依存关系对二进制文件不可见。
$ otool -l HelloRx | grep -A 2 RPATH | grep path
path @executable_path/../Frameworks (offset 12)
path @loader_path/Frameworks (offset 12)
path @executable_path/../Frameworks (offset 12)
path @loader_path/Frameworks (offset 12)
XCode假定二进制文件可以在Frameworks
目录中找到相对于二进制文件的框架。不幸的是,如果我查看构建目录,则没有Frameworks
目录,因此会出错。
$ ls
HelloRx Pods_HelloRx.framework RxCocoa
HelloRx.swiftmodule RxAtomic RxSwift
$ ls ..
Debug
为使混乱,所有框架都复制到自己的Rx*
目录中,而不是一个全局Frameworks
目录中。
我可以通过在“构建设置” >>“运行路径搜索路径”中添加更多路径来解决此问题。
'@executable_path/RxAtomic'
'@executable_path/RxSwift'
我做到了,但是二进制文件仍然崩溃。
dyld: Library not loaded: @rpath/libswiftAppKit.dylib
Referenced from: /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/RxSwift/RxSwift.framework/Versions/A/RxSwift
Reason: image not found
现在是缺少libswiftAppKit.dylib
的RxSwift。
这可以通过添加另一个Runpath路径来“修复”。
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx'
这终于停止了崩溃,但是我的应用程序引发了很多警告:
objc[64025]: Class _TtCE6AppKitVSo17NSAnimationEffectP33_9E6F1C1DB126EBCC5B18B8BAC8A387CC26_CompletionHandlerDelegate is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftAppKit.dylib (0x101360b98) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059a250). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC8Dispatch16DispatchWorkItem is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftDispatch.dylib (0x101a7c6d0) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059bd28). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC10FoundationP33_45BFD3D387700B862E3A7353B97EF7ED20_CharacterSetStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib (0x101c34f00) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059d5e8). One of the two will be used. Which one is undefined.
objc[64025]: Class _TtC10Foundation12_DataStorage is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftFoundation.dylib (0x101c34fa8) and /Users/luke/Library/Developer/Xcode/DerivedData/HelloRx-ftbkqquhoytidfesyxazbaovndbu/Build/Products/Debug/HelloRx (0x10059d690). One of the two will be used. Which one is undefined.
...
我可以忍受警告,但这显然不是正确的解决方案。那让我觉得,解决这个问题的正确方法是什么?
(我是XCode和Swift的新手,所以也许我在做疯狂的事情)
答案 0 :(得分:1)
一种快速的方法可能是将所有pods框架都设置为静态库。
Mach-O Type
更改为Static Library
。这会将所有广告连播更改为Mach-O Type
为Static Library
。 (每次运行pod install
时,它都会被改回,因此您可能不得不再次执行此操作)此方法的优点是您的输出将是单个可执行文件。
如果要使用动态框架,请遵循本教程: https://medium.com/livefront/how-to-add-a-dynamic-swift-framework-to-a-command-line-tool-bab6426d6c31。 通过使用第二种方法,您的输出将不仅是单个可执行文件,而且还必须提供所有动态框架。