iOSアプリ開発でOSSのライブラリをCocoaPodsで管理する事が多いと思います。
CocoaPodsはライブラリのバージョン管理、チーム間共有の容易性に秀でたシステムでとても素晴らしいのですが、
アプリビルド時に管理しているライブラリを毎回ビルドするので、
管理するライブラリが増えるほどビルド時間が長くなるデメリットがあります。
そのデメリットをある程度カバーできるのが今回紹介する Cocoapods Binary です。
Cocoapods Binaryとは
CocoaPodsで管理されているライブラリをあらかじめビルドしておく事で、
毎回のビルド時間を短縮する事ができるプラグインです。
インストール方法
gemでインストールできます。
$ gem install cocoapods-binary
使用方法
Podfileに以下を追加します。
plugin 'cocoapods-binary'
次に、プレビルドを適用するライブラリを指定していきます。
- Podfile全てのライブラリに適用
all_binary!
をPodfileに追記
- 個別にライブラリに適用
- ライブラリの末尾に
, :binary => true
を追記- 例)
pod 'RealmSwift', :binary => true
- 例)
- ライブラリの末尾に
個別にライブラリ適用した場合はこの様な記述になります。
その後、 pod install
もしくは、 pod update
を実行します。
上記のPodfileを$ pod install
すると、通常のCocoaPodsのみでは以下の様なログが出ますが、
$ pod install
Analyzing dependencies
Downloading dependencies
Installing Alamofire (5.2.2)
Installing Realm (5.3.5)
Installing RealmSwift (5.3.5)
Generating Pods project
Integrating client project
Pod installation complete! There are 2 dependencies from the Podfile and 3 total pods installed.
CocoaPods + Cocoapods BInaryで行うと以下の様に
$ pod install
🚀 Prebuild frameworks
Analyzing dependencies
Downloading dependencies
Installing Realm (5.3.5)
Installing RealmSwift (5.3.5)
Generating Pods project
Prebuild frameworks (total 2)
Prebuilding Realm...
Prebuilding RealmSwift...
🤖 Pod Install
Analyzing dependencies
Downloading dependencies
Installing Alamofire (5.2.2)
Installing Realm (5.3.5)
Installing RealmSwift (5.3.5)
Generating Pods project
Integrating client project
Pod installation complete! There are 2 dependencies from the Podfile and 3 total pods installed.
と言う感じになり、PrebuildとInstallの2つのブロックでログが出力され、
binary適用したRealmはPrebuildが実行され、Installされているのに対して、AlamofireはInstallのみ実行された事が確認できます。
ビルド時間計測


58.221s -> 11.327s
ざっと5倍のスピードになっていますね!
しかも、今回はRealmのみに対応しましたが、Alamofireも対応したら更に高速化できます。
注意点
リソースを持っているライブラリはCocoapods Binaryを適用するとリソースが欠落してしまう制約?バグ?
があるので、リソースを持っているライブラリは素直に適用しないか、
Carthageに移すか検討した方が良さそうです。
まとめ
Cocoapods Binaryを適用すると簡単にビルド時間を圧縮できるので、
CocoaPodsによるビルド時間にお悩みの方は入れてみるのをお勧めします!
Cocoapods Binaryを適用した個人アプリをリリースしました。良ければインストールしてみてください🙏
