Skip to content

Commit

Permalink
Make bazel Android remote deps compatible with bzlmod (#16772)
Browse files Browse the repository at this point in the history
* Upgrade bazel's bzlmod rules_java version to 5.3.5

Latest rules_java has fixes for Android build integration.

PiperOrigin-RevId: 488701777
Change-Id: I144f6feac88feaad5419fcdd9e21e5b8e2003b16

* Adds a bzlmod extension for remote android_tools

PiperOrigin-RevId: 488720288
Change-Id: I6e7baa1d87d2d7b5434d8870e726faf362842dac

Co-authored-by: Googler <[email protected]>
  • Loading branch information
Wyverald and ted-xie authored Nov 16, 2022
1 parent 0703058 commit bd88c7e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ dist_http_archive(
)

# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE
# and tools/android/android_extensions.bzl
http_archive(
name = "android_tools_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
Expand All @@ -413,6 +414,7 @@ http_archive(
)

# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_remote_tools.WORKSPACE
# and tools/android/android_extensions.bzl
http_jar(
name = "android_gmaven_r8_for_testing",
sha256 = "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972",
Expand Down
5 changes: 4 additions & 1 deletion src/MODULE.tools
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module(name = "bazel_tools")

bazel_dep(name = "rules_cc", version = "0.0.2")
bazel_dep(name = "rules_java", version = "5.1.0")
bazel_dep(name = "rules_java", version = "5.3.5")
bazel_dep(name = "rules_license", version = "0.0.3")
bazel_dep(name = "rules_proto", version = "4.0.0")
bazel_dep(name = "rules_python", version = "0.4.0")
Expand Down Expand Up @@ -33,3 +33,6 @@ register_toolchains("@local_config_sh//:local_sh_toolchain")

remote_coverage_tools_extension = use_extension("//tools/test:extensions.bzl", "remote_coverage_tools_extension")
use_repo(remote_coverage_tools_extension, "remote_coverage_tools")

remote_android_extensions = use_extension("//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
2 changes: 2 additions & 0 deletions src/test/shell/bazel/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ android_sh_test(
tags = [
"no-remote",
"no_windows",
# bzlmod test requires network
"requires-network",
],
)

Expand Down
22 changes: 22 additions & 0 deletions src/test/shell/bazel/android/android_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@ EOF
//java/com/example/hello:hello || fail "build failed"
}

function test_hello_android_bzlmod() {
write_hello_android_files
setup_android_sdk_support
cat > java/com/example/hello/BUILD <<'EOF'
android_binary(
name = 'hello',
manifest = "AndroidManifest.xml",
srcs = ['MainActivity.java'],
resource_files = glob(["res/**"]),
)
EOF
cat > MODULE.bazel << 'EOF'
# Required for android_integration_test_with_platforms
bazel_dep(name = "platforms", version = "0.0.5")
EOF

bazel clean
# Check that android builds with bzlmod enable work.
bazel build --experimental_enable_bzlmod \
//java/com/example/hello:hello || fail "build failed"
}

function test_android_tools_version() {
create_new_workspace
setup_android_sdk_support
Expand Down
33 changes: 33 additions & 0 deletions tools/android/android_extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module extension to declare Android runtime dependencies for Bazel."""

load("//tools/build_defs/repo:http.bzl", "http_archive", "http_jar")

def _remote_android_tools_extensions_impl(_ctx):
http_archive(
name = "android_tools",
sha256 = "1afa4b7e13c82523c8b69e87f8d598c891ec7e2baa41d9e24e08becd723edb4d", # do_not_remove_this_android_tools_update_marker
url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.27.0.tar.gz",
)
http_jar(
name = "android_gmaven_r8",
sha256 = "8626ca32fb47aba7fddd2c897615e2e8ffcdb4d4b213572a2aefb3f838f01972",
url = "https://maven.google.com/com/android/tools/r8/3.3.28/r8-3.3.28.jar",
)

remote_android_tools_extensions = module_extension(
implementation = _remote_android_tools_extensions_impl,
)

0 comments on commit bd88c7e

Please sign in to comment.