Google reveals more information about integrating Rust into Android

Since 2019, the Android team has been committed to introducing the Rust programming language into AOSP (Android Open Source Project) as a memory-safe alternative to platform native code development. Recently, Google further elaborated on the integration of Rust into AOSP in a blog post.

Google pointed out that, like any large project, introducing a new language requires careful consideration. For Android, an important aspect is to evaluate how to best integrate Rust into Android’s build system. According to the Android team, there are many challenges in integrating Rust into large projects; for example, abandoning Cargo and directly using the Rust compiler rustc may remove organizations from the wider Rust community.

The team also stated that when crates developed for Android can benefit the Rust community, they hope to release them as independent crates, and believe that the success of Rust in Android depends on minimizing the differences between Android and the entire Rust community, and hope that the Rust community can benefit from Android’s participation.

Rust provides Cargo as the default build system and package manager, collects dependencies, and calls rustc (Rust compiler) to build the target crate (Rust package). In Android, Soong replaced this role and directly called rustc. The reason is:

  • The C language dependency in Cargo is handled independently, and Soong has provided related mechanisms
  • Directly invoking the compiler through Soong can provide more stability and control required by Android to support various build configurations
  • Independent builds are very important for Android to create repeatable builds
  • The incremental build is very important to maintain engineering productivity
Ivan Lozano of the Android team said
Using the Rust compiler directly allows us to avoid these issues and is consistent with how we compile all other code in AOSP. It provides the most control over the build process and eases integration into Android’s existing build system. Unfortunately, avoiding it introduces several challenges and influences many other build system decisions because Cargo usage is so deeply ingrained in the Rust crate ecosystem.
The Android team also revealed that they plan to add documentation on how to define and use Rust modules in Soong in source.android.com in the near future. It hopes that Android’s support for Rust will continue to develop with the Rust ecosystem, and it hopes to continue to participate in discussions on how to integrate Rust into the existing build system.