Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Rust in the NDK? #1487

Open
Zingam opened this issue Apr 13, 2021 · 9 comments
Open

[FR] Rust in the NDK? #1487

Zingam opened this issue Apr 13, 2021 · 9 comments

Comments

@Zingam
Copy link

Zingam commented Apr 13, 2021

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?

@DanAlbert
Copy link
Member

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.

@Geobert
Copy link

Geobert commented Apr 15, 2021

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 :)

@Zingam
Copy link
Author

Zingam commented Apr 15, 2021

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

@DanAlbert
Copy link
Member

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.

@Zingam
Copy link
Author

Zingam commented Apr 15, 2021

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

@DanAlbert
Copy link
Member

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

@Geobert
Copy link

Geobert commented Apr 19, 2021

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?

@DanAlbert
Copy link
Member

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

@DanAlbert
Copy link
Member

FYI, I have posted #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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants