Replies: 1 comment 2 replies
-
Hi, TL;DR, as a maintainer here, yeah I'm familiar with both architectures but I probably wouldn't end up saying that one is categorically better than the other other - they both have their place with different trade offs. As a random example, the official native programming model for Meta Quest is based around In the context of Originally this crate was essentially written as a shim over android_native_app_glue.c and it was intentionally adhering to the architecture that For anyone that's entirely allergic to touching Java/Kotlin then The other benefit from the native thread architecture is that you can approximate a more traditional native programming model with a For example I was working at Embark Studios for a while on a cross-platform game engine written in Rust and it made sense in that case to be able to run an event loop in a native main thread, with minimal architectural differences compared to other platforms. But there are also significant limitations with the Even very basic things like using Intents and requesting permissions can't be done without writing some Java code so I personally don't find it super compelling to try and pretend that it's ever possible to build Android applications without needing to write at least some support code in Java/Kotlin. A disadvantage (imo) with the threaded architecture is that it adds a layer of indirection between your native code and the Java main thread which receives a lot of important event callbacks that applications care about. The Java APIs also aren't all thread safe and may only be usable from the main java thread.
Even though I'm the maintainer for this crate I do think that the non-threaded, native library architecture makes a lot of sense in many cases. Especially for applications that want to build UIs using Android's standard toolkit then if you want to write a lot of business logic in Rust it would make more sense in that case to have a Rust library that you invoke via JNI. The two models aren't even entirely mutually exclusive. Assuming that you end up writing some kind of As it is, There might be opportunities for android-activity to streamline more hybrid models, but while I think in the end I'd expect to combine elements of both. Even if you're running your app in a I would also keep in mind that JNI can be quite a pain and so there are also trade offs with how to keep that managable if you lean into creating lots of Rust library code that's trying to drive a lot of Java interaction. Sometimes it's also simpler to accept writing some amount of Java / Kotlin code so you can distil a smaller surface area to access via JNI. While you're investigating what you want for your app, you might want to investigate this android-view project to see what it might look like to go with more of a single-threaded architecture: https://github.com/rust-mobile/android-view It's probably fair to say that If you want to render with opengles or vulkan / wgpu then you almost certainly don't want to try and render from the UI thread, and so then it could be reasonable to go for a threaded model for rendering at least. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I wonder if the implementors here looked into this at some point and have an answer. It seems the usual "native" (non JVM) app in android uses a separate thread for a native main/event loop. That's what this crate supports. But it also seems like you could build something where the main Activity class is in Java/Kotlin, and it has a bunch of glue methods that forward to Rust via JNI, without creating a second thread. (Some talk about this in an ndk discussion thread)
It would have more Java glue but they would be conceptually trivial forwarding methods for the most part. Did you all look into this at some point and decide against it for some reason, or did you go straight for the 2-thread style?
Beta Was this translation helpful? Give feedback.
All reactions