官方编译环境配置地址:https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html
1、安装Android ndk环境
2、配置ndk 到path中
export ANDROID_HOME=/Users/$USER/Library/Android/sdk
export NDK_HOME=$ANDROID_HOME/ndk-bundle
3、安装rust环境,如果有可以跳过
curl https://sh.rustup.rs -sSf | sh
4、生成交叉编译的文件
$符号是命令行中自带的符号,不需要复杂
$ mkdir NDK
$ cd NDK
${NDK_HOME}/build/tools/make_standalone_toolchain.py --api 26 --arch arm64 --install-dir NDK/arm64
${NDK_HOME}/build/tools/make_standalone_toolchain.py --api 26 --arch arm --install-dir NDK/arm
${NDK_HOME}/build/tools/make_standalone_toolchain.py --api 26 --arch x86 --install-dir NDK/x86
5、然后可以直接切换到 .cargo 的目录下,直接创建一个config文件,并添加编译配置
cd ~/.cargo
open ./
然后创建一个文本文件。复制一下信息并保存.
[target.aarch64-linux-android]
ar = "<上边NDK的实际路径>/NDK/arm64/bin/aarch64-linux-android-ar"
linker = "<上边NDK的实际路径>/NDK/arm64/bin/aarch64-linux-android-clang"
[target.armv7-linux-androideabi]
ar = "<上边NDK的实际路径>/NDK/arm/bin/arm-linux-androideabi-ar"
linker = "<上边NDK的实际路径>/NDK/arm/bin/arm-linux-androideabi-clang"
[target.i686-linux-android]
ar = "<上边NDK的实际路径>/NDK/x86/bin/i686-linux-android-ar"
linker = "<上边NDK的实际路径>/NDK/x86/bin/i686-linux-android-clang"
然后保存config文件。
6、添加Android架构到rustup中,以便在交叉编译时使用:
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android
7、然后可以通过cargo创建项目,添加配置即可。
8、最后执行编译命令即可
以下三个命令是生成不同系统架构的动态链接库的命令,按需要使用即可
cargo build --target aarch64-linux-android --release
cargo build --target armv7-linux-androideabi --release
cargo build --target i686-linux-android --release
9、编译过程中出现的问题
error: linking with `/Users/chenpengfei/Desktop/rust_work/NDK/arm64/bin/aarch64-linux-android-clang` failed: exit status: 1
|
= note: LC_ALL="C" PATH="/Users/chenpengfei/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/chenpengfei/.cargo/bin:/usr/local/bin" VSLANG="1033" "/Users/chenpengfei/Desktop/rust_work/NDK/arm64/bin/aarch64-linux-android-clang" "-Wl,--version-script=/var/folders/43/dq34hssn0cq8ww9gr28fg_dm0000gn/T/rustcXC8DNK/list" "/var/folders/43/dq34hssn0cq8ww9gr28fg_dm0000gn/T/rustcXC8DNK/symbols.o" "/Users/chenpengfei/Desktop/rust_work/rust-android/rust/target/aarch64-linux-android/release/deps/rust.rust.284674e3-cgu.6.rcgu.o" "-Wl,--as-needed" "-L" "/Users/chenpengfei/Desktop/rust_work/rust-android/rust/target/aarch64-linux-android/release/deps" "-L" "/Users/chenpengfei/Desktop/rust_work/rust-android/rust/target/release/deps" "-L" "/Users/chenpengfei/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-linux-android/lib" "-Wl,-Bstatic" "/Users/chenpengfei/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-linux-android/lib/libcompiler_builtins-a1bf1be11cbf0db9.rlib" "-Wl,-Bdynamic" "-ldl" "-llog" "-lunwind" "-ldl" "-lm" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/Users/chenpengfei/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/aarch64-linux-android/lib" "-o" "/Users/chenpengfei/Desktop/rust_work/rust-android/rust/target/aarch64-linux-android/release/deps/librust.so" "-Wl,--gc-sections" "-shared" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs"
= note: /Users/chenpengfei/Desktop/rust_work/NDK/arm64/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lunwind
clang90: error: linker command failed with exit code 1 (use -v to see invocation)
error: could not compile `rust` due to previous error
经过调试,发现是因为Mac电脑上无法安装lunwind 导致,然后使用brew进行安装,但是依然失败。
出现以下异常:
libunwind: Linux is required for this software.
后续使用其他电脑进行测试。