Skip to content

angcyo/AndroidRustDemo

Repository files navigation

2023-5-17

Android NDK 独立工具链

将 NDK 与其他构建系统配合使用

~~~Building and Deploying a Rust library on Android~~~

cargo

crates

插件

https://github.com/mozilla/rust-android-gradle

rust jni 依赖

https://crates.io/crates/jni

指定NDK版本

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'
    }
}

指定NDK工具链

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 the cargo { ... } block (if not overridden by local.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.

指定子工具命令路径

https://github.com/mozilla/rust-android-gradle#specifying-paths-to-sub-commands-python-cargo-and-rustc

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 ...

rust代码修改之后,需要重新编译rebuild

./gradlew clean assembleDebug

另一种方式

https://juejin.cn/post/7170696817682694152

生成NDK工具链

需要安装python3 python-3.11.3-amd64.exe

https://www.python.org/


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

...

About

2023-5-18

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published