I found a tool that allows compiling Rust to JVM bytecode (make JAR files from Rust, if you dare?) (github.com)
from ChubakPDP11@programming.dev to rust@programming.dev on 14 May 2024 14:11
https://programming.dev/post/14079228

I thought this might interest you Rust folk. This is kinda like an LLVM bitcode to JVM bytecode translator. So run rustc with –emit=llvm-ir (I think that’s the flag) and then pass the bitcode image to this program, then get JVM bytecode which you can make a JVM bytecode archive (JAR) from it. Could be an interesting? The author says it can JVM code from Clang output, so why not Rustc?

Keep in mind that these are two different beasts, JVM is designed for a safe virtual machine and LLVM is chiefly used to be translated into machine code. However, stupider and more wonderful things have been done with LLVM and JVM, for example, there’s an LLVM to 6502 translator, so you could be making NES games with Rust.

I will test this in a few days, busy implementing my own JVM (hope I can do it) and I don’t have the Rust toolchain installed on my system. But hey maybe someone can make a Cargo plugin from it (is it possible?)

Thanks, later.

#rust

threaded - newest

onlinepersona@programming.dev on 14 May 2024 15:59 next collapse

I think I had a discussion with some people about this in the fediverse and they were practically in denial that it existed 😅 Some serious java hate.

It does make me wonder how Slint allows creation of Android apps… this transpiler is quite old, so it’s doubtful slint is basing their dev on this. But it would be cool to write Android apps in pure Rust and have the toolchain spit out java bytecode with this that actually runs on android…

Anti Commercial-AI license

BB_C@programming.dev on 14 May 2024 19:06 next collapse

they were practically in denial that it existed 😅 Some serious java hate.

You seem to imply that this is something 1. needed 2. wanted. 3. somehow threatening. Which is a hilarious thought.

onlinepersona@programming.dev on 14 May 2024 20:23 collapse

Did you even read the rest of my comment?

Anti Commercial-AI license

BB_C@programming.dev on 14 May 2024 20:35 collapse

Yes. That was a part of “1. needed”.
Read about how Dart/Flutter does it.

onlinepersona@programming.dev on 14 May 2024 20:43 collapse

So because Dart/Flutter do it one way, it should be the only sanctioned way to do it? There’s only one path to Rome and every other path is sacrilege? Nothing good ever came of exploring other options, right?

Anti Commercial-AI license

BB_C@programming.dev on 14 May 2024 21:27 collapse

The problem is, if you knew anything about the subject matter, you would know that even JAVA code ends up being compiled ahead-of-time by ART to ELF binaries on Android.
Even non-devs know this stuff!

ChubakPDP11@programming.dev on 14 May 2024 23:47 collapse

Hey now, I am just a yokel from Mazar-Sharif, New York who sells dingbat carcasses for infant consumption, but does Android even use JVM? I was under the consumption it uses Dalvik? Let me consume a dingbat – ok, so who cares what “java” gets compiled to, JIT, AOT even tree-walked, we’re talking about JVM here. Here: sourceforge.net/projects/jasmin/ An ancient JVM bytecode assembler.

BB_C@programming.dev on 15 May 2024 06:34 next collapse

Maybe I should have written something simpler like “What is the shortest distance between two points, and how does it look like?”

LeFantome@programming.dev on 15 May 2024 23:21 collapse

I do not know why I al defending that guy, but the comment he is replying to specifically mentioned Android. So, he is not the one that narrowed the conversation from JVM to Android.

Also, I am sure your dingbat humour is quite hilarious. Unfortunately, it went completely over my head.

AVincentInSpace@pawb.social on 15 May 2024 07:38 collapse

We can already write Android apps in pure Rust though. Android apps are allowed to run native machine code – that’s how the Android versions of Genshin Impact, CoD, Fortnite et. al. work – and winit and wgpu support Android NativeActivity as a backend.

github.com/inferrna/hello_world_android_egui

sashin@veganism.social on 15 May 2024 07:44 next collapse

@AVincentInSpace @onlinepersona Are there guides on this? How would we do the GUIs for these apps?

AVincentInSpace@pawb.social on 15 May 2024 07:53 collapse

I dunno. I just kinda aimlessly Googled terms related to Android and Rust until I found that link.

The GitHub link in my previous comment shows a basic example of using the egui GUI framework on Android via wgpu. I haven’t found an excuse to play around with it yet, plus getting the Android NDK cross compiler working with Cargo (for crates that compile C code) is a bit of a pain, but I’d assume it works how it says on the tin.

Tell you what, gimme a minute to screw around with this and I’ll report back

XTL@sopuli.xyz on 15 May 2024 08:45 collapse

This is a thing I look for now and then, but it always seems to fall back to “install android studio” and I nope at that point. Maybe one of these days. Having the ability to make some simple app would be interesting.

AVincentInSpace@pawb.social on 15 May 2024 09:07 collapse

@sashin@vegnism.social @XTL@sopuli.xyz Alright, I’m back. Survey says: we are, indeed, Android yet!

I found this example repo in my travels: github.com/inferrna/hello_world_android_egui

It shows the same demo setup that can be experienced through a Web browser at egui.rs, only now as a native-code Android application. To compile it I had to:

  • install lld (it uses that linker I think exclusively and will complain if you don’t have it)
  • rustup target add aarch64-linux-android
  • ignore the build instructions in the repo since the tool they recommend (cargo apk) has been deprecated and no longer works (either that or i couldn’t get it working – xbuild is easier anyway)
  • cargo install xbuild
  • cargo update the repo since the version of android-activity it ships with is out of date and will crash since my version of Android passed it a null pointer it wasn’t expecting
  • connect phone to PC via adb
  • invoke xbuild to compile and deploy the app: x run --device adb:ADB_ID_GOES_HERE

I did NOT have to:

  • download Android Studio, even as the command line tools (xbuild took care of downloading the SDK and NDK for me)

(I did download them separately as part of a different troubleshooting step, but I deleted them and it continued working. Not sure what to make of this. Not about to reinstall my OS to make sure these steps are reproducible. Please let me know if they are or not!)

UPDATE: It seems egui is not able to accept text input at all – it does not know how to bring up the virtual keyboard when you click a textbox, nor does it accept keyboard input when the virtual keyboard is forced open through third party app nor through a physical USB keyboard. Still more work to be done, but I’m shocked at how much is already possible!

XTL@sopuli.xyz on 15 May 2024 11:14 collapse

Very interesting, thank you. I should give this a try when I get back to a dev machine. Sounds like it still downloads and uses the SDK which might be significant for licensing or other reasons, but I’m probably fine with that for personal use.

onlinepersona@programming.dev on 15 May 2024 09:17 collapse

They are probably using Android NDK and generating glue code that calls the compiled library with it. It wouldn’t surprise me if the actual solution is creating an activity with an OpenGL context into which apps can draw.

Regardless, I don’t see the harm of transpiling parts of the app into JVM to access the full breadth of features. Take a look at what’s missing on winit. There’s probably more, but I’m not an android dev.

Anti Commercial-AI license

AVincentInSpace@pawb.social on 15 May 2024 09:38 collapse

I don’t see the harm of transpiling parts of the app into JVM to access the full breadth of features

You mean apart from the 5x performance hit?

I’m unaware of anything the Android SDK supports that the NDK doesn’t, and the ndk-rs crate provides bindings for it. I agree that Rust on Android is not where we’d like it to be yet, especially with regard to familiar interfaces like winit, but transpiling Rust to JVM bytecode is not the solution. Come to that, it’s not the solution to any problem that currently does or (with any luck) will ever exist. Best case scenario, you shift the problem from creating bindings to the NDK to creating much uglier bindings to random Java classes through something JNI-like.

Speaking of the JNI, actually, writing Rust native code that can both be called from and call into to Java/Kotlin/other JVM code via the JNI is actually fairly straightforward.

Transpiling Rust to Java bytecode is a remarkably silly idea and I cannot fathom why anyone would ever do it, except maybe if they really cared about portability.

onlinepersona@programming.dev on 15 May 2024 10:01 collapse

Yeah, OK. You’ve already made up your mind on the issue. There’s no need to continue the discussion.

Anti Commercial-AI license

AVincentInSpace@pawb.social on 15 May 2024 10:35 collapse

No, I’m serious. Why would you ever do that? What features are available to the Java version of the Android API that aren’t available via the NDK? And if those features exist, why is writing the portions of your app that depend on those features in Kotlin and the rest in Rust infeasible?

sik0fewl@lemmy.ca on 15 May 2024 03:20 next collapse

Finally, the speed of the Rust compiler with the memory efficiency of the JVM!

LeFantome@programming.dev on 15 May 2024 20:42 collapse

We have had Ruby for a long time already.

crispy_kilt@feddit.de on 15 May 2024 07:55 next collapse

But why

verstra@programming.dev on 15 May 2024 08:16 next collapse

Well the jar does run on multiple platforms now.

XTL@sopuli.xyz on 15 May 2024 08:41 next collapse

It runs on one particularly horrible platform only. Jre.

verstra@programming.dev on 15 May 2024 19:56 collapse

Ok, I wasn’t clear: you can now run a single compiled binary on multiple platforms/architectures/operating systems.

But yea, rip performance, probably.

crispy_kilt@feddit.de on 15 May 2024 08:45 collapse

Which is fewer platforms than LLVM would provide

scarilog@lemmy.world on 15 May 2024 09:21 collapse

Why not?

crispy_kilt@feddit.de on 15 May 2024 13:55 collapse

Understandable, have a nice day

blazebra@programming.dev on 16 May 2024 23:51 collapse

Does it still work?

ChubakPDP11@programming.dev on 17 May 2024 02:20 collapse

If you use JVM 8 yes. Use Zero.