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_repositoryandandroid_ndk_repositoryrepo rules shouldn't be loaded. However, with the transition to toolchains, usingregister_toolchains()will cause that repo to be loaded. Soregister_toolchains("@androidndk//:all")will cause the ndk to be loaded, and the Starlark version ofandroid_sdk_repositorysimilarly: https://github.com/bazelbuild/rules_android/blob/6cd2cbb345eff9c94462498501d83cf700357113/examples/basicapp/WORKSPACE#L39-L42The 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 ifANDROID_NDK_HOMEis not set. But there's an in-progress change to add that: https://github.com/bazelbuild/rules_android_ndk/pull/63A temporary workaround is to remove the
register_toolchains()and instead register the toolchains with--extra_toolchainson the command line (and this can also be placed into the.bazelrcas something likebuild:android --extra_toolchains=@androidndk//:alland then--config=androidon the command line). The drawback is that you have to remember to set this whenever you build an Android target.