1. 程式人生 > >Carthage的安裝和使用,以及常見報錯解決

Carthage的安裝和使用,以及常見報錯解決

1、Carthage簡單概念介紹以及與Cocopods的區別:github地址 參考博文地址

  • Carthage 類似於 CocoaPods,為使用者管理第三方框架和依賴,但不會自動修改專案檔案和生成配置
  • Carthage 是去中心化的依賴管理工具,安裝依賴時不需要去中心倉庫獲取 CocoaPods 所有依賴的索引,節省時間
  • 對專案無侵入性,Carthage 設計上也比較簡單,利用的都是 Xcode 自身的功能,開發者在建立依賴時,相比 CocoaPods 也簡單許多
  • Carthage 管理的依賴只需編譯一次,專案乾淨編譯時,不會再去重新編譯依賴,節省時間,大大增加了Achive和Build的時間
  • 是由Swift語言編寫的,自動將第三方框架程式設計為 Dynamic framework( 動態庫 )
  • 與 CocoaPods 無縫整合,一個專案能同時擁有 CocoaPods 和 Carthage 
  • 缺點: 
    • 僅支援 iOS8 + 
    • 它只支援Framework,所以不能用來針對 iOS 8 以前的系統版本進行開發
    • 支援的 Carthage 安裝的第三方框架和依賴不如 CocoaPods 豐富
    • 無法在 Xcode 裡定位到原始碼,只可以看到一個庫的標頭檔案
    • 安裝包的大小比用CocoaPods安裝的包大
    • 每次update時,需要從git倉庫中重新拉取下載,比較耗時
  •  CocoaPods是現在主流的 iOS/Mac 的包管理工具
  • 使用方便,除編寫 Podfile 以外其他幾乎都是自動完成
  • 軟體包數量多,主流支援;
  • 缺點:
    • 每次更新環境都需要連線到中心倉庫,比較耗時 
    • 繁瑣的維護過程
    • 每次編譯都會把所有第三方庫都重新編譯一次

2、Carthage的安裝和使用

  • 使用homebrew進行安裝
    brew install carthage
  • 進入專案所在資料夾
    cd ~/路徑/專案資料夾
  • 建立一個空的Cartfile檔案
    touch Cartfile
  • 編輯Cartfile(標註需要哪些依賴庫對應版本或者Git分支)

1、方法一:開啟Xcode並編輯

open -a Xcode Cartfile

2、方法二:vim編輯,輸入I進入編輯模式,按esc離開,並輸入:wq儲存)

vim Cartfile
github "texturegroup/texture"
    • 依賴源 Dependency origin

Carthage支援兩種型別的源,一個是github,另一個是git。

github 表示依賴源,告訴Carthage去哪裡下載檔案。依賴源之後跟上要下載的庫,格式為Username/ProjectName

git 關鍵字後面跟的是資料庫的地址,可以是遠端的URL地址,使用git://, http://, ssh://,或者是本地資料庫地址。

    • 依賴版本號 Dependency Version

告訴Carthage使用哪個版本,這是可選的,不寫預設使用最新版本

== 1.0 表示使用1.0版本

>= 1.0 表示使用1.0或更高的版本

~> 1.0 表示使用版本1.0以上但是低於2.0的最新版本,如1.2,1.6

  • 執行Carthage
carthage update --platform iOS
TF013550:trunk name$ carthage update --platform ios

*** Fetching texture

*** Cloning SwiftyJSON

*** Cloning ObjectMapper

*** Fetching SDWebImage

*** Fetching PINCache

*** Cloning libwebp

*** Fetching PINOperation

*** Cloning libwebp

*** Checking out PINRemoteImage at "3.0.0-beta.6"

*** Checking out SDWebImage at "4.3.3"

*** Checking out ObjectMapper at "3.1.0"

*** Checking out SwiftyJSON at "4.0.0"

*** Checking out PINCache at "3.0.1-beta.2"

*** Checking out texture at "2.3.1"

*** xcodebuild output can be found in /var/folders/xh/0p311s3j6rl5nmm29_brfc340000gn/T/carthage-xcodebuild.W3tEmd.log

說明:

1、carthage會clone/fetch檔案中對應的git第三方庫,把每一個第三方庫編譯成二進位制檔案的framework檔案。

2、--platform iOS命令是可選的,目前每次更新時建議加上,作用是保證只為iOS編譯framework,如果不指定平臺,會為全平臺編譯framework檔案。

3、當命令執行完畢,在Cartfile檔案同級別的資料夾中生成一個名為“Carthage”資料夾和“Cartfile.resolved”檔案。開啟Carthage資料夾,可以看到兩個資料夾Build和Checkouts。

4、Cartfile.resolved 檔案用來跟蹤專案當前所用的依賴版本號,為了保持多端開發一致

  • 檢視carthage更多指令
carthage help

TF013550:trunk name$ carthage help

Available commands:

   archive           Archives built frameworks into a zip that Carthage can use

   bootstrap         Check out and build the project's dependencies

   build             Build the project's dependencies

   checkout          Check out the project's dependencies

   copy-frameworks   In a Run Script build phase, copies each framework specified by a SCRIPT_INPUT_FILE environment variable into the built app bundle

   fetch             Clones or fetches a Git repository ahead of time

   help              Display general or command-specific help

   outdated          Check for compatible updates to the project's dependencies

   update            Update and rebuild the project's dependencies

   version           Display the current version of Carthage

升級指定的FrameWork:

carthage update SVProgressHUD --platform iOS 
  • 新增Framework到專案中

1、新增到“Linked Frameworks and Libraries”,點選加號,從/Carthage/Build/iOS資料夾中選擇Framework新增

2、新增一個新的“Run Script”,並新增命令

說明:carthage copy-frameworks 命令剔除了額外的框架

/usr/local/bin/carthage copy-frameworks

點選Input Files下面的+號為每一個framework新增條目

$(SRCROOT)/Carthage/Build/iOS/MJRefresh.framework
build phase對專案執行來說不是必須的,但是,這個配置解決了APP因為使用的frameworks包含二進位制影象的iOS模擬器在提交APP Store時會被自動拒絕的問題。

3、Carthage安裝過程中的報錯(持續更新中......)

TF013550:trunk name$ carthage update --platform ios

*** Fetching texture

*** Cloning WQPhotoAlbum

*** Cloning SDWebImage

A shell task (/usr/bin/env git clone --bare --quiet https://github.com/rs/SDWebImage.git /Users/yuge/Library/Caches/org.carthage.CarthageKit/dependencies/SDWebImage) failed with exit code 128:

error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54

fatal: The remote end hung up unexpectedly

fatal: early EOF

fatal: index-pack failed

1、列印當前xcode的路徑:

xcode-select --print-path

2、輸入一下命令:(如果有多個xcode版本路徑 請選擇其中一個)

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
TF013550:trunk name$ xcode-select --print-path

/Applications/Xcode.app/Contents/Developer

TF013550:trunk name$ sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

Password:

TF013550:trunk name$ gem update --system
  • 錯誤二:執行carthage build --platform ios報錯如下:

** Skipped downloading ObjectMapper.framework binary due to the error:

"API rate limit exceeded for 115.236.71.65. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"