While most people associate Java and maybe Kotlin with Android, the low-level parts of the mobile OS are written in C and C++. But Google is now adding support for Rust for OS development.
“The Android Open Source Project (AOSP) now supports the Rust programming language for developing the OS itself,” Jeff Vander Stoep and Stephen Hines of the Android Team write in the announcement post. “For the lower layers of the OS, Java and Kotlin are not an option. Lower levels of the OS require systems programming languages like C, C++, and Rust. These languages are designed with control and predictability as goals. They provide access to low-level system resources and hardware. They are light on resources and have more predictable performance characteristics.”
Looking at a graphic that describes the architecture of Android (and its open-source AOSP underpinnings), it appears that the bootloader, Trusted Execution Environment (TEE), fastboot/recovery, hypervisor, kernel, drivers, and hardware abstraction layers (HALs) are all written in C/C++ today, while some other components, like media and Bluetooth/NFC are written in a combination of those lower-level languages and Java/Kotlin. Apps and the frameworks that developers use to write apps are written in Java/Kotlin.
So why add support for Rust? According to Google, Rust provides the same low-level control and performance as C and C++ but adds memory protection, which it says consistently represents about 70 percent of Android’s high severity security vulnerabilities. Today, Google is forced to sandbox C/C++ code to provide memory protection, but sandboxing harms performance. With Rust, Google can add new features to Android/AOSP that are both safer and lighter on resources.
As for the tens of millions of lines of C/C++ code that are currently in Android/AOSP, Google says that rewriting them all is simply not feasible and that it will focus its Rust efforts on new development and not on rewriting mature C/C++ code.
“Adding a new language to the Android platform is a large undertaking,” the two conclude. “There are toolchains and dependencies that need to be maintained, test infrastructure and tooling that must be updated, and developers that need to be trained. For the past 18 months, we have been adding Rust support to the Android Open Source Project, and we have a few early adopter projects that we will be sharing in the coming months. Scaling this to more of the OS is a multi-year project.”