Oxidized NDK - NDK repackaged with Rust toolchain
Go to file
2024-02-01 15:02:33 -08:00
.github/workflows Update action versions 2024-02-01 15:02:33 -08:00
patches Update Rust to 1.75.0 2024-01-08 12:29:46 -08:00
.gitignore Use the same LLVM source code as NDK 2023-08-31 19:54:46 -07:00
build-windows.sh Fix NDK download and extraction 2023-09-01 03:13:53 -07:00
build.sh No longer need cross compilation 2024-01-31 18:39:59 -08:00
clean.sh Use the same LLVM source code as NDK 2023-08-31 19:54:46 -07:00
common.sh Try to cross compile for macOS 2024-01-11 15:14:03 -08:00
config-darwin.toml Update Rust to 1.75.0 2024-01-08 12:29:46 -08:00
config-linux.toml Update Rust to 1.75.0 2024-01-08 12:29:46 -08:00
config-windows.toml Update Rust to 1.75.0 2024-01-08 12:29:46 -08:00
dist-macos.sh No longer need cross compilation 2024-01-31 18:39:59 -08:00
LICENSE Add README and LICENSE 2022-04-11 04:32:39 -07:00
README.md Update patches 2022-06-29 20:17:56 -07:00

Oxidized NDK

This is not an officially supported Google product.

Oxidized NDK (ONDK) is an unofficial repackaged Android NDK that includes a Rust toolchain. This repository hosts build scripts to build and package ONDK using GitHub Actions. Every file included in the final package either originates from the official NDK zip or is built in GitHub Actions.

This project does not include or attempt to do any modifications to Rust and LLVM.
This project is for experimental purposes, and does not guarantee any Android NDK or Rust functionality.
Use at your own risk.

Download the latest ONDK in releases.

Supports all NDK host platforms:
Linux (x64), Windows (x64), and macOS (x64 + arm64, universal binaries).

How to Use

For building C/C++ code, ONDK is just like any ordinary Android NDK, no special configurations are needed.

For building Rust code, link ONDK's Rust toolchain with rustup:

rustup toolchain link <name> <ondk>/toolchains/rust

The std crate is intentionally not prebuilt and included in ONDK, so building requires a little bit of setup.
Here is an example for building a project targeting API 21 for ARM64:

LLVM_BIN="<ondk>/toolchains/llvm/prebuilt/<os>-x86_64/bin"

# We need to tell cargo where to find the NDK linker for Android.
# You can also set this in config.toml, check the official documentation.
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="$LLVM_BIN/aarch64-linux-android21-clang"

# We also need to specify the NDK C compiler with proper flags.
# This is used for feature detection internally in std's build system.
export TARGET_CC="$LLVM_BIN/clang"
export TARGET_CFLAGS='--target=aarch64-linux-android21'

# Finally, build our project with -Z build-std
cargo <+name> build -Z build-std --target aarch64-linux-android

P.S. I strongly recommend checking out min-sized-rust to minimize Rust binaries.

How ONDK is Built

  • Download the latest Rust source code
  • Apply some patches to its build system (no patches to any code that is part of the final product)
  • Build a Rust sysroot with rustc + cargo + std source code + clang
  • Download the latest stable NDK zip
  • Replace only the LLVM/Clang executables of NDK, and copy the Rust sysroot into the package

FAQ

  • Q: Why do we need this? Doesn't the official Rust toolchains already work for Android?
    A: I started this project because I wanted to enable Cross-Language LTO. This allows C/C++ and Rust code to be optimized together during link time, extremely useful for mixed language projects. However, because NDK's Clang and official Rust toolchains are not built with the same LLVM, their LTO output format is very likely to be incompatible with each other. ONDK builds rustc and clang from scratch with the same LLVM source code to ensure cross-language LTO compatibility.

  • Q: Why is std not prebuilt for Android targets?
    A: I personally strongly dislike the idea of prebuilding any dependency that will be included in the final product of a project. Building std along with the project's compilation pipeline makes it easy to disable features, such as removing unwind for panic, and allow std to be LTO-ed with your project. The toolchain should not forcefully include and link code that is unnecessary for your project!

License

Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.