Skip to content

Commit 4776eed

Browse files
committed
binder: bazel build support (bzlmod mode only)
1 parent 039ad77 commit 4776eed

7 files changed

Lines changed: 113 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=binder

.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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ project, run:
2828
$ ./gradlew publishToMavenLocal
2929
```
3030

31+
Building with Bazel
32+
-------------------
33+
34+
grpc-java can also be built using Bazel in bzlmod mode.
35+
36+
Some parts of grpc-java depend on Android. By default, building requires an
37+
the `ANDROID_HOME` environment variable pointing at an installed Android SDK.
38+
Use the `skip_android` configuration to avoid this requirement:
39+
40+
```sh
41+
$ bazel build //... --config=skip_android
42+
```
43+
3144
### Notes for IntelliJ
3245
Building in IntelliJ works best when you import the project as a Gradle project and delegate IDE
3346
build/run actions to Gradle.

MODULE.bazel

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,31 @@ IO_GRPC_GRPC_JAVA_ARTIFACTS = [
4646
]
4747
# GRPC_DEPS_END
4848

49+
ANDROID_ARTIFACTS = [
50+
"androidx.annotation:annotation:1.6.0",
51+
"androidx.annotation:annotation-jvm:1.6.0",
52+
"androidx.core:core:1.13.1",
53+
"androidx.lifecycle:lifecycle-common:2.6.2",
54+
]
55+
4956
bazel_dep(name = "abseil-cpp", version = "20250512.1")
5057
bazel_dep(name = "bazel_jar_jar", version = "0.1.11.bcr.1")
5158
bazel_dep(name = "bazel_skylib", version = "1.7.1")
5259
bazel_dep(name = "googleapis", version = "0.0.0-20240326-1c8d509c5", repo_name = "com_google_googleapis")
5360
bazel_dep(name = "grpc-proto", version = "0.0.0-20240627-ec30f58.bcr.1", repo_name = "io_grpc_grpc_proto")
5461
bazel_dep(name = "protobuf", version = "33.4", repo_name = "com_google_protobuf")
62+
bazel_dep(name = "rules_android", version = "0.7.2")
5563
bazel_dep(name = "rules_cc", version = "0.0.9")
5664
bazel_dep(name = "rules_java", version = "9.1.0")
5765
bazel_dep(name = "rules_jvm_external", version = "6.0")
5866

67+
android_sdk_repository_extension = use_extension(
68+
"@rules_android//rules/android_sdk_repository:rule.bzl",
69+
"android_sdk_repository_extension",
70+
)
71+
use_repo(android_sdk_repository_extension, "androidsdk")
72+
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
73+
5974
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
6075
maven.install(
6176
artifacts = IO_GRPC_GRPC_JAVA_ARTIFACTS,
@@ -64,7 +79,32 @@ maven.install(
6479
],
6580
strict_visibility = True,
6681
)
67-
use_repo(maven, "maven")
82+
83+
# We isolate Android-specific deps in this separate repo ('grpc_android_maven') instead of the one
84+
# above (having default name 'maven').
85+
#
86+
# Under bzlmod, rules_jvm_external merges all contributions to the same named repository (like the
87+
# default 'maven' repo) across the entire dependency graph. If we put Android dependencies there,
88+
# then *every* downstream consumer's merged 'maven' repository would include these Android
89+
# artifacts.
90+
#
91+
# Because merged repositories are resolved using only the root module's (the consumer's)
92+
# repository list, any consumer without 'maven.google.com' configured in their root module
93+
# would fail to resolve these artifacts, breaking their build even if they don't use Android.
94+
#
95+
# By using a distinct name ('grpc_android_maven'), we prevent merging. This repository is resolved
96+
# independently, allowing it to use grpc-java's own repository config (which includes
97+
# maven.google.com) without affecting downstream consumers' default 'maven' repo.
98+
maven.install(
99+
name = "grpc_android_maven",
100+
artifacts = ANDROID_ARTIFACTS,
101+
repositories = [
102+
"https://repo.maven.apache.org/maven2/",
103+
"https://maven.google.com", # for androidx.*
104+
],
105+
strict_visibility = True,
106+
)
107+
use_repo(maven, "maven", "grpc_android_maven")
68108

69109
maven.override(
70110
coordinates = "com.google.protobuf:protobuf-java",
@@ -158,3 +198,7 @@ maven.override(
158198
coordinates = "io.grpc:grpc-util",
159199
target = "@io_grpc_grpc_java//util",
160200
)
201+
maven.override(
202+
coordinates = "io.grpc:grpc-binder",
203+
target = "@io_grpc_grpc_java//binder",
204+
)

binder/BUILD.bazel

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 = "binder",
8+
srcs = glob([
9+
"src/main/java/**/*.java",
10+
]),
11+
manifest = "src/main/AndroidManifest.xml",
12+
custom_package = "io.grpc.binder",
13+
visibility = ["//visibility:public"],
14+
exports = ["@grpc_android_maven//:androidx_annotation_annotation"],
15+
deps = [
16+
"//api",
17+
"//core:internal",
18+
# Resolve android deps from the isolated grpc_android_maven repository.
19+
"@grpc_android_maven//:androidx_annotation_annotation",
20+
"@grpc_android_maven//:androidx_annotation_annotation_jvm",
21+
"@grpc_android_maven//:androidx_core_core",
22+
"@grpc_android_maven//:androidx_lifecycle_lifecycle_common",
23+
artifact("com.google.code.findbugs:jsr305"),
24+
artifact("com.google.errorprone:error_prone_annotations"),
25+
artifact("com.google.guava:guava"),
26+
],
27+
)

examples/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
33
load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
44
load("@rules_java//java:java_binary.bzl", "java_binary")
55
load("@rules_java//java:java_library.bzl", "java_library")
6+
load("@rules_android//rules:rules.bzl", "android_library")
67

78
proto_library(
89
name = "helloworld_proto",
@@ -278,3 +279,15 @@ java_library(
278279
"@io_grpc_grpc_java//xds",
279280
],
280281
)
282+
283+
# grpc-binder requires Android SDK and some Maven deps that aren't needed by
284+
# the other targets. This just makes sure the example MODULE works
285+
# with grpc-binder.
286+
android_library(
287+
name = "test_grpc_binder_compiles",
288+
testonly = 1,
289+
visibility = ["//visibility:public"],
290+
exports = [
291+
"@io_grpc_grpc_java//binder",
292+
],
293+
)

examples/MODULE.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ bazel_dep(name = "rules_java", version = "9.3.0")
33
bazel_dep(name = "grpc-proto", version = "0.0.0-20240627-ec30f58", repo_name = "io_grpc_grpc_proto")
44
bazel_dep(name = "protobuf", version = "33.1", repo_name = "com_google_protobuf")
55
bazel_dep(name = "rules_jvm_external", version = "6.0")
6+
bazel_dep(name = "rules_android", version = "0.7.2")
67

78
# Do not use this override in your own MODULE.bazel. It is unnecessary when
89
# using a version from BCR. Be aware the gRPC Java team does not update the
@@ -30,3 +31,13 @@ maven.install(
3031
repositories = ["https://repo.maven.apache.org/maven2/"],
3132
strict_visibility = True,
3233
)
34+
35+
# Configure Android SDK. This is only resolved if building Android targets
36+
# (like //:test_grpc_binder_compiles). Users building the plain Java examples
37+
# do not need the Android SDK installed or ANDROID_HOME set.
38+
android_sdk_repository_extension = use_extension(
39+
"@rules_android//rules/android_sdk_repository:rule.bzl",
40+
"android_sdk_repository_extension",
41+
)
42+
use_repo(android_sdk_repository_extension, "androidsdk")
43+
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")

0 commit comments

Comments
 (0)