Android Now Supports Rust for Low-Level Code

Posted on April 6, 2021 by Paul Thurrott in Android, Dev, Mobile with 6 Comments

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

Tagged with , , ,

Join the discussion!

BECOME A THURROTT MEMBER:

Don't have a login but want to join the conversation? Become a Thurrott Premium or Basic User to participate

Register
Comments (6)

6 responses to “Android Now Supports Rust for Low-Level Code”

  1. Avatar

    sentinel6671

    This is really neat. I'm in the process of learning Rust. Loving it, it's a great language with terrific tools.

  2. Avatar

    martinusv2

    Does Microsoft uses Rust for Windows Defender or the like?

  3. Avatar

    davehelps

    Did anyone else think "Android has a hypervisor?" 🧐

  4. Avatar

    samp

    *sigh*. Another previously unknown code to add to the to-do list.

  5. Avatar

    bkkcanuck

    I have been tinkering with Rust for around 2 years now (though my current contract has not allowed me any time to devote to it in 6 to 12 months unfortunately). Rust is one of the most 'exciting' 'new' languages out there. The widely used languages tended to fall into two camps when it came to efficiency ... unsafe but efficient (C and C++) or safe but not efficient (Java). On top of this underlying division of languages the developer mentality of those committed to each camp only makes things worse .... Those that are committed to efficient languages tend to take care to ensure their code is more efficient (generally speaking), while those writing java often give no thought to it -- unless it becomes too much of an issue to ignore. C and C++ tend to leave. In fact many Java developers don't give any thought to memory management since the java has a garbage collector to deal with automatically cleaning up the memory behind the scene -- but this comes at a cost (especially for 'lower powered' mobile devices with batteries). [Apple's Swift language does not have a garbage collector and thus will be more predictable and efficient on resources].

    Rust is an interesting language since it implemented a borrow checker and lifetimes to manage memory at compile-time rather at runtime like garbage collected languages -- allowing it to basically give you the best of both worlds. Rust could be considered a low-level (closer to bare-metal) language with a lot of high level abstractions which makes it efficient to develop software in, or a high level language that gives you the benefits of being able to write efficient bare-metal applications -- all while eliminating a whole class of defects and vulnerabilities that make up 70% of the security vulnerabilities in existing software.

    The Rust community is very a very large and very active community... and I have been amazed at the dedication to building 3rd party support -- which bodes well for it's long-term viability. The recent spinning off of Rust into it's own foundation (until this year it was part of Mozilla) seems to have been even more beneficial since although it was always open, being part of another foundation has unfortunately given some pause in thinking it was a 'Mozilla' owned language.

    We now seem to be seeing more interest from Google, Microsoft, and also Linux. I was pleasantly surprised to see Linus Torvalds (famous for C is the only real language that he would consider [C++ is evil - which I agree with]) saying it is something that is worth looking at for peripheral support (like drivers)... Obviously, Linux is not going to be rewritten in Rust... (though there are some experimental Rust based kernel development going on) as there is literally billions invested in the C based kernel.

Leave a Reply