Compile failed with -target=aarch64-linux-android

Why am I able to successfully compile when I run cargo build in the rust directory, but it fails when I add --target=aarch64-linux-android?
Here are the steps I took:

git clone git@github.com:mozilla/rust-android-gradle.git
rustup target add aarch64-linux-android
cd rust-android-gradle/samples/rust

and this compiled successfully:
cargo build
image

this compiled failed:
cargo build --target=aarch64-linux-android
image

The error message told me that target aarch64-linux-android is not installed, but I have already installed it.”
image

The error message is trying to tell you that it couldn't find the standard library for that target. It also provides a hint for how to resolve it.

By default, rustup will only install pre-compiled versions of the standard library for your host machine, so if you want to compile to other architectures or operating systems, you need to do rustup target add blah.

They said they already did that

1 Like

Is it possible that you have two copies of Rust installed on your machine? (e.g. a one from system's package manager — which doesn't support rustup targets)

Does which rustc point to ~/.cargo/bin/rustc?

Thank you for your answer :slight_smile:
I don't think this possibility exists because when I execute cargo build --target=aarch64-linux-android and cargo build in the same terminal, the result is that cargo build will build an error while cargo build --target=aarch64-linux-android will not.

It is possible that cargo executes a rust compiler which isn't managed by rustup and as such rustup target add doesn't have any effect on it. What do which cargo and which rustc show?

Just sharing the equivalent Cargo issue here to avoid duplicated effort:

1 Like

Sure, sorry, I raised this issue on both sides because I wasn't sure if it was my problem or a bug. I have already closed the issue.

This is the result of me executing the two commands that you mentioned separately

I feel like /usr/local/bin/cargo is not the cargo downloaded from rustup. Could you try if /Users/ccy/.cargo/bin/cargo build --target=aarch64-linux-android works?

1 Like

It is exactly this problem. Rust installed from a Linux distro is not going to support rustup add, which means it will work fine without --target, and fail when --target for another architecture is used.

1 Like

Thank you for your reply :slight_smile:
I executed it and encountered the same error

Sorry, I'm a bit confused. Both my Rust and Cargo were installed through rustup, and initially I followed the official instructions to complete the installation by executing the command "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh" in the command line.

You have two rusts on your computer. A bad one that can't support Android no matter what you do, which is always going to break when you use --target for another platform, and a good one that has Android support installed, but which you are not using, because the bad one is taking over.

I see you're using macOS, so run:

brew uninstall rust

because Rust from Homebrew can't compile for Android. You are using Rust from Homebrew, and not Rust from rustup. Only Rust installed via rustup can be configured for cross-compilation via rustup commands.

4 Likes

Thank you very much for @kornel 's patient explanation. As you said, there were two versions of Rust installed on my computer, and I didn't even realize it. Following your advice, I removed Rust from Homebrew, and the problem was solved. Thank you again!

1 Like

Can we use rustup in Android devices?

Android is a tier 2 target without host tools. This means that while we ship a precompiled standard library, we don't ship a precompiled rustc version for android. Using rustup requires a precompiled rustc version for the host shipped by the rust project.

1 Like

There are unofficial ways to get rust compilers on Android (e.g. for termux) but not, afaik, rustup itself (as that is only made for and to distribute the officially supported host tool platforms)

I have the same problem, and after carefully examination, I found I installed 2 rust package at my computer. so when I run rustup and cargo, I am using 2 exe with diffent libs. Actually until today I can't say I figure out this problem totally, but after reinstall the rust I solve my problem and it work well until now. If you have same problem, please carefully examine all the folders where you install rust and check the PATH
environmental variable as well.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.