Building with Bazel

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

Part 1: Learning Bazel

14. Incorporate a BazelRC File

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Write an Android Build File Next episode: 15. Run Unit Tests
Transcript: 14. Incorporate a BazelRC File

Episode 14 - Incorporate a BazelRC File

When you have a build that takes a few options, you create a bazelrc file and use it to pass configuration options to your build. The bazelrc is a hidden file called .bazelrc. You can provide your build options in that file which will be applied to all build but you can also designate flags for different configurations. That way, you can choose a configuration at build time.

Bazel looks for the .bazelrc in three different locations. First, on macOS, linux and other unix variants, it will search in the etc directory. On windows, it searches in the ProgramData The next place is in your workspace directory right next to your workspace. The final place is in your home folder on unix based systems and in your user profile directory on windows. Alternatively, you can also pass in a bazelrc file.

To get started, navigate to your workspace file. Create an .bazelrc file. We’ll first add our flags to the build file itself.

Add the following:

build --define=android_dexmerger_tool=d8_dexmerger 
build --nouse_workers_with_dexbuilder 

Now lets try this out. Build the app.

bazel build //Bullseye-Android/app/src:yourfirstapp

This time our build runs fine because all the flags are added. But this is true for all the builds in the monorepo so these flags don’t make much sense when building an iOS app.

Instead, we can pass it to the bazelrc file. Update bazelrc to the following:

build:dex --define=android_dexmerger_tool=d8_dexmerger 
build:dex --nouse_workers_with_dexbuilder 

Now we’ve create a dex configuration. To run it, we simply pass in the configuration using the config flag. To show that the config file is being used, lets clean the build.

bazel clean

Just a note about bazel clean. Each time you run your build, Bazel will analyze your code, looking for any changes. It will then compile those affected targets. This means, you will rarely ever need to do a clean. In fact, you’ll only be increasing your compile times.

Finally, we run the build using the dex configuration.

bazel build //Bullseye-Android/app/src:yourfirstapp --config=dex

Mind you, you can do other things with the bazelrc files such as specify startup options, you can import other rc files, and so forth. For more information, see the documentation.