行文環(huán)境
- Xcode 10.1
- Carthage 0.31.2
Carthage
簡介
Carthage 是 swift 開發(fā)的代碼依賴工具,與Cocoapods直接引入第三方源代碼并修改 Xcode workspace 不同,Carthage 是將第三方庫預先 build 好的 framework 引入工程。這樣更適合大型項目,預先編譯好的 framework 不需要重新編譯。
快速使用
安裝
brew install carthage在工程目錄下創(chuàng)建
Cartfile在
Cartfile里添加依賴庫,比如github "Alamofire/Alamofire" ~> 4.7.2運行
carthage update會生成
Cartfile.resolved和 Carthage 文件夾 包括 Build 和 Checkout-
添加 framework 到工程,在 Build Phases 里新建 New Run Script Phase,輸入:
/usr/local/bin/carthage copy-frameworks在
Input Files項里填入$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework在
Output Files項里填入$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Alamofire.framework
常用命令
carthage update 更新并重新編譯 framework
carthage bootstrap 類似 update,一般用于新 checkout 的倉庫第一次運行 carthage
--platform iOS build 平臺參數
--cache-builds build 時使用緩存
carthage build --no-skip-current build private framework
Cartfile
引用github庫 github "Alamofire/Alamofire" ~> 4.7.2
指定具體tag github "malcommac/SwiftDate" == 4.5.1
指定分支 github "XiaomaniOS/RichEditorView" "xiaoman"
私有倉庫 git "ssh://git@git.xiaoman.co:7999/im/xmprotobuf.git"
本地倉庫 git "file:///Users/neilpa/code/project" "branch"
缺點
- 第一次 build 時間很長(喝杯咖啡先)
- CI 運行
carthage update費時,使用--cache-builds加快效率 - 沒有 Cocoapods 的各種其他功能
創(chuàng)建私有 framework
新建項目
新建 Cocoa Touch Framework 的項目
將 framework 的 scheme 設置成 shared,這里有個坑,新創(chuàng)建的項目 Xcode 不會生成 .xcscheme 文件,carthage 需要找這個文件來選擇哪些 scheme 需要 build,所以需要修改一下 scheme 讓 git 里生成.xcscheme 文件
設置 General -> Deployment Target
添加代碼
將源代碼加入工程,確保需要暴露的類等是 public 的,因為默認是 internal,外部無法識別
Objective-C代碼需要設置header為Public

私有 framework 如果有依賴庫可以使用 Carthage 添加,主工程使用私有 framework 時也會 build 出依賴的 framework
編譯
carthage build --no-skip-current
如果有錯參考 Resolve build failures
如果提示找不到 scheme,檢查上面提到的 .xcscheme 文件有沒有提交到 git
成功后打 git tag,Carthage 用 tag 來定位代碼
使用
用上面提到的方法加入到主工程即可,注意依賴庫也需要添加
Read More
Carthage
- Carthage/Carthage
- Carthage or CocoaPods: That is the question
- Creating a Private Framework With Carthage
- Local Git Repos?
- Nested Frameworks?
- bootstrap vs update?
- Github API rate limit reached when building iOS project with Carthage
- Five Steps to Migrate iOS Project from CocoaPods to Carthage
其他
- Dynamic Versus Static Framework in iOS
- Creating a Framework for iOS
- Getting Started with Reusable Frameworks for iOS Development
- JohnSundell/SwiftPlate
- Making a CocoaPod
- Improving Your Build Time in Xcode 10
- How the Zalando iOS App Abandoned CocoaPods and Reduced Build Time
- Building Objective-C Frameworks