~~~Building and Deploying a Rust library on Android~~~
https://github.com/mozilla/rust-android-gradle
android {
...
ndkVersion '23.1.7779620'
...
}
rustup target add armv7-linux-androideabi # for arm
rustup target add aarch64-linux-android # for arm64
rustup target add i686-linux-android # for x86
rustup target add x86_64-linux-android # for x86_64
rustup target add x86_64-unknown-linux-gnu # for linux-x86-64
rustup target add x86_64-apple-darwin # for darwin x86_64 (if you have an Intel MacOS)
rustup target add aarch64-apple-darwin # for darwin arm64 (if you have a M1 MacOS)
rustup target add x86_64-pc-windows-gnu # for win32-x86-64-gnu
rustup target add x86_64-pc-windows-msvc # for win32-x86-64-msvc
...
交叉编译 cross compile
主动编译:
./gradlew cargoBuild
或者运行Android工程时自动编译
tasks.whenTaskAdded { task ->
if ((task.name == 'javaPreCompileDebug' || task.name == 'javaPreCompileRelease')) {
task.dependsOn 'cargoBuild'
}
}
https://github.com/mozilla/rust-android-gradle#specifying-ndk-toolchains
The plugin can either use prebuilt NDK toolchain binaries, or search for (and if missing, build) NDK toolchains as generated by make_standalone_toolchain.py.
A prebuilt NDK toolchain will be used if:
rust.prebuiltToolchain=true
in the per-(multi-)project${rootDir}/local.properties
prebuiltToolchain=true
in thecargo { ... }
block (if not overridden bylocal.properties
)- The discovered NDK is version 19 or higher (if not overridden per above)
The toolchains are rooted in a single Android NDK toolchain directory. In order of preference, the toolchain root directory is determined by:
rust.androidNdkToolchainDir
in the per-(multi-)project${rootDir}/local.properties
- the environment variable
ANDROID_NDK_TOOLCHAIN_DIR
${System.getProperty(java.io.tmpdir)}/rust-android-ndk-toolchains
Note that the Java system property java.io.tmpdir
is not necessarily /tmp
, including on macOS
hosts.
Each target architecture toolchain is named like $arch-$apiLevel
: for example, arm-16
or arm64-21
.
The plugin invokes Python
, Cargo
and Rustc
. In order of preference, the plugin determines what
command to invoke for Python
by:
- the value of
cargo { pythonCommand = "..." }
, if non-empty rust.pythonCommand
in${rootDir}/local.properties
- the environment variable
RUST_ANDROID_GRADLE_PYTHON_COMMAND
- the default, python
In order of preference, the plugin determines what command to invoke for Cargo
by:
- the value of
cargo { cargoCommand = "..." }
, if non-empty rust.cargoCommand
in${rootDir}/local.properties
- the environment variable
RUST_ANDROID_GRADLE_CARGO_COMMAND
- the default, cargo
In order of preference, the plugin determines what command to invoke for rustc
by:
- the value of
cargo { rustcCommand = "..." }
, if non-empty rust.rustcCommand
in${rootDir}/local.properties
- the environment variable
RUST_ANDROID_GRADLE_RUSTC_COMMAND
- the default, rustc
(Note that failure to locate rustc
is not fatal, however it may result in rebuilding the code more
often than is necessary).
Paths must be host operating system specific. For example, on Windows:
rust.pythonCommand=D:/Python/Python311/python.exe
On Linux,
env RUST_ANDROID_GRADLE_CARGO_COMMAND=$HOME/.cargo/bin/cargo ./gradlew ...
./gradlew clean assembleDebug
https://juejin.cn/post/7170696817682694152
需要安装python3
python-3.11.3-amd64.exe
python D:\Android\Sdk\ndk\23.1.7779620\build\tools\make_standalone_toolchain.py --api 21 --arch arm
--install-dir ndk/arm
python D:\Android\Sdk\ndk\23.1.7779620\build\tools\make_standalone_toolchain.py --api 21 --arch
arm64 --install-dir ndk/arm64
python D:\Android\Sdk\ndk\23.1.7779620\build\tools\make_standalone_toolchain.py --api 21 --arch x86
--install-dir ndk/x86
python D:\Android\Sdk\ndk\23.1.7779620\build\tools\make_standalone_toolchain.py --api 21 --arch
x86_64 --install-dir ndk/x86_64
D:\Python\Python311\python.exe D: \Android\Sdk\ndk\23.1.7779620\build\tools\make_standalone_toolchain.py --api 21 --arch arm --install-dir ndk/arm
...