Search code examples
androidbuildbazeltoolchain

How to resolve @bazel_tools//tools/android:databinding_exec when switching from bazel 3.6.0 to 3.7.0


I’ve been using bazel to build an android project, initially starting with a 0.24 version, then using the 2.0 version for a longer time, and now I am trying to switch to the latest 3.7.0 / 3.7.1 versions.

Currently I am using the following important .bazelrc settings for android:

build:androidbuild --crosstool_top=@androidndk//:default_crosstool
build:androidbuild --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build:android-32 --config=androidbuild --cpu=x86 --fat_apk_cpu=x86

All cc_binary / cc_library targets can still be built the same way we used to, but the android_binary targets no longer builds and I am getting this error:

ERROR: While resolving toolchains for target @bazel_tools//tools/android:databinding_exec: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.

I tried to find which was the bazel version that started to throw this error, and I found that I can still build my project with 3.6.0, but I start getting the above error if I switch to 3.7.0 or 3.7.1.

Looking at the 3.6.0 vs 3.7.0 changes, I am wondering if the new create_dummy_sdk_toolchain call in tools/android/android_sdk_repository_template.bzl would cause this failure or there is something else I am missing (like changes in the cc_flags_supplier.bzl / compiler_flag.bzl sources to set incompatible_use_toolchain_transition to True).

I tried the approach of using --platforms instead, but I am still getting errors even in that case:

build:androidbuild --extra_toolchains=@androidndk//:all
build:android-32 --config=androidbuild --cpu=x86 --platforms=//config:android_x86

With:

platform(
    name = "android_x86",
    constraint_values = [
        "@bazel_tools//platforms:x86_32",
        "@bazel_tools//platforms:android",
    ],
    cpu_constraints = [
    ],
    os_constraints = [
    ],
    target_platform = True,
)

Error:

ERROR: While resolving toolchains for target @bazel_tools//tools/android:instrumentation_test_check: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.

Can you please advise me on what is missing from the project that is required to solve the problem caused by the 3.6.0 vs 3.7.0 diffs, do we now need to define a proper toolchain for Android (can't we rely anymore on bazel to figure out how to build android targets)?

UPDATE Dec/16/2020

Looks like the problem was caused by this code:

WORKSPACE:

register_execution_platforms(
    "//tools/config:host_platform",
)

In the BUILD file:

platform(
    name = "host_platform",
    constraint_values = [
    ],
    cpu_constraints = [
        "@bazel_tools//platforms:x86_32",
        "@bazel_tools//platforms:x86_64",
    ],
    host_platform = True,
    os_constraints = [
        "@bazel_tools//platforms:windows",
    ],
)

For some reason this works in bazel 3.6.0 even when the host is Linux, but it's no longer working in 3.7.0.


Solution

  • The problem was caused by this code:

    WORKSPACE:

    register_execution_platforms(
        "//tools/config:host_platform",
    )
    

    In the BUILD file:

    platform(
        name = "host_platform",
        constraint_values = [
        ],
        cpu_constraints = [
            "@bazel_tools//platforms:x86_32",
            "@bazel_tools//platforms:x86_64",
        ],
        host_platform = True,
        os_constraints = [
            "@bazel_tools//platforms:windows",
        ],
    )
    

    For some reason this works in bazel 3.6.0 even when the host is Linux, but it's no longer working in 3.7.0.