Android Bazel Java App Template

April 8, 2022 ยท View on GitHub

License Build Twitter Follow

Bazel + Android Studio + Robolectric + Espresso + Mockito + EasyMock/PowerMock

Technologies used:

Build Tools:

NameDescription
BazelBazel build system
Android SDKOfficial SDK
Android SDK Build ToolsOfficial Build Tools
Android Studio orOfficial IDE
IntellijIntellij IDE

Testing Frameworks:

NameDescription
EspressoInstrumentation Framework
RobolectricUnit Testing Framework

Getting Started:

Android Studio or Intellij Support (Simple):

  • Import/Open this project with Android Studio/Intellij
    1. Install the Bazel plugin.
    2. In the project selection dialog, click "Import Bazel Project".
    3. For the project view, select "Create from scratch".
    4. Click "Finish".

Building and Running

This project builds with Bazel, Bazel's Android rules, and the Android Build tools.

Build the APK:

$ bazel build //src/main:template_app

Install the APK:

$ bazel mobile-install //src/main:template_app

or:

$ bazel build //src/main:template_app && adb install bazel-bin/src/main/template_app.apk

Run the App:

$ bazel mobile-install //src/main:template_app --start_app

Note: Expect the first build to take a few minutes, depending on your machine, because Bazel's cache is clean. After Bazel downloads and builds the app once, subsequent builds will be much faster.

Testing

Running the Unit Tests:

The Junit and Robolectric tests run on the JVM, no need for emulators or real devices.

$ bazel test //src/test:all

Run a single unit test (android_local_test):

$ bazel test //src/test:play_services_utils_test

Get the list of all android_local_test targets:

$ bazel query 'kind(android_local_test, //src/test/...)'

Running the Instrumentation Tests:

The Espresso instrumentation tests run on the device. There is no need to launch an emulator, Bazel will do it automatically as part of the instrumentation test.

This is currently only supported on Linux.

$ bazel test //src/androidTest:main_activity_test

Read the Bazel docs here.