Better Programming

Advice for programmers.

Follow publication

iOS Dev Tools

Building iOS apps faster using Bazel

Anurag Ajwani
Better Programming
Published in
10 min readApr 19, 2022
Photo by Loubna Aggoun on Unsplash

How to convert an iOS app project to use Bazel

1. Install Bazel

brew install bazel

2. Download the starter pack

cd $HOME
curl https://github.com/anuragajwani/bazel-ios-tut/archive/starter.zip -L -o starter.zip -s
unzip -q starter.zip
cd bazel-ios-tut-starter

3. Converting the project to use Bazel

.
├── README.md
├── SaladMaker
├── SaladMaker.xcodeproj
└── SayHelloKit
Targets within SaladMaker.xcodeproj
cat > WORKSPACE.bazel <<-EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_apple",
sha256 = "4161b2283f80f33b93579627c3bd846169b2d58848b0ffb29b5d4db35263156a",
url = "https://github.com/bazelbuild/rules_apple/releases/download/0.34.0/rules_apple.0.34.0.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()
EOF
cat > SayHelloKit/BUILD.bazel <<-EOF
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "SayHelloKit",
module_name = "SayHelloKit",
srcs = ["SayHello.swift"],
visibility = ["//visibility:public"],
)
EOF
mkdir SaladMaker/Sources
mkdir SaladMaker/Resources
mv SaladMaker/*.swift SaladMaker/Sources
mv SaladMaker/Assets.xcassets SaladMaker/Resources && mv SaladMaker/Preview\ Content SaladMaker/Resources
cat > SaladMaker/BUILD.bazel <<-EOF
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
load("@build_bazel_rules_apple//apple:resources.bzl", "apple_resource_bundle")
apple_resource_bundle(
name = "SaladMaker_Resoures",
bundle_id = "com.anuragajwani.SaladMaker_Resources",
resources = [
"Resources/Assets.xcassets",
"Resources/Preview Content"
],
visibility = ["//visibility:public"],
)
swift_library(
name = "Sources",
srcs = glob(["Sources/**"]),
deps = [
"//SayHelloKit:SayHelloKit"
],
visibility = ["//visibility:public"],
)
ios_application(
name = "SaladMaker",
bundle_id = "com.anuragajwani.SaladMaker",
deps = [
":Sources",
":SaladMaker_Resoures"
],
families = [
"iphone"
],
infoplists = ["Info.plist"],
minimum_os_version = "14.0",
visibility = ["//visibility:public"],
resources = [":SaladMaker_Resoures"]
)
EOF
bazel build //SaladMaker:SaladMaker
Building the SaladMaker app using Bazel

Debugging iOS apps built with Bazel

Broken file references in Xcode
xcodeproj generated with Tulsi

Remote caching build outputs in Bazel

bazel build --remote_cache=http://replace-with-your.host:port //SaladMaker:SaladMaker

When not to use Bazel

Summary

Final Thoughts

Want to Connect?For more on iOS development follow me on Twitter!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Anurag Ajwani
Anurag Ajwani

Written by Anurag Ajwani

Senior iOS Engineer at Travelperk. 7+ years experience with iOS and Swift. Blogging my knowledge and experience.