Skip to content

Commit 7adce4c

Browse files
committed
Support building android/ with Bazel (bzlmod only)
1 parent 2f12772 commit 7adce4c

6 files changed

Lines changed: 93 additions & 3 deletions

File tree

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
2+
3+
common:skip_android --deleted_packages=android

.github/workflows/testing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ jobs:
100100
key: ${{ runner.os }}-bazel-${{ env.USE_BAZEL_VERSION }}-${{ hashFiles('WORKSPACE', 'repositories.bzl') }}
101101

102102
- name: Run bazel build
103-
run: bazelisk build //... --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }}
103+
run: bazelisk build //... --config=skip_android --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }}
104104

105105
- name: Run bazel test
106-
run: bazelisk test //... --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }}
106+
run: bazelisk test //... --config=skip_android --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }}
107107

108108
- name: Run example bazel build
109109
run: bazelisk build //... --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }}

COMPILING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,13 @@ to ensure you're always building using the latest supported version, then try:
163163
$ bazelisk build //...
164164
```
165165

166+
Some parts of grpc-java depend on Android. Bazel can build these parts too but,
167+
for size, licensing and maintenance reasons, it requires a locally installed
168+
Android SDK. If you don't have the SDK and/or don't care about Android, use the
169+
`skip_android` configuration to skip building the Android parts:
170+
171+
```sh
172+
$ bazelisk build //... --config=skip_android
173+
```
174+
166175
You cannot run the tests from Bazel at this time.

MODULE.bazel

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,31 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
4949
]
5050
# GRPC_DEPS_END
5151

52+
ANDROID_ARTIFACTS = [
53+
"androidx.annotation:annotation:1.6.0",
54+
"androidx.annotation:annotation-jvm:1.6.0",
55+
"androidx.core:core:1.13.1",
56+
"androidx.lifecycle:lifecycle-common:2.6.2",
57+
]
58+
5259
bazel_dep(name = "abseil-cpp", version = "20250512.1")
5360
bazel_dep(name = "bazel_jar_jar", version = "0.1.11.bcr.1")
5461
bazel_dep(name = "bazel_skylib", version = "1.7.1")
5562
bazel_dep(name = "googleapis", version = "0.0.0-20260514-1dbb1a14", repo_name = "com_google_googleapis")
5663
bazel_dep(name = "grpc-proto", version = "0.0.0-20240627-ec30f58.bcr.1", repo_name = "io_grpc_grpc_proto")
5764
bazel_dep(name = "protobuf", version = "33.4", repo_name = "com_google_protobuf")
65+
bazel_dep(name = "rules_android", version = "0.7.2")
5866
bazel_dep(name = "rules_cc", version = "0.0.9")
5967
bazel_dep(name = "rules_java", version = "9.1.0")
6068
bazel_dep(name = "rules_jvm_external", version = "6.0")
6169

70+
android_sdk_repository_extension = use_extension(
71+
"@rules_android//rules/android_sdk_repository:rule.bzl",
72+
"android_sdk_repository_extension",
73+
)
74+
use_repo(android_sdk_repository_extension, "androidsdk")
75+
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
76+
6277
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
6378
maven.install(
6479
artifacts = IO_GRPC_GRPC_JAVA_ARTIFACTS,
@@ -69,6 +84,25 @@ maven.install(
6984
)
7085
use_repo(maven, "maven")
7186

87+
# Define a separate, dev-only extension import for Android deps.
88+
# This prevents downstream non-Android users from having to resolve
89+
# Google Maven (which is required for androidx.*) or rules_android transitively.
90+
grpc_android_maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven", dev_dependency = True)
91+
grpc_android_maven.install(
92+
name = "grpc_android_maven",
93+
artifacts = ANDROID_ARTIFACTS,
94+
repositories = [
95+
"https://repo.maven.apache.org/maven2/",
96+
"https://maven.google.com",
97+
],
98+
strict_visibility = True,
99+
# For Bazel 8+ compatibility.
100+
use_starlark_android_rules = True,
101+
# Set this explicitly since the default guess is incorrect under Bzlmod.
102+
aar_import_bzl_label = "@rules_android//rules:rules.bzl",
103+
)
104+
use_repo(grpc_android_maven, "grpc_android_maven")
105+
72106
maven.override(
73107
coordinates = "com.google.protobuf:protobuf-java",
74108
target = "@com_google_protobuf//:protobuf_java",

android/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@rules_android//rules:rules.bzl", "android_library")
2+
load("@rules_jvm_external//:defs.bzl", "artifact")
3+
4+
licenses(["notice"])
5+
6+
android_library(
7+
name = "android",
8+
srcs = glob([
9+
"src/main/java/**/*.java",
10+
]),
11+
manifest = "src/main/AndroidManifest.xml",
12+
custom_package = "io.grpc.android",
13+
# TODO(jdcormie): Figure out how to expose this publicly without making
14+
# existing non-android users configure rules_android and maven.google.com.
15+
visibility = ["//:__subpackages__"],
16+
deps = [
17+
"//api",
18+
"//core:internal",
19+
artifact("com.google.code.findbugs:jsr305"),
20+
artifact("com.google.errorprone:error_prone_annotations"),
21+
artifact("com.google.guava:guava"),
22+
],
23+
)

buildscripts/kokoro/android.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,36 @@ cat <<EOF >> gradle.properties
1919
org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=1024m
2020
EOF
2121

22-
export ANDROID_HOME=/tmp/Android/Sdk
22+
# https://bazel.build/versions/9.1.0/docs/sandboxing gives each worker its own
23+
# isolated and ephemeral /tmp. Install the Android SDK anywhere but there.
24+
export ANDROID_HOME=$HOME/Android/Sdk
2325
mkdir -p "${ANDROID_HOME}/cmdline-tools"
2426
curl -Ls -o cmdline.zip \
2527
"https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip"
2628
unzip -qd "${ANDROID_HOME}/cmdline-tools" cmdline.zip
2729
rm cmdline.zip
2830
mv "${ANDROID_HOME}/cmdline-tools/cmdline-tools" "${ANDROID_HOME}/cmdline-tools/latest"
2931
(yes || true) | "${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager" --licenses
32+
33+
# Bazel/android_rules requires build-tools at least version 35.0.0 and, unlike
34+
# gradle, won't download anything for itself.
35+
# TODO(jdcormie): Keep this version in sync with AGP's default and any
36+
# `buildToolsVersion` config.
37+
"${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager" --install "build-tools;35.0.1"
38+
39+
# TODO(jdcormie): Use the same SDK version as build.gradle's compileSdkVersion.
40+
"${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager" --install "platforms;android-34"
41+
42+
# Bazelisk takes care of installing Bazel.
43+
mkdir -p /tmp/bazelisk
44+
curl -Ls -o /tmp/bazelisk/bazelisk https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64
45+
chmod +x /tmp/bazelisk/bazelisk
46+
export PATH=/tmp/bazelisk:$PATH
47+
48+
export USE_BAZEL_VERSION=9.1.0
49+
bazelisk build \
50+
//android
51+
3052
curl -Ls https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.tar.gz | \
3153
tar xz -C /tmp
3254
export PATH=/tmp/cmake-3.26.3-linux-x86_64/bin:$PATH

0 commit comments

Comments
 (0)