28

I was reading about the Android Testing Samples project and noticed that a "new" build tool named Bazel is being used for building Android projects:

Experimental Bazel Support

Some of these samples can be built with Bazel on Linux. These samples contain a BUILD.bazel file, which is similar to a build.gradle file. The external dependencies are defined in the top level WORKSPACE file.

This is experimental feature. To run the tests, please install the latest version of Bazel (0.12.0 or later) by following the instructions on the Bazel website.

Then,

  • What is the added advantage of using Bazel over the existing Gradle?
  • Is it really good to have TWO build tools for Android?
  • Does it mean that Android developers will probably need to learn this new build tool in future?

Update #1: Updated with accepted answer from farhana and thanks to Jin for the in depth details about the google build systems.

Update #2: Updated with google code labs for bazel android introduction.

beigirad
  • 4,986
  • 2
  • 29
  • 52
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 2
    Yes, looking for the same. Just for reference - https://stackoverflow.com/questions/29245787/what-are-the-differences-between-bazel-and-gradle – jignesh.world Jan 03 '19 at 06:13
  • 1
    Does Google has a road map of migrate Android development from Gradle to Bazel? – shizhen Jan 04 '19 at 01:53
  • 1
    @shizhen No, but there are [codelabs](https://codelabs.developers.google.com/codelabs/bazel-android-intro/index.html#0) for you to try it out. – Jin Jan 09 '19 at 17:30
  • Android Gradle Plugin. Personally waiting for it to become Android Bazel Plugin in the future – mochadwi Oct 21 '21 at 08:15

1 Answers1

17

I'm the author of the Bazel support in that repository, and I work on the Bazel Android rules team. farhana has written an amazing answer already, but I just want to add on a few points:

  • Bazel is the build system used internally at Google, and that means it's the same build system used to build Google apps like YouTube, Gmail, and Photos.
  • Some of these Google apps are huge, and will take Bazel very long to build them locally. Therefore, there's a remote execution feature in Bazel (on top of a remote cache like Gradle has, but more fine-grained IIUC) that allows you to fan out your build actions to a remote farm of executors. Bazel has a static and hermetic dependency graph of actions that is designed to be fully reproducible, with each action having a fully declared set of inputs, outputs, environment variables, command lines, and additional knowledge within Bazel about configuration, toolchains and host/target/execution platform. With this graph, Bazel can safely farm out actions to remote workers, enabling very large degrees of parallelism. I have successfully built the Tensorflow Android app with 500-800 parallel jobs on Google Cloud Remote Build Execution before.
  • Automated test discovery. Bazel's static graph also encodes the dependencies between tests, binaries/libraries and data, so making a change in your source files automatically invalidates all tests that depend on them. This tells Bazel to rerun the tests, and skip the cached ones if they do not need to run again. And yes, you can use remote execution with tests too, so you can run hundreds to thousands of tests at the same time.
  • Bazel can build many languages: C++, Go, Java, Scala, Rust, and even Haskell. It can also build clients like iOS and Angular/TypeScript/JavaScript. This means that you don't need a separate tool like CMake to build your NDK and C++ code. You can use the same build tool to build both front and backend, and run integration tests together.
  • Bazel has a built in command called mobile-install, which allows for fast and iterative development of Android apps. It builds your app, shards out native libs, dexes and resource files, and only pushes changed shards to the device to reduce build and deploy times. Read more about it here.

Is it really good to have TWO build tools for Android?

There are more than just two: Buck & Pants are two other popular build systems for Android. Each build system has its pros and cons, and are designed and implemented to solve a particular set of requirements. For example, Bazel (and its surrounding ecosystem of tools) was born out of Google's gigantic monorepo, so it solves scalability issues really well.

Having the option to try out different approaches, IMO, is a good thing.

Does it mean that Android developer probably need to learn this new build tools in future?

Bazel is open source and has support in Android Studio via the Bazel plugin. If you think it looks interesting, feel free to try it out! We're still in early stages of adapting the Android rules to work in the open source world, so expect some features to be work-in-progress for the time being.

Jin
  • 12,748
  • 3
  • 36
  • 41
  • 2
    @MarkHan It's ready :-) https://github.com/bazelbuild/rules_jvm_external – Jin Mar 15 '19 at 04:46
  • Does Bazel support all the android libraries we are used to using from Maven Central? – IgorGanapolsky May 01 '19 at 21:28
  • 1
    Yes. You can use rules_jvm_external and set repositories you want to look up from, like maven.google.com and Maven Central. – Jin May 01 '19 at 21:47