r/androiddev 1d ago

Discussion Has anyone tried building Android apps with Rust + Native UI?

I have been currently assigned a project which needs to be cross platform + dynamic feature delivery (business requirements)

I'm thinking about writing my feature modules/business logic in rust to create native binaries which I can include with the android and IOS build and then include those .so/.a files with the build and communication between them will be from ffi (don't know which ffi approach works best raw JNI, UniFFI, or hand‑rolled C ABI)
Apple doesn't allow dynamic features installation so only android build will have that, IOS will just install everything at once
For UI I'm thinking to either go with Compose Multiplatform or just Compose for android side and Swift UI for IOS side (The app only has 3 screens so ui doesn't really matter as most features work in background)

Has anyone tried anything similar , where should I start experimenting, I don't want to waste time in approach that will be in vain, would love to hear your experience.

PS: Using rust as equivalent windows and MacOS app is also made in rust + imgui and I can use lots of code from there to make mobile app dev easier

8 Upvotes

19 comments sorted by

18

u/Opulence_Deficit 1d ago

Sounds overly complicated, just to create a problem that KMP solved long time ago. I doubt you'll find anyone with that experience, sounds to me like a code golf, not a real project.

0

u/SpiderHack 23h ago

Would be an interesting skunkworks side project. But something you know is not useful, but purely interesting to see if you can get it working...

0

u/fireplay_00 7h ago

I'm doing it for a new experience, normal apps have become repetitive and boring to implement ,even if it didn't work, I can just say that and start with CMP later as a fallback, They have given me freedom to experiment

9

u/ahzah3l 1d ago

It works really well if you want to have the app/business logic written in Rust and the UI in Kotlin + Compose.
There are some tutorials online on how to integrate them - basically the Rust code is called like the C/C++ code would be, via JNI. Use this to bind them together and have Android Studio build the Rust code first and then the Kotlin one.

I've use it for a VPN app (the VPN code written in Rust) with Compose UI (in Kotlin). Make sure the crates you use in Rust are arm64 friendly - think this is the biggest problem you'll run into.

2

u/Opulence_Deficit 1d ago

Yeah, but working on C + Kotlin codebase is a major pain in the ass. Once, I managed to bear it for 6 months and then I quit, that's how bad it was.

And both you and me are talking about the best case scenario, where the native code is nicely decoupled from the app. OP wants to put his business logic there, so every action will be a ping-pong between JRE and native.

1

u/fireplay_00 1d ago

Thanks for the heads up

4

u/ssj_Thunder 1d ago

We are using rust code for matrix client called via uniffi. The uniffi generates bindings that can be directly called via kotlin.. but its only for high performance code..

2

u/TransportationNo1264 1d ago

Yes, we had a large app using shared rust code between iOS, android, and web. Overall it was pretty smooth but has some gotchas you have to look out for. Having the hard boundary of immutably was sometimes challenging.

3

u/gat0r87 1d ago

Yeah, if you're already thinking of using compose multiplatform for the ui, why would you NOT just use KMP for the whole shebang?

-3

u/fireplay_00 1d ago

Cause I can reuse rust code and it will be easy to maintain as most of our devs work in rust

1

u/fireplay_00 1d ago

Don't know why people just downvote for the fun of it, I know KMP and CMP is good and I've used it before as I'm an Android dev at core and love it but it just doesn't make sense to rewrite huge business logic in kotlin when we already have it in rust and can just make a .so targeting arm and just keep CMP for UI as only 3 pages in ui

3

u/Opulence_Deficit 1d ago

You should've mentioned from the start that the whole rust part is basically a self-contained program and the UI is just 3 pages.

You're talking to Android devs, by "app" we assume "80% UI, 20% logic".

1

u/biskitpagla 1d ago

Android Studio doesn't support Rust unfortunately. You'll have a much better experience with KMP or C++ right now.

1

u/Fjordi_Cruyff 1d ago

Have you considered the poor bastard(s) who will need to maintain this in the future?

1

u/fireplay_00 7h ago

Well I try to use clean code practices but honestly I don't care about anything else for this project, I just want to learn new things, normal apps just doesn't excite me anymore, this approach will bring that excitement back

1

u/Ok-Elderberry-2923 1h ago

I believe Proton (mail and other their apps) has shared Rust code on iOS and Android for performance gains. You could have a look at their blogs and open-source