Carthage 筆記

Journey on programming
3 min readJul 5, 2018

--

Reference

https://gist.github.com/weihanglo/97e949a9dbf92deb111999b6e42e9654
http://achangguru.blogspot.com/2017/12/ios-carthage.html

特色

非侵入性、去中心化、clean build 不會需要多花時間編譯第三方套件、Dynamic Framework

安裝指令

// 安裝前建議先更新 brew 的版本
brew update
// 透過 brew 安裝 carthage
brew install carthage

如果沒有安裝 brew 的話可以透過下列指令進行安裝

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

建立 Cartfile

在專案的根目錄底下,建立一個 Carthage 的資料夾,在裡面建立一個 Cartfile 的文件。
文件內容編寫模式跟 Podfile 很像,可以透過 github 關鍵字或是指定 git 網址來下載所需要的套件,也可以在後方去指定該套件的版本。

== 1.0 使用指定的版本
>= 1.0 使用指定或更高的版本
~> 1.0 使用1.0~2.0中的最新版本例如1.8

透過 Carthage 下載套件

確定 Cartfile 修改完成之後,只需要切換到該路徑,然後輸入下列指令,即可透過 Cartfile 當中所定義的項目來做安裝。

carthage update --platform iOS// 如果不需要指定特殊的平台來做編譯的話,直接執行下列指令就好
carthage update

編譯完成之後,會再原路徑底下出現 Build 跟 Checkouts 這兩個資料夾

  • Build : 裡面放的是編譯完成的套件
  • Checkouts: 裡面則是直接將遠端 repo 直接 clone 到本地做備份
  • Cartfile.resolved: 類似 podfile.lock 可以確定當前安裝的套件版本

建議可以把 Cartfile.resolved 也透過 git 來做版本控管,未來如果有需要取得跟專案相同套件,僅需要執行 carthage bootstrap 即可把全部相符的套件都一並下載下來了。

添加 framework 到專案中

從 Xcode -> Target -> General -> Linked Frameworks and Libraries 當中將剛剛 Carthage 編譯完成的 .framework 加入

修改 run script

在 app target -> Build Phases 下新增一個 New Run Script Phase

# 自動將 framework 複製到 target app 的 bundle中
/usr/local/bin/carthage copy-frameworks

並在 input files 中加入 frameworks 的相依路徑

ex:
$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework

carthage.private

我們可以額外定義 carthage.private 來針對開發時所需要 framework 來做安裝,該檔案的編輯方式跟 Cartfile 相同,但是寫在其中的 framework 不會被當成主專案的相依套件,所以只有在測試環境才可以做使用。這個我自己也沒有試過,網路上看到的資料也不多,先知道有這個東西可以使用就好。

--

--

Journey on programming

Software Developer at 91APP. If you like my articles, please clap and follow me on Medium. Never stay still, never plateau!