English 中文(简体)
How can I fix a build error with @bazel_tools//tools/android:databinding_annotation_processor
原标题:
  • 时间:2023-11-24 04:26:27
  •  标签:
  • bazel

I downloaded the Bazel "Build an Android App" tutorial from:

https://bazel.build/start/android-app

and modified the targetSdkVersion to 33 and it built fine with Bazel.

I then switched the MainActivity to use AppCompatActivity and integrated external dependencies using rules_jvm_external with bzlmod, following directions from:

https://github.com/bazelbuild/rules_jvm_external/blob/master/docs/bzlmod.md

I m now getting:

ERROR: /home/maru/.cache/bazel/_bazel_maru/ffa24f764f8fce22a35440e17ba7441e/external/bazel_tools/tools/android/BUILD:446:6: no such target  //external:databinding_annotation_processor : target  databinding_annotation_processor  not declared in package  external  defined by /home/maru/android/examples/android/tutorial/WORKSPACE (Tip: use `query "//external:*"` to see all the targets in that package) and referenced by  @bazel_tools//tools/android:databinding_annotation_processor 

Update 2023-11-30

I ve revisited this starting from scratch and Bazel s "Build an Android App" builds fine until I enable bzlmod:

  1. Add a .bazelrc containing common --enable_bzlmod
  2. mv WORKSPACE WORKSPACE.bzlmod
  3. touch MODULE.bazel

Then I get the above error relating to databinding, my use of AppCompatActivity was a red herring. Is this a known issue that bzlmod and Android builds are currently (using bazel version 6.4.0) actively incompatible?

最佳回答

databinding hasn t been updated to work with bzlmod, it requires a bind() in the workspace:

bind(
    name = "databinding_annotation_processor",
    actual = "//tools/android:compiler_annotation_processor",
)

and a target in tools/android/BUILD in the workspace (or wherever the bind points to):

java_plugin(
    name = "compiler_annotation_processor",
    generates_api = True,
    processor_class = "android.databinding.annotationprocessor.ProcessDataBinding",
    visibility = ["//visibility:public"],
    deps = [
        "@bazel_tools//src/tools/android/java/com/google/devtools/build/android:all_android_tools",
    ],
)

and bzlmod doesn t support bind().

Can you use Compose instead? https://github.com/bazelbuild/examples/tree/main/android/jetpack-compose


Update

This particular issue is fixed in bazel 7.0.0 with https://github.com/bazelbuild/bazel/commit/f4fcb422adf1a33fe687cfe9d23d5fa0f74551fc as part of https://github.com/bazelbuild/rules_android/issues/97

Bazel 7.0.0 hasn t been released yet, but if you have bazelisk installed you can do:

USE_BAZEL_VERSION=last_rc bazelisk  build //src/main:app --enable_bzlmod

However there are still some things that need to be fixed, e.g. you ll run into:

ERROR: no such package  @@android_gmaven_r8//jar : The repository  @@android_gmaven_r8  could not be resolved: Repository  @@android_gmaven_r8  is not defined

This can be worked around by adding the dependencies to the local workspace:

  1. Make sure there s a BUILD file in the root workspace

  2. Create android_extensions.bzl and add the dependencies: (Based on android_extensions.bzl):

# 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("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_jar")

def _remote_android_tools_extensions_impl(_ctx):
    http_archive(
        name = "android_tools",
        sha256 = "2b661a761a735b41c41b3a78089f4fc1982626c76ddb944604ae3ff8c545d3c2",  # DO_NOT_REMOVE_THIS_ANDROID_TOOLS_UPDATE_MARKER
        url = "https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.30.0.tar",
    )
    http_jar(
        name = "android_gmaven_r8",
        sha256 = "57a696749695a09381a87bc2f08c3a8ed06a717a5caa3ef878a3077e0d3af19d",
        url = "https://maven.google.com/com/android/tools/r8/8.1.56/r8-8.1.56.jar",
    )

remote_android_tools_extensions = module_extension(
    implementation = _remote_android_tools_extensions_impl,
)
  1. Add the following to MODULE.bazel:
remote_android_extensions = use_extension(":android_extensions.bzl", "remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")

This should all be fixed in a future minor release of Bazel 7.

问题回答

暂无回答




相关问题
不能根据Bazel平台选择依赖

我正在学习Bazel,并试图建造一些第三方图书馆。 我有时使用我的马奇,有时使用我的班子计算机。 因此,在我建立开放式电话时,我想根据......使用不同的连接点。

热门标签