I have project in which I want do build android and non-android targets. My WORSPACE.bazel
file has defined android related dependencies.
android_sdk_repository(
name = "androidsdk",
api_level = 33,
)
http_archive(
name = "rules_android_ndk",
....
)
load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
android_ndk_repository(
name = "androidndk",
api_level = 33,
)
register_toolchains("@androidndk//:all")
The problem is that code above requires to have Android's SDK/NDK downloaded to my computer no matter which target I want to build.
I don't want to other developers (who works on that project, but not on Android's target) have to download Android SDK.
Is there any preferable approach to skip using some dependencies, depending on a building target ?
I know that I cannot create conditions in WORKSPACE
file but maybe it's achievable in other way ?
In general Bazel only looks at what's needed to build what's requested, so usually if nothing Android-related is requested the android_sdk_repository
and android_ndk_repository
repo rules shouldn't be loaded. However, with the transition to toolchains, using register_toolchains()
will cause that repo to be loaded. So register_toolchains("@androidndk//:all")
will cause the ndk to be loaded, and the Starlark version of android_sdk_repository
similarly: https://github.com/bazelbuild/rules_android/blob/6cd2cbb345eff9c94462498501d83cf700357113/examples/basicapp/WORKSPACE#L39-L42
The workaround in place for android_sdk_repository
(both Starlark and native) is to generate an empty toolchain that will produce an error if it ends up being actually used (e.g. https://github.com/bazelbuild/rules_android/blob/6cd2cbb345eff9c94462498501d83cf700357113/rules/android_sdk_repository/empty.template.bzl#L56-L66)
However android_ndk_repository
(the starlark version as used in the question) doesn't yet produce an empty toolchain if ANDROID_NDK_HOME
is not set. But there's an in-progress change to add that: https://github.com/bazelbuild/rules_android_ndk/pull/63
A temporary workaround is to remove the register_toolchains()
and instead register the toolchains with --extra_toolchains
on the command line (and this can also be placed into the .bazelrc
as something like build:android --extra_toolchains=@androidndk//:all
and then --config=android
on the command line). The drawback is that you have to remember to set this whenever you build an Android target.