Skip to content

Oppia Bazel Setup Instructions

Ben Henning edited this page May 28, 2024 · 6 revisions

Table of Contents

Overview

Bazel is an open-source build and test tool similar to Make, Maven, and Gradle. It uses a human-readable, high-level build language.

WARNING: We recommend to not use the Android Studio Bazel plugin since it currently has compatibility issues with the project.

Installation

  1. Download and Install Java 8 using the links from the Java website.

  2. Select your Operating System for instructions on setting up Bazel:

Building the app

After the installation completes you can build the app using Bazel.

Move your command line head to the ~/opensource/oppia-android, then run the below bazel command:

bazel build //:oppia

Building + installing the app

bazel build //:oppia && adb install -r bazel-bin/oppia.apk

Running specific module (app) Robolectric tests

bazel test //app/...

Running all Robolectric tests (slow)

bazel test //...

Known Issues and Troubleshooting

See our troubleshooting wiki page for some known issues with Bazel, and the corresponding troubleshooting steps.

Concepts and Terminology

Workspace
A workspace is a directory where we add targeted SDK version, all the required dependencies and there required Rules. The directory containing the WORKSPACE file is the root of the main repository, which in our case is the oppia-android root directory is the main directory.

Packages
A package is defined as a directory containing a file named BUILD or BUILD.bazel.

Binary rules
A rule specifies the relationship between inputs and outputs, and the steps to build the outputs. In Android, rules are defined using android_binary. Android rules for testing are android_instrumentation_test and android_local_test.

BUILD files
Every package contains a BUILD file. This file is written in Starlark Language. In this Build file for module-level, we generally define android_library, kt_android_library to build our package files as per the requirement.

Dependencies
A target A depends upon a target B if B is needed by A at build. A -> B

deps = [ "//app",]

Here, deps is used to define the dependencies which is a type of dependencies called deps dependencies and it includes the files/directory/target which are dependent. From the above example the dependency is the app target which is defined in the Build file of app package.

Example of Dependencies

  1. srcs dependencies
  2. deps dependencies

Loading an extension
Bazel extensions are files ending in .bzl. Use the load statement to import a symbol from an extension.

load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library")

Here, we are loading android.bzl and we are going to use it with a symbol name kt_android_library. Arguments to the load function must be string literals. load statements must appear at top-level in the file.

Visibility of a file target
With the example from our codebase, target app whose visibility is public.

  • visibility = ["//visibility:public"], - Anyone can use this target.
  • "//visibility:private" - Only targets in this package can use this target.

Testing
when we want to run test cases on Bazel build environment, we usually pass arguments related to test which app_test.bazl required to run our test.

app_test(
    name = "HomeActivityLocalTest",
    srcs = ["src/test/java/org/oppia/android/app/home/HomeActivityLocalTest.kt"],
    test_class = "org.oppia.android.app.home.HomeActivityLocalTest",
    deps = TEST_DEPS,
)
Clone this wiki locally