Ashoat / rust-in-react-native-demo

Demo of how to use Rust in React Native
BSD 3-Clause "New" or "Revised" License
26 stars 1 forks source link

Is it running on Android? #3

Open tharyckgusmao opened 10 months ago

tharyckgusmao commented 10 months ago

Hello, I would like to thank you for sharing your knowledge, I discovered your work through a pusher video.

I spent yesterday studying your example of integration with Rust and I couldn't make it work on Android.

For some reason at compile time it can't find the method "rustAdd" in my case "rustSum", I tried different ways, adjusting cmakelist, adjusting include headers, but it can't find the method reference.

Do you have any ideas or solutions for this scenario? Thank you very much

On run Yarn android:

image image image

Ashoat commented 10 months ago

Ugh sorry – I think the Android version here doesn't work correctly. We have a project up https://github.com/CommE2E/comm that uses it in a way that works with Android, but it doesn't go through TurboModules, so the code is just a little different.

We're currently working through a more specific Android issue, but it also has to do with a linker error... cc @vdhanan

timmyjose commented 10 months ago

Same here (albeit different error messages) - tried building for Android, but couldn't. Any plans to patch this MVP?

timmyjose commented 8 months ago

@Ashoat @vdhanan

I'm sorry - I know this is not the right place, but since you folks are the only ones having done work similar to what we're trying to do (RN + Turbo Modules + Rust), please forgive me for asking here (again).

So we managed to get a working setup for both iOS and Android. We use Turbo Modules (via create-react-native-library, so a bit roundabout) and generate the Rust crate ahead of time via custom scripts.

The iOS static Rust library is around 969M, and when built tor production/Release, the generated binary is around 21-30MB.

The Android static libraries are around 500MB for each ABI/arch/platform, but the generated aab file is around 1GB, and the extracted (tested using bundletool) apk file is around 500MB! This is making it impossible to submit to the Play Store.

This discrepancy is making no sense to me (am a newbie to mobile, RN, and Android/iOS, learning on the go!).

I'm wondering, since you folks use Rust extensively for comm (from what I've seen), if you faced similar issues, or if you use a different way of linking etc.?

Any insights/advice/ideas/help would be deeply appreciated! 🙏

timmyjose commented 8 months ago

@Ashoat @vdhanan

I'm sorry - I know this is not the right place, but since you folks are the only ones having done work similar to what we're trying to do (RN + Turbo Modules + Rust), please forgive me for asking here (again).

So we managed to get a working setup for both iOS and Android. We use Turbo Modules (via create-react-native-library, so a bit roundabout) and generate the Rust crate ahead of time via custom scripts.

The iOS static Rust library is around 969M, and when built tor production/Release, the generated binary is around 21-30MB.

The Android static libraries are around 500MB for each ABI/arch/platform, but the generated aab file is around 1GB, and the extracted (tested using bundletool) apk file is around 500MB! This is making it impossible to submit to the Play Store.

This discrepancy is making no sense to me (am a newbie to mobile, RN, and Android/iOS, learning on the go!).

I'm wondering, since you folks use Rust extensively for comm (from what I've seen), if you faced similar issues, or if you use a different way of linking etc.?

Any insights/advice/ideas/help would be deeply appreciated! 🙏

Okay, finally a solution for this (long and painful route, but will share only the end result). Thanks to @kim_nl for suggesting apk analyzer - that helped us realise that the Rust code was not getting optimised at all (for Android) whereas iOS was seemingly optimising a 969MB static libary into a 20MB binary. After plenty of experimentation, we were able to use Cargo's optimisations to get down the static libraries for Android down from around 500MB per arch/abi to around 54MB per arch/abi! So now the finaly Android apk size is around 53MB (with an 80MB aab), which is great!

Sharing this here so that future folks looking at using Rust libraries for mobile.

I think the reason why the iOS one was optimising so much out is that for iOS, unless i'm mistaken, it's native throughout whereas for Android, you have the added complications of the Java world. Just a conjecture, since as someone pointed out on Discord, the toolchain is pretty much the same - LLVM.

(Putting it here in case people come across a similar issue!)