Building with Bazel
Part 1: Learning Bazel Create a Workspace
— iOS & Swift, Android & Kotlin

Lesson Complete

Play Next Lesson

Create a Workspace

This video was last updated on Jul 8 2022

Create a new workspace for your iOS app, by incorporating several special rules to written by the community.


Episode 9 - Create a Workspace


In the last episode, we moved the JokeGenerator to the root level of the monorepo. The workspace was empty since we didn’t need to do anything outside of compiling the program, so we can delete all the existing content.

Now let’s write the rules for building our iOS app. We need to incorporate a few rules into the workspace when building an iOS app.

Now if we head over Bazel’s official repository, you’ll find that we have a ton of rules for different languages and environments. We’re interested in the rules_apple repository since that allows us to compile code for Apple devices. But also, we’re interested in the rules_swift. That allows us to compile our Swift code. By having Swift as a separate target, we can use Bazel to compile Swift for other Swift supported platforms.

Okay, back to our Workspace. First, lets add a comment for the iOS app.

# Rules for building the Bullseye for iOS

There are a few ways to handle dependencies which we will cover soon enough. For now, we just want to includes the rules as a download. To do this, we can use the http archive function. This is included with bazel tools.

Add the following:


Here are accessing the http rules file included with bazel. Yet, we want to access the http_archive function, so we must designate it after the rules address.

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

This imports the http_archive function and we access it using the variable http_archive. Now lets get our swift rules.



First lets provide a name that we can use to reference.

    name = "build_bazel_rules_swift",

Next, we’ll provide the SHA value. This ensures we are using the correct file. If the file changes, we’ll receive a different SHA value resulting in a build failure. For the sake of accuracy, I’m pasting in the SHA. If you are following along, make sure to pause the video.

sha256 = "a2fd565e527f83fb3f9eb07eb9737240e668c9242d3bc318712efa54a7deda97",

Finally, let’s provide the url for the rules.

url = "",

The http archive just downloads, extracts the rules and makes the targets available. We need to run two specific functions to load the dependencies. Open up GitHub repo for the swift rules.

The rules are contained in the swift subfolder. Click on it. Then click on repositories.bzl. The first function is called swift_rules_dependencies. This is located in the repositories.bzl file. If you scroll down, you’ll see the swift_rules_dependencies function. We can reference this from the load method.



To get repositories.bzl, we reference the label we defined in the previous load statement.


This was in the swift folder in the repositories.bzl file.


Finally, we need to load the swift_rules_dependencies method.


Finally, we’ll call the function.


We’ll do the same for swift rules extra dependencies. We get this from the extras.bzl file then call it.



That allows to compile Swift. Now comes the Apple rules. First, we’ll get the rules much like we did for Swift.

    name = "build_bazel_rules_apple",
    sha256 = "a5f00fd89eff67291f6cd3efdc8fad30f4727e6ebb90718f3f05bbf3c3dd5ed7",
    url = "",

Next, we’ll load the apple_rules_dependencies function.


Finally, we’ll call the apple_rules_dependencies.


Congrats! We have all the resources set to run build iOS apps. Now we have to write the rules for our Android app except before we start, you should have a good understanding of dependencies work with Bazel.