Building with Bazel
Jul 8 2022 · Starlark, Bazel 5.1, Visual Studo Code 1.66
Part 1: Learning Bazel
16. Use the Android Studio Plugin
Leave a rating/review
Episode 16 - Use the Android Studio Plugin
Bazel is a powerful building environment and while it may take some time to configure your build, once you have all the settings in place, you build system will scale with the size of your team. But as you develop your app, switching back and forth to the command line can take time.
The good news is Bazel has a plugin that works with Android Studio. This allows you to integrate Bazel directly into your workflow. When you switch to this plugin, Android Studio will switch to Bazel instead of using Gradle to compile your android code.
To get started, open up Android Studio. We’ll need to first install the Bazel plugin. To do this, open up Android Studio. Open Preferences. Look for plugins and then search for Bazel. Click the install button.
Okay, close your project. Now, we need to import our project into Bazel. From the project selection, look for the three buttons next to the open button. Click it and you’ll see a few options. You’ll see we have the option to Import a Bazel Project. Click this option.
At this point we get a few options to setup our bazelproject file. Since we haven’t imported our project using the Bazel plugin, let’s create an entirely new one. Now we get to configure our bazelproject file.
First, we’ll setup our directories. We’re using a monorepo, but we only want to import our Android project. Add the following:
We’ll keep the directories as is. That is, the period. We’ll also keep the directories set to true. Next, we’ll add our targets. Add the following:
For the additional languages, we’re need to designate that we’re using the Kotlin language. Comment out the Kotlin option. Finally, we also need to designate our platform. In our case, we’re using android-32.
Now we have our app configured. Now when the android studio starts up, you’ll notice that we have a Bazel menu option. We also have a Bazel heart placed in the menubar. The heart is the sync button. This will query Bazel and setup the IDE’s project structure to fit Bazel’s model.
Now if you run into problems, you can open the Bazel menu and select the Expand Sync to Working Set.
You can also edit your bazelproject file as well. Click the Bazel menu button and select the Project and Open Project View File button.
Now we want to configure our build target. Click the configuration and then the edit configurations button. Right now, we have one configuration. Select this configuration. You’ll see we can pass in some flags. Lets update our configuration to use our
dex configuration. Add the following in the Bazel flags.
You’ll also see native debugging which is critical to use breakpoints and the like. We’re just going to run this on the emulator without debugging, so uncheck both of these.
Now close out the dialog. Build and run the latest version and have it run on an emulator. And with that, we are working with Bazel integrated directly into Android Studio.
The Bazel plugin comes with other options as well, but this should get your started. For more information, check out the plugin’s documentation.