【Cocoapods Binary】Cocoapodsのビルド時間を爆速化する

Cocoapods爆速化Tech
この記事は約5分で読めます。

iOSアプリ開発でOSSのライブラリをCocoaPodsで管理する事が多いと思います。

CocoaPodsはライブラリのバージョン管理、チーム間共有の容易性に秀でたシステムでとても素晴らしいのですが、

アプリビルド時に管理しているライブラリを毎回ビルドするので、

管理するライブラリが増えるほどビルド時間が長くなるデメリットがあります。

そのデメリットをある程度カバーできるのが今回紹介する Cocoapods Binary です。

Cocoapods Binaryとは

GitHub - leavez/cocoapods-binary: integrate pods in form of prebuilt frameworks conveniently, reducing compile time
integrate pods in form of prebuilt frameworks conveniently, reducing compile time - GitHub - leavez/cocoapods-binary: integrate pods in form of prebuilt framewo...

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のみでは以下の様なログが出ますが、

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で行うと以下の様に

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のみ実行された事が確認できます。

ビルド時間計測

CocoaPodsのみ
cocoapods-original-result

CocoaPods + Cocoapods Binary
cocoapod-binary-result

58.221s -> 11.327s

ざっと5倍のスピードになっていますね!

しかも、今回はRealmのみに対応しましたが、Alamofireも対応したら更に高速化できます。

注意点

リソースを持っているライブラリはCocoapods Binaryを適用するとリソースが欠落してしまう制約?バグ?

Bug: Removes Copy Pods Resources build phase when using cache · Issue #115 · leavez/cocoapods-binary
I have this problem with the GoogleTagManager pod, but I think it's a general problem with pod specs that define a .bundle in "resources": When running pod inst...

があるので、リソースを持っているライブラリは素直に適用しないか、

Carthageに移すか検討した方が良さそうです。

まとめ

Cocoapods Binaryを適用すると簡単にビルド時間を圧縮できるので、

CocoaPodsによるビルド時間にお悩みの方は入れてみるのをお勧めします!

Cocoapods Binaryを適用した個人アプリをリリースしました。良ければインストールしてみてください🙏

‎Scannable - Simply Recognizer
‎Scannable - Mobile Scanner App. 無料 スキャンアプリ / 書類 スキャン / OCR / 写真 スキャン シンプル操作で画像からテキスト解析! 教科書や手紙、レシートをデジタルデータにして保存できます。 データ化したテキストは編集・共有可能です。 日本語・英語・中国語(簡体字)...

タイトルとURLをコピーしました