From bd88c7e47b911e5fa1f8a3c26d0f8317943c43ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=B9d=C5=8Dng=20Y=C3=A1ng?= Date: Wed, 16 Nov 2022 04:40:52 +0100 Subject: [PATCH] Make bazel Android remote deps compatible with bzlmod (#16772) * 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 --- WORKSPACE | 2 ++ src/MODULE.tools | 5 ++- src/test/shell/bazel/android/BUILD | 2 ++ .../bazel/android/android_integration_test.sh | 22 +++++++++++++ tools/android/android_extensions.bzl | 33 +++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tools/android/android_extensions.bzl diff --git a/WORKSPACE b/WORKSPACE index e6800ee46859b2..0d78fcf027e243 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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, @@ -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", diff --git a/src/MODULE.tools b/src/MODULE.tools index 63da4da84a4e23..a31ef59a80b547 100644 --- a/src/MODULE.tools +++ b/src/MODULE.tools @@ -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") @@ -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") diff --git a/src/test/shell/bazel/android/BUILD b/src/test/shell/bazel/android/BUILD index 7783268adbc091..3d01aadb4f8c94 100644 --- a/src/test/shell/bazel/android/BUILD +++ b/src/test/shell/bazel/android/BUILD @@ -44,6 +44,8 @@ android_sh_test( tags = [ "no-remote", "no_windows", + # bzlmod test requires network + "requires-network", ], ) diff --git a/src/test/shell/bazel/android/android_integration_test.sh b/src/test/shell/bazel/android/android_integration_test.sh index 84293ff401f1fd..1b647c0043e254 100755 --- a/src/test/shell/bazel/android/android_integration_test.sh +++ b/src/test/shell/bazel/android/android_integration_test.sh @@ -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 diff --git a/tools/android/android_extensions.bzl b/tools/android/android_extensions.bzl new file mode 100644 index 00000000000000..ea84807d8487e7 --- /dev/null +++ b/tools/android/android_extensions.bzl @@ -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, +)