Bazel for Android

Akshay Nandwana
Mar 8, 2020 · 4 min read

Bazel is an open-source build and test tool similar to Make, Maven, and Gradle.

Before starting with the Bazel, let’s see what a build tool is and how it is connected with Android.

As an Android Developer, we work on different types of files like Java, Kotlin, XML and also png, gif, jpg, vector and much more. But as a result, all we need at last is just .apk to distribute to users.

How come all these files get converted into one file? Android Studio uses Gradle to do all these complex work and gives .apk as an output. So, here Gradle is a build tool or we can say a Build System.

What Gradle is doing under the hood?

So, we understand, we need a build system to do all the automation, packing, testing and deploying and other processes for us and saves developers time and productivity hence increases.

What is Bazel?

Bazel is another build format just like Gradle, Maven or other existing build systems. So, what makes Bazel different?

This used internally at Google, means this build system used to build Google Maps, Gmail or other products. Bazel supports projects in multiple languages and builds outputs for multiple platforms. Bazel supports large codebases across multiple repositories and large numbers of users. This solves the scalability issue.

Bazel has more structured configuration files than any other build format.

Checkout out what thought Gradle team has about Bazel

Concepts and the Bazel Terminology

Workspace — This can be understood as a root folder consists of all your project files. Inside a workspace folder, it consists of a WORKSPACE text file in which we input Android Rules from GitHub and configure Android SDK Tools. Android Build Rules are written in the Starlark language.

BUILD — This file will describe the relationship among the android build outputs or class files and their dependencies. This is a text file written in the Starlark language.

Packages — This folder consists of the BUILD file is known as packages and the subfolders known as sub-packages.

android_library Rule — This rule helps Bazel to understand how to build an Android Library Module from the given source file and their dependencies.

android_binary Rule — This helps in making APK file.

Setup Bazel

Based on your platform you can follow the steps here to install Bazel on your machine.

For macOS, it requires only 4 steps;

A. Install XCode command-line tools and accept the license agreement

sudo xcodebuild -license accept

B. Download the Bazel — Check the latest version from here

bazel-<version>-installer-darwin-x86_64.sh

C. Run Bazel installer

chmod +x bazel-<version>-installer-darwin-x86_64.sh
./bazel-<version>-installer-darwin-x86_64.sh --user

D. Setup your environment — try echo $HOME or echo $PATH if you don’t know these

export PATH="$PATH:$HOME/bin"
bazel --version

Great, now you have a Bazel setup on your machine.

Build an Android App

Let’s clone the provided repository by Bazel and check the folders inside it

git clone https://github.com/bazelbuild/examples.gitcd examples/android/tutorial

Create a WORKSPACE text file. As we know we put our WORKSPACE file in the root folder, so here, we are putting out a file inside the tutorial folder.

Now create library modules. Go to src/main/java/com/example/bazel/ and create BUILD text file under it.

Now, we want to add the android_binary which builds the APK file. For this, we had to add one more top-level BUILD text file. Go to src/main and create. the file under it.

Great, we are ready to build our application. Go to the tutorial folder again on your terminal and run

bazel build //src/main:app

Or you can open this project inside Android Studio by following steps:

This will generate three folders:

  • bazel-bin stores .apk
  • bazel-genfiles stores intermediary source files that are generated by Bazel rules
  • bazel-out stores other types of build outputs

Or you can open this project inside Android Studio by following steps:

Now to run the project from here:

Open project level BUILD file > right-click on android_binary and click on run ‘main:app’

Congratulations 🎉We had created our first Android app using Bazel as our Build System. In the next article, we will be discussing how to bring external libraries in use and we will set up some architecture for our project.

If you find this reading useful, please click the 👏 button and share it to help others find it! Feel free to leave a comment 💬 below.
Have feedback? Let’s be friends on
Instagram.

MindOrks

Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store