blob: 857007f448e84ce468de6415b242cbf7f85e8104 [file] [view]
# Android support in Bazel
## Disclaimer
NOTE: This branch is a development preview of the Starlark implementation of
Android rules for Bazel. This code is incomplete and may not function as-is.
A recent version of Bazel (7.4+, 8.0+, HEAD, Bazel 9 pre-release) is required.
This ruleset depends on [Protobuf](https://github.com/protocolbuffers/protobuf),
which has a minimum C++ language level of 17 (as of Protobuf v30, ~2025 Q3).
Depending on your system's compiler version, you may have to set `-std=c++17`
in your C++ toolchain arguments. This repository's .bazelrc file provides a
minimal set of Bazel configuration flags to build an Android app.
## Overview
This repository contains the Starlark implementation of Android rules in Bazel.
The rules are being incrementally converted from their native implementations
in the [Bazel source
tree](https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/android/).
Stardoc for the Android rules can be found at
[https://bazelbuild.github.io/rules_android](https://bazelbuild.github.io/rules_android/).
A complete sample app lives in [`examples/basicapp`](examples/basicapp). Set
`ANDROID_HOME` to an Android SDK and run `bazel build //java/com/basicapp:basic_app`
from that directory.
## Getting Started
Bzlmod is the recommended way to depend on these rules. Add the following to your
`MODULE.bazel` file. `0.7.3` is the latest release on the Bazel Central
Registry; pin a newer version if one is available.
```starlark
bazel_dep(name = "rules_java", version = "9.0.3")
bazel_dep(name = "bazel_skylib", version = "1.8.1")
bazel_dep(name = "rules_android", version = "0.7.3")
remote_android_extensions = use_extension(
"@rules_android//bzlmod_extensions:android_extensions.bzl",
"remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_tools")
android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
use_repo(android_sdk_repository_extension, "androidsdk")
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
```
The Android SDK is resolved from `ANDROID_HOME`.
WORKSPACE remains supported. Add the following to your WORKSPACE file:
```starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_android",
sha256 = "c4cd258d3761eff08ee044c0252179bb6ee8af8ab24f7dafb73b280eeda98243",
strip_prefix = "rules_android-0.7.3",
url = "https://github.com/bazelbuild/rules_android/releases/download/v0.7.3/rules_android-v0.7.3.tar.gz",
)
# Android rules dependencies
load("@rules_android//:prereqs.bzl", "rules_android_prereqs")
rules_android_prereqs()
##### rules_java setup for rules_android #####
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
rules_java_dependencies()
# note that the following line is what is minimally required from protobuf for the java rules
# consider using the protobuf_deps() public API from @com_google_protobuf//:protobuf_deps.bzl
load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility
proto_bazel_features(name = "proto_bazel_features")
# register toolchains
load("@rules_java//java:repositories.bzl", "rules_java_toolchains")
rules_java_toolchains()
##### rules_jvm_external setup for rules_android #####
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
rules_jvm_external_deps()
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
rules_jvm_external_setup()
##### rules_android setup #####
load("@rules_android//:defs.bzl", "rules_android_workspace")
rules_android_workspace()
# Android SDK setup
load("@rules_android//rules:rules.bzl", "android_sdk_repository")
android_sdk_repository(
name = "androidsdk",
)
register_toolchains(
"@rules_android//toolchains/android:android_default_toolchain",
"@rules_android//toolchains/android_sdk:android_sdk_tools",
)
```
Then, in your BUILD files, import and use the rules:
```starlark
load("@rules_android//android:rules.bzl", "android_binary", "android_library")
android_binary(
...
)
android_library(
...
)
```
`@rules_android//rules:rules.bzl` is the same load path.
## Documentation
* [Stardoc API Reference](https://bazelbuild.github.io/rules_android/)
* [Shrinking and Optimization with R8](docs/r8-optimization.md)
* [Basic app example](examples/basicapp)