Building with Bazel

Jul 8 2022 · Starlark, Bazel 5.1, Visual Studo Code 1.66

Part 1: Learning Bazel

11. Add Dependencies

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Understand Dependencies Next episode: 12. Write an iOS Build File
Transcript: 11. Add Dependencies

Episode 11 - Add Dependencies

In our workspace, we’re going to use maven as a way to resolve our dependencies. We’re going to use maven install to grab all the various artifacts. We simply provide the artifacts we want to get as well as the versions. In this case, will just be fetching the artifacts.

Demo

To get started, open the WORKSPACE in your editor of choice. We’re going to write the rules to create our android app. Let’s add a comment at the bottom.

# Rules for building Bullseye for Android

We’ll get started by loading the android SDK. In my case, I have the build tools 30.0.0.2 installed. You can add build tools by way of the SDK manager in Android Studio.

android_sdk_repository(
    name = "androidsdk",
    api_level = 32,
    build_tools_version = "30.0.2"
)

Next, we’ll get the android_tools.

http_archive(
    name = "android_tools",
    sha256 = "ed5290594244c2eeab41f0104519bcef51e27c699ff4b379fcbd25215270513e",
    url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.23.0.tar.gz",
)

Next we need to get android rules.

http_archive(
    name = "rules_android",
    urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
    sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
    strip_prefix = "rules_android-0.1.1",
)

Of course, building an android app we need to load the rules for the JVM. First, we’ll define a couple of variables.

RULES_JVM_EXTERNAL_TAG = "2.4"
RULES_JVM_EXTERNAL_SHA = "2393f002b0a274055a4e803801cd078df90d1a8ac9f15748d1f803b97cfcdc9c"

Next, we’ll get the actual JVM rules.

http_archive(
    name = "rules_jvm_external",
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    sha256 = RULES_JVM_EXTERNAL_SHA,
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

Now that we have the JVM ready go, we need to get a reference to the maven install function. We’ll do this by way of load.

load("@rules_jvm_external//:defs.bzl", "maven_install")

At this point, we can now add all our dependencies. In the maven install function, we’ll pass in an array of artifacts.

maven_install(
    artifacts = [

    ]
)

Now we’ll list all our artifacts. There’s a lot of them.

artifacts = [
    "androidx.core:core-ktx:1.3.2",
    "androidx.core:core:1.3.2",
    "androidx.appcompat:appcompat:1.4.1",
    "com.google.android.material:material:1.5.0",
    "androidx.constraintlayout:constraintlayout:2.1.3",       
    "androidx.lifecycle:lifecycle-runtime:2.2.0",
    "junit:junit:4.13.2",
    "androidx.test.ext:junit:1.1.3",
    "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1",
    "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1",
    "androidx.test.espresso:espresso-core:3.4.0",
    "androidx.lifecycle:lifecycle-runtime:2.2.0",
    "androidx.lifecycle:lifecycle-viewmodel:2.2.0",
    "androidx.lifecycle:lifecycle-common:2.2.0",
    "androidx.databinding:viewbinding:7.0.0-alpha02",
],

After which, we then define all the various repositories.

repositories = [
    "https://maven.google.com",
    "https://jcenter.bintray.com",
    "https://repo1.maven.org/maven2",
],

Finally, we set fetch sources to true to download the dependencies.

fetch_sources = True,

Believe it or not, we still have a little more code to write. The app is written in Kotlin so we must get ahold of the Kotlin rules.

rules_kotlin_version = "legacy-1.4.0-rc4"
rules_kotlin_sha = "9cc0e4031bcb7e8508fd9569a81e7042bbf380604a0157f796d06d511cff2769"

http_archive(
    name = "io_bazel_rules_kotlin",
    urls = ["https://github.com/bazelbuild/rules_kotlin/releases/download/%s/rules_kotlin_release.tgz" % rules_kotlin_version],
    sha256 = rules_kotlin_sha,
)

Finally, we need to get ahold of a few functions that need to be called. Add the following:

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
kotlin_repositories()
kt_register_toolchains() 

And that’s it. We have our Workspace all setup for both our monorepo. Before we can build, we actually need to write our build file which is what we’ll do in the next episode.