用 Bazel 來建構 iOS App

·

3 min read

一、什麼是 Bazel

Bazel 是 Google 開發的免費建構軟體的工具,通常用在軟體的建構與測試上。詳細的官方使用說明可以去直接去官網參考:bazel.build

二、為何要用 Bazel 來建構 iOS App

在開發 iOS App 使用 Xcode 就已經可以滿足所有需求,從開發、建構到測試。但在更大型的 App 的建構,將所有功能模組化開發,而在最後整合打包的時候使用 Bazel 會是更方便的選擇。

本篇文章將會說明如何利用 Bazel 來建構 iOS App,而這邊打包的 iOS App 範例是一個空白的專案,讓初學者更容易學習這個工具。

三、結論

先說結論,看完這篇可以學習到如何使用 Bazel 來將 iOS 的專案打包成 .ipa 檔案,可以直接安裝在手機上執行。

四、步驟說明

1 安裝 Xcode

直接到 App Store 下載安裝

2 安裝 brew

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3 安裝 Bazel

$ brew install bazel

4 創建一個新的 iOS App 專案

  • 本範例使用 Xcode14.0 來創建
  • 使用 objective-C
  • 專案名稱為 App (可自由命名)
  • 在專案內的 Storyboard 建置一個 Label ,上面顯示 Hello App (可自由創作)

5 在 App 專案內的 Info.plist 新增 key

  • CFBundleVersion
  • CFBundleShortVersionString
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundleShortVersionString</key>
<string>1</string>
  • 因為 Xcode 14.0 建立專案不會有這兩個 key,用 Bazel 會需要這兩個 key

6 建立工作資料夾

$ mkdir iOS
  • 資料夾名稱可自由命名

7 將 App 專案複製到工作資料夾內

所以工作資料夾的樹狀結構:

iOS
 - /App/...
 - App.xcodeproj

8 建立 WORKSPACE 檔案

$ touch WORKSPACE

9 更新 WORKSPACE 檔案

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "build_bazel_rules_apple",
    sha256 = "a19cf84dd060eda50be9ba5b0eca88377e0306ffbc1cc059df6a6947e48ac61a",
    url = "https://github.com/bazelbuild/rules_apple/releases/download/1.1.1/rules_apple.1.1.1.tar.gz",
)

load(
    "@build_bazel_rules_apple//apple:repositories.bzl",
    "apple_rules_dependencies",
)

apple_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:repositories.bzl",
    "swift_rules_dependencies",
)

swift_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:extras.bzl",
    "swift_rules_extra_dependencies",
)

swift_rules_extra_dependencies()

load(
    "@build_bazel_apple_support//lib:repositories.bzl",
    "apple_support_dependencies",
)

apple_support_dependencies()

10 建立 BUILD 檔案

$ touch BUILD
  • 讓 Bazel 讀取建構作業流程

11 更新 BUILD 檔案

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")

objc_library(
    name = "Lib",
    srcs = glob([
        "App/*.h",
        "App/*.m",
    ]),
    data = [
        "App/Base.lproj/LaunchScreen.storyboard",
        "App/Base.lproj/Main.storyboard",
    ],
)

ios_application(
    name = "App",
    bundle_id = "com.example.app",
    families = ["iphone", "ipad"],
    infoplists = [":App/Info.plist"],
    minimum_os_version = "15.0",
    deps = [":Lib"],
)

所以至此,工作資料內共有這些檔案:

iOS
    - /App/...
    - BUILD
    - WORKSPACE
    - App.xcodeproj

12 建構應用程式

直接在工作目錄底下:

$ bazel build App

成功會產生 .ipa

INFO: Analyzed target //:App (1 packages loaded, 12 targets configured).
INFO: Found 1 target...
Target //:App up-to-date:
  bazel-out/applebin_ios-ios_x86_64-fastbuild-ST-591a3331a341/bin/App.ipa
INFO: Elapsed time: 5.766s, Critical Path: 5.05s
INFO: 20 processes: 7 internal, 9 darwin-sandbox, 4 local.
INFO: Build completed successfully, 20 total actions

如果這一步發生失敗,確認 Xcode 跟 Xcode command line 已經安裝,並且執行:

$ bazel clean --expunge

此輸出只能在模擬器上執行,如果要在實體機上執行,需要填入 provisioning_profile

13 填入 provisioning_profile

load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")

objc_library(
    name = "Lib",
    srcs = glob([
        "App/*.h",
        "App/*.m",
    ]),
    data = [
        "App/Base.lproj/LaunchScreen.storyboard",
        "App/Base.lproj/Main.storyboard",
    ],
)

ios_application(
    name = "App",
    bundle_id = "com.example.app",
    families = ["iphone", "ipad"],
    infoplists = [":App/Info.plist"],
    minimum_os_version = "15.0",
    deps = [":Lib"],
    provisioning_profile = "provisioning_profile.mobileprovision",
)
  • 個人的 provisioning_profile 可以在 ~/Library/MobileDevice/Provisioning/Profiles 找到,並且複製到工作資料夾內

14 建構應用程式

$ bazel build App --ios_multi_cpus=armv7,arm64

成功會產生 .ipa

INFO: Analyzed target //:App (1 packages loaded, 13 targets configured).
INFO: Found 1 target...
Target //:App up-to-date:
  bazel-out/applebin_ios-ios_armv7-fastbuild-ST-3d7961f14ea2/bin/App.ipa
INFO: Elapsed time: 5.435s, Critical Path: 5.20s
INFO: 24 processes: 9 internal, 10 darwin-sandbox, 5 local.
INFO: Build completed successfully, 24 total actions

15 在裝置上安裝 .ipa

  • 啟動 Xcode,在方方工作列選取 Windows > Devices and Simulators
  • 在左側清單中選取已連接的裝置
  • 然後在 INSTALLED APPS 按加號按鈕,然後選取 .ipa,會自動安裝到手機上

以上步驟可以直接下載範例專案試用:iOS

五、參考