Search code examples
androidrustandroid-ndkbazel

Build rust_binary for android using bazel


I can easily build cc_binary and execute it on android device. I want to do the same using rust_rules. But not able to build it. I'm confused with flags that must be used.

I didn't try on linux, but I want to know exactly what I'm doing.

My BUILD.bazel:

rust_binary(
    name = "hello",
    srcs = [
        "src/main.rs",
    ],
)

Currently I try to achieve executable rust binary with following flags (I'm on Bazel 7.0.0):

bazel build //rust:hello --android_platforms=//:android_arm64 --extra_toolchains=@androidndk//:toolchain

But fail with

external/androidndk/toolchains/llvm/prebuilt/darwin-x86_64/BUILD:8:19: in cc_toolchain_suite rule @@androidndk//toolchains/llvm/prebuilt/darwin-x86_64:cc_toolchain_suite: cc_toolchain_suite '@@androidndk//toolchains/llvm/prebuilt/darwin-x86_64:cc_toolchain_suite' does not contain a toolchain for cpu 'darwin_arm64'

I'm running on macOS.


Solution

  • The issue was solved by adding

    extra_target_triples = [
       "aarch64-linux-android",
    ],
    

    to rust_register_toolchains.

    Full answer is here.

    I was able to build and run rust_binary on android:

    bazel build //:hello --platforms=//:android_arm64
    adb push bazel-bin/hello /data/local/tmp
    adb shell /data/local/tmp/hello
    Hello world!