Skip to content

Library creation fails with Android NDK r23b #636

@b-spencer

Description

@b-spencer

Problem

Using fresh installations of Rust installed on x86_64-unknown-linux-gnu (Debian 10) via the rustup method, compiling packages for Android with NDK r23b (the current LTS release) does not work.

Steps

  1. curl -sf -L https://static.rust-lang.org/rustup.sh | sh (fresh install for the local user)

  2. rustup target add aarch64-linux-android

  3. Create an empty library crate that depends on ring:

cargo new --lib hello
echo 'ring = "0.16"' >>hello/Cargo.toml
  1. Compile for the host and all is well:
$ cargo build
    Updating crates.io index
   Compiling libc v0.2.107
   Compiling cc v1.0.72
   Compiling once_cell v1.8.0
   Compiling spin v0.5.2
   Compiling untrusted v0.7.1
   Compiling ring v0.16.20
   Compiling hello v0.1.0 (/home/.../hello)
    Finished dev [unoptimized + debuginfo] target(s) in 7.48s
  1. Compile for aarch64-linux-android the simple, documented way and it fails, but that's because of all the reasons in Build and link issues on Linux hosts with newer Android NDKs #459:
$ cargo build --target aarch64-linux-android
   Compiling untrusted v0.7.1
   Compiling spin v0.5.2
   Compiling once_cell v1.8.0
   Compiling libc v0.2.107
   Compiling ring v0.16.20
error: failed to run custom build command for `ring v0.16.20`

Caused by:
  process didn't exit successfully: `/home/.../hello/target/debug/build/ring-b1c7634ecf998ee6/build-script-build` (exit status: 101)
...
  1. Add all the workarounds from Build and link issues on Linux hosts with newer Android NDKs #459 and it still doesn't work:
$ PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/:$PATH \
  CC_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  cargo build --lib --release --target aarch64-linux-android
   Compiling ring v0.16.20
error: failed to run custom build command for `ring v0.16.20`

Caused by:
  process didn't exit successfully: `/home/.../hello/target/release/build/ring-14a8ac965b8995d8/build-script-build` (exit status: 1)
...
  --- stdout
  running: "aarch64-linux-android-ar" "cq" ...
  ...

  --- stderr

  error occurred: Failed to find tool. Is `aarch64-linux-android-ar` installed?

The newest versions of the Android NDK simply do not ship GNU binutils and thus there is no aarch64-linux-android-ar to use. No amount of changing paths or environment variables is likely to make this work. I suspect that the rust toolchain will need to use llvm-ar and the other LLVM tools instead if it is to work with the current LTS versions of the Android NDK.

Activity

TroyNeubauer

TroyNeubauer commented on Jan 11, 2022

@TroyNeubauer

Same issue here. Would love to see a fix to allow me to use the current version of the NDK

sshxmz

sshxmz commented on Feb 14, 2022

@sshxmz

Same problem here.

outdated profile for cc::build:
default compiler = 'aarch64-linux-android21-clang'
default archiver = 'aarch64-linux-android-ar'

rileyhawk1417

rileyhawk1417 commented on Feb 18, 2022

@rileyhawk1417

Same issue here. Would love to see a fix to allow me to use the current version of the NDK

Does version 22/21 have the files. Was hesitant after downloading via Android studio and manually downloading the NDK archive from the website.

[Edit]
Found what I was looking for. Hopefully this issue gets attention, will just be using version 22 for now.

dot-asm

dot-asm commented on Mar 11, 2022

@dot-asm
Contributor

Would you mind testing a variant of #657? "A variant" means that I would have to prepare a variant that would extend the suggested approach to android targets.

dot-asm

dot-asm commented on Mar 14, 2022

@dot-asm
Contributor

In the context I also wonder how do you specify the compiler? The background for the question is following. My understanding is that you're supposed to specify linker through .cargo/config.toml. In which case it would be only natural to use it even as default C compiler and use it to derive C++ compiler name...

irediaes

irediaes commented on Mar 22, 2022

@irediaes

To fix copy and rename llvm-ar to aarch64-linux-android-ar in ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/{SYSTEM}/bin

Do this for all your target arch.

b-spencer

b-spencer commented on Mar 22, 2022

@b-spencer
Author

@irediaes Thanks for the information!

Armed with this tip, I guessed that I can also set an AR_* variable just like CC_* in my current list of workarounds.

Taking the command from my item 6 above (in the first comment) and adding

AR_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar

to it seems to make the build succeed.

PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/:$PATH \
  CC_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  AR_aarch64_linux_android=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar \
  CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang \
  cargo build --lib --release --target aarch64-linux-android

I don't understand why rust's tools' expectations don't align with how the Android NDK ships, but it seems that at least I can configure it to work without modifying the Android NDK itself.

I'll leave this issue open in the hopes that either the rust toolchain will adapt to the NDK such that no workarounds at all are required, or at least that how to do this becomes documented somewhere.

Thanks again for the key information.

dot-asm

dot-asm commented on Mar 25, 2022

@dot-asm
Contributor

Isn't answer to all these questions cargo install cargo-apk?

I don't understand why rust's tools' expectations don't align with how the Android NDK ships

It has nothing to do with Android. If you want to cross-compile for any platform, it is your responsibility to specify appropriate cross-linker. You can do it yourself or use helpers like cargo-apk. (Just in case, no, one can't do choose linker in a build script, it's too late.)

briansmith

briansmith commented on Sep 27, 2024

@briansmith

Should this be closed? Everything in the description is so outdated that it is hard to see if it has any relevance today.

NobodyXu

NobodyXu commented on Sep 28, 2024

@NobodyXu
Collaborator

Thanks, I've closed this.

If anyone still encounters this issue, feel free to open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @briansmith@b-spencer@dot-asm@TroyNeubauer@NobodyXu

        Issue actions

          Library creation fails with Android NDK r23b · Issue #636 · rust-lang/cc-rs