ndk icon indicating copy to clipboard operation
ndk copied to clipboard

[FR] Rust in the NDK?

Open Zingam opened this issue 4 years ago • 9 comments
trafficstars

After reading this blog: https://security.googleblog.com/2021/04/rust-in-android-platform.html on Android Platform adopting Rust I wonder if there are any ideas to add Rust support to the NDK (like including a compiler). I never understood what is the actual relation between the Android Platform and the NDK. If I recall correctly there were blog post years ago. What do these Rust related developments in the Android Platform mean for the NDK?

Zingam avatar Apr 13 '21 05:04 Zingam

Surprised it took so long for someone to ask :)

We don't currently have any plans to provide build system or IDE support for Rust in apps, or to distribute the Rust toolchain in the NDK.

That said, Rust itself already supports building for Android. As long as your libraries are built for Android and enable the various binary security features required by the loader (AFAIK the Rust toolchain enables these automatically), Rust libraries work just fine in apps. We have ensured that if developers do use Rust in their apps that these will work correctly with the entire publishing and reporting pipelines, including Play. There are numerous apps today that use Rust. Firefox is the obvious example but they're not alone :)

One thing we'd really love to know here is what use cases folks have in mind for Rust. Rust is a native language (i.e. distributed as ELF, not dex) so it would have the same level of API access that C++ does in the NDK. You'd still need to handle most APIs across JNI, and you'd still need to worry about per-arch differences (as well as distribute binaries for every ABI). I can think of a few reasons on my own (I'm also a fan of Rust, FWIW), but I'm curious which parts are most important to actual app developers.

I never understood what is the actual relation between the Android Platform and the NDK.

"The platform" is roughly "the OS itself". All the libraries that your app uses from the device, the framework, the zygote, etc.

The way the NDK relates to that is that the NDK includes stub definitions of the libraries that the platform exposes to apps. That's what you're getting when you link against libc.so, a stub. The device provides the implementation rather than bundling that library in your app.

DanAlbert avatar Apr 13 '21 21:04 DanAlbert

I am not familiar with NDK at all. My Android dev days are 6 years old and I was coding in Kotlin. My dream would be to write Android apps 100% in Rust, UI included. I understand that I would need to take care of the multiple arch out there and as I said, I'm not familiar with NDK at all, maybe it's already doable with NDK so I just need to learn how to do it :)

Geobert avatar Apr 15 '21 13:04 Geobert

@DanAlbert Thank you for the detailed answer. Somebody had to do it and I lost patience waiting for the question I guess. I was making some imaginary connections I guess: CLion adding Rust support, Linux adding Rust support, Android, etc... and maybe AS...

Zingam avatar Apr 15 '21 16:04 Zingam

My dream would be to write Android apps 100% in Rust, UI included.

Rust is a native language so it will only have direct access to the NDK APIs. The Android APIs that for the UI are all Java. You'd need to use those APIs through JNI, and you can do that today. It will almost certainly not be worth the effort. Kotlin is the way to go if you want a modern language to write your whole app in.

CLion adding Rust support

That does certainly help :) AS's C++ support is built on CLion and there's still quite a lot of additional work that goes into that, so I suspect Rust would similarly need quite a bit more work. Also would need a way to build Rust from AGP.

DanAlbert avatar Apr 15 '21 19:04 DanAlbert

A quick search reveals various projects by third parties to support Rust in Android:

https://plugins.gradle.org/search?term=rust

https://github.com/mozilla/rust-android-gradle https://github.com/AndrewGaspar/corrosion

Zingam avatar Apr 15 '21 20:04 Zingam

https://docs.rs/ndk/0.3.0/ndk/index.html might also be interesting, though it looks rather incomplete.

DanAlbert avatar Apr 15 '21 20:04 DanAlbert

https://docs.rs/ndk/0.3.0/ndk/index.html might also be interesting, though it looks rather incomplete.

So maybe that could be a step in a better support of Rust in the NDK: official Rust bindings?

Geobert avatar Apr 19 '21 16:04 Geobert

Yes, if we were to officially support Rust that would be one of the things we'd need to do.

DanAlbert avatar Apr 19 '21 17:04 DanAlbert

FYI, I have posted https://github.com/android/ndk/issues/1742 with more detail about what's needed here. The answer hasn't changed (not funded, not supported, no timeline), but that explains why this is not a simple matter of "build rustc, ship it".

DanAlbert avatar Aug 01 '22 20:08 DanAlbert