Google has announced more information on its integration of Rust into the Android Open Source Project (AOSP). The Android team has been at work trying to integrate Rust into the project since 2019 as a memory-safe alternative to platform-native development.
“As with any large project, introducing a new language requires careful consideration. For Android, one important area was assessing how to best fit Rust into Android’s build system,” Ivan Lozano, Android Team at Google, wrote in a post.
According to the Android team, there are several challenges to integrating Rust in large projects, such as the risk that using the Rust Compiler, rustc, instead of Cargo might remove organizations from the Rust community as a whole.
In Rust, Cargo is the default build system and package manager, but in Android that role is taken by Soong and calls rustc directly. Reasons for this include that C dependencies in Cargo are handled independently while Soong already provides a mechanism for that, calling compilers directly through Soong provides more stability and control, self-contained builds are important in Android to create reproducible builds, and incremental builds are important to maintain engineering productivity.
In addition, the team gave an answer for why it supports proc_macros, but not build.rs scripts. According to the team, this is because build.rs code is written as one-off code while proc_macros define reusable functionality, and that can be more useful to the Rust community. They also explained that proc_macros are usually better maintained, easier to deal with in the code review process, and more readily sandboxed.
The Android team also noted that when crates developed for Android could benefit the Rust community, it plans to release them as independent crates. “We believe that the success of Rust within Android is dependent on minimizing any divergence between Android and the Rust community at large, and hope that the Rust community will benefit from Android’s involvement,” Lozano wrote.
The Android team hopes to soon add documentation on how to define and use Rust modules in Soong. “We expect Android’s support for Rust to continue evolving alongside the Rust ecosystem and hope to continue to participate in discussions around how Rust can be integrated into existing build systems,” Lozano wrote.