Prerequisites:
Before getting
started, we have to make sure the following
prerequisites are met:
A
computer running Linux or macOS.
The
latest version of Bazel installed on local machine. This
can be downloaded it from the official website: https://bazel.build/.
The
latest version of the Android SDK and Android NDK installed on local machine. Can
be downloaded from the official website: https://developer.android.com/studio#downloads.
Step 1: Set up a
new Android project
First, we need
to create a new Android project. We can do this using Android Studio, which is
an official IDE for Android development. Just open Android Studio and create a
new project. Choose the project name, package name, and other settings
according to our requirements.
Step 2: Add
Bazel support to Android project
Once a new
Android project is created, I need to add Bazel support to it. To do this, a
new file named WORKSPACE in the root directory of the project ought to be
created. This file is used by Bazel to define the project and its dependencies.
Added the
following code to the WORKSPACE file:
android_sdk_repository(
name = "androidsdk",
api_level =
28,
build_tools_version
= "28.0.3",
path =
"/path/to/android-sdk",
)
android_ndk_repository(
name = "androidndk",
path =
"/path/to/android-ndk",
api_level =
28,
)
Replace
/path/to/android-sdk and /path/to/android-ndk with the actual paths of
Android SDK and Android NDK installations.
Step 3: Create a
BUILD file
Next, created a
new file named BUILD in the root directory of the project. This file is used
to define the targets that I want to build with Bazel.
Added the
following code to the BUILD file:
android_binary(
name = "app",
srcs = glob(["src/main/java/**/*.java"]),
resource_files
= glob(["src/main/res/**"]),
manifest = "src/main/AndroidManifest.xml",
custom_package
= "com.example.myapp",
multidex = "native",
deps = [
"//third_party:androidx",
"//third_party:google",
"//third_party:okhttp",
],
)
This code
defines an Android binary target named app. The target includes the source
files, resources, and manifest file of
Android project. It also specifies the package name of the app and
includes the dependencies that I want to build with my project.
Step 4: Add
third-party dependencies
If project uses any third-party libraries or frameworks, it
needs to be added to the Bazel project. To do this, a new directory named
third_party in the root directory ought to be created for the project.
Next, a new file
named WORKSPACE in the third_party directory should be created. Add the
following code to the WORKSPACE file:
maven_jar(
name = "androidx",
artifact = "androidx.appcompat:appcompat:1.2.0",
repository = "https://maven.google.com",
)
maven_jar(
name = "google",
artifact = "com.google.android.material:material:1.2.0",
repository = "https://maven.google.com",
)
maven_jar(
name = "okhttp",
artifact = "com.squareup.okhttp3:okhttp:4.9.1",
repository = "https://repo1.maven.org/maven2/",
)
This code
defines the third-party dependencies that this project uses. In this example, I
have added three dependencies: androidx.appcompat, com.google.android.material,
and com.squareup.okhttp3.
Step 5: Build
your Android project with Bazel
Once I have set
up the project with Bazel and added the necessary dependencies, I can build the
project using the following command:
bazel build //:app
This Command
will build the app target that I defined in the BUILD file. The output APK
file will be generated in the bazel-bin/app.apk directory.