Bazel is a new build system developed by Google which allows for an alternative to Gradle for building Android apps. In this guide, I'll show you how you can use Bazel to build your existing Gradle-based Android app.
Now we'll start converting over your build.gradle into Bazel's WORKSPACE and BUILD files. This guide will assume that your using Bazel 0.2.0, which, given the rate Bazel moves at, will most likely be outdated by the time you read this.
Build directory setup
The first thing you may have to do if you want to be able to build with both Gradle and Bazel is change the build output directory name that Gradle uses. You only need to do this if you're working on a machine with a case-insensitive file system (Windows / Mac OS). This is because Gradle uses "build" as the name for the directory and Bazel uses BUILD as the name for its build files.
This can be done by setting the buildDir property in your project level build.gradle as seen below.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
Basic Bazel setup for a Gradle Project
You'll need to start by creating two files, WORKSPACE and BUILD. If you went through the Bazel tutorial for building an Android app, these should seem familiar.
The WORKSPACE file should go in the root directory of your project and should look something like the following:
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
build_tools_version corresponds to buildToolsVersion in build.gradle, api_level corresponds to compileSdkVersion and path refers to your SDK directory, which should be the same as the sdk.dir property in local.properties.
The BUILD file should be created in the app directory if you're using the standard directory layout for Android Gradle projects.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
name is a property that will be used when building / running your project. custom_package should correspond to applicationId I your build.gradle. manifest, srcs and resource_files correspond to your AndroidManifest.xml, Java source files and resource files respectively. Note that the paths specified here are relative to the directory that the BUILD file is in.
Building with support library dependencies
The last thing you'll need in order to build an newly created Android project from Android Studio is the support library dependencies.
This can be done by including the libraries as deps in your BUILD file.
Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.
The naming may seem odd, but appcompat_v4 refers to support_v4.
Note that unlike Gradle, you can't exclude the appcompat_v4 dependency and expect it to be included because of appcompat_v7.
At this point it is now possible to build a new Android Studio project with Bazel, but you probably want to be able to do more than just that. Continue reading to find out how to include remote dependencies in your project.