rinf: Problems building Android apk because of a third-party crate that depends on `libc++`
Report
I’ve built a simple test project app in rinf, that compiles fine into a Windows 10 app but fails on it’s android apk build.
Notes:
-
Windows build works fine, this is android related
-
that the error occurs on
await Rinf.initialize(handleSignal); -
I tested to build an android apk on a rinf free flutter project and it works fine => I think I have isolated the problem to something rtinf related.
-
This was tried but had no effect on this problem: https://rinf.cunarist.com/frequently-asked-questions/#i-encounter-an-error-related-to-loading-native-libraries-on-older-android-versions
Archive image of docs in case of change console.log("I'm a code block!");
The error:
Exception has occurred.
ArgumentError (Invalid argument(s): Failed to load dynamic library 'libhub.so': dlopen failed: cannot locate symbol "__cxa_pure_virtual" referenced by "/data/app/~~_mXqJx6uWI4qtMfTdu-8Dw==/com.example.flutter_times_app-ULEB3K6eK4BvwfNRNEoEyA==/lib/x86_64/libhub.so"...)
Steps to Reproduce
- set up a basic flutter app with a button that makes a
[RINF:DART-SIGNAL]call. - Make sure it compiles to a desktop app and everything works as intended.
- Try building an android apk using an android studio emulator plus visual code debugger
- –> ArgumentError (Invalid argument(s): Failed to load dynamic library ‘libhub.so’
I used the visual code debugger to get more info out of the build process. When building from the CLI I managed to build an apk that didn’t work, but got no build errors. The Visual code debugger was more information rich about the problem, in my experience.
System Information
I have only tested building android apk files on rinf 6.0.0-alpha and -beta, I did not do any android build attempts when running rinf 5.x.
The android emulator version used was: Pixel 7 API 31, Android 12.0 "S"
rustc 1.75.0 (82e1608df 2023-12-21)
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.16.7, on Microsoft Windows [Version 10.0.19045.3930], locale en-UK)
[✓] Windows Version (Installed version of Windows is version 10 or higher)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Chrome - develop for the web
[✓] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.4)
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.85.2)
[✓] Connected device (4 available)
[✓] Network resources
• No issues found!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 43 (43 by maintainers)
Cool! I was away over the weekend, so didn’t read back until now.
It was a good time working with you! This is a great project 😃
Closing as the best way to fix this is to create custom
build.rsfile.This information was added to the docs, please feel free to continue the discussion when you have any updates 😃
Since it had no effect, I never mentioned this crate in the reported troubleshooting above. I did try to use this, but after some testing I realized that it expects that
libc++_shared.cois already available on the system, which it isn’t in the case of android apk files, apparently. So I still had to add the library manually to the android build for it to not generate any runtime errors.tl:dr; the
link-cpluspluscrate don’t actually contain the library, it just helps the linker to recognize the one you specify and expects it’s already on the system. See it’s repo the file isn’t contained within it, and I’ve already tested it without success.Would it be possible to “by some rinf config option” have it toggle the inclusion of
libc++_shared.soexclusively on android? If not, I think maybe just leave this as a guide in the documentation about when the file might be needed and how to include it manually in the flutter project structure; like I did it.Automating it is nice but as you say it’s also nice avoiding bloats and potential collisions with other things.
Yeah, maybe we can provide that feature from Rinf.
I have another idea to do that. We can maybe use ‘cargo feature’, so that users can edit their
Cargo.tomlfile to linklibc++to theirhubcrate.This can be much easier to implement than using
pubspec.yamlin my humble opinion.Yeah, maybe some research could be done to check for potential collsions. We can discuss more and gather helpful links and articles here. I wanted to apply this idea of
libc++quickly, but maybe we can be a bit more careful.Again, thank you for your patience 😃
As far as I understand it
libc++contains a bunch of macros, and even is my program didn’t use them it was looking for their API and panicked when those definitions were missing. I draw this conclusion since it seems like it was possible to fix the problem by providing “dummy” function handles with the same names they were asking for in the error messages. (When I realized this, I decided to add the real file instead because it felt like less work and safer.)Considering your experimental results:
Android is the only OS that `rodio` was not working on
I think that suggests that the library is already provided by all the other OS platforms. So if looking to have rinf automate the addition of the library, I think that would only be needed on
flutter build apk / appbundle, if possible. And then we could use that one targeted for the android system. (copies of these files can be found in the NDK installed by Android Studio, that’s from where I got my file.)The one thing to consider is if there’s any risk of collisions with something else also adding the same library or if that’s just an overwrite that won’t matter.
Interesting development to see that it works everywhere except Android of all places. My guess is that the other OS platforms provide libc++ on path on one way or the other, while Android has chosen not to do so and instead compartmentalize code more.
I say pass on adding it to rinf for now. It’s better to side on the side of “less is more”. However
libc++is a special case due to how widely used it is, so maybe. But only if there’s lots and lots of problems like this. At least we could add some guides in the docs on what to do in case of this type of problem.There’s a certain class of problems that will be more common in the domain of using rinf, that won’t be directly rinf related. I think there’s a case for having something in the docs that could try give a helping hand on such issues. I did write a blogpost on how to add
libc++to a flutter androud build: https://tech.victoriee.com/blog/2024/01/22/adding-custom-libraries-to-the-flutter-apk-build/And maybe when we find problems that could be solved like this, that aren’t rinf specific but rinf use related, we could document a suggested solution for them. Just an idea. I think such a section would differ from the F.A.Q. section of the documentation site, and be part of some troubleshooting section instead. I could make a pull request, if it sounds reasonable as a suggestion.
Okay, so I’ve tested Rinf with
rodioon all 6 platforms:It turned out that Android is the only OS that
rodiowas not working on, with those symbol errors that you’ve mentioned.Now it’s the step to discuss what we can do. I can suggest making a PR at
rodiorepository to linklibc++withbuild.rsfile. Includinglibc++by default in Rinf might be bloated, what do you think, @VictorieeMan ?I assume that
librinf.soshould have been generated by Flutter, since Rinf at pub.dev is a ‘flutter ffi plugin’. This file doesn’t really have any functionality.libhub.sois the one generated by Rinf’s Rust builder(Cargokit), including the main Rust logic inside thehubcrate.I believe
libc++could be embedded inlibhub.so, but not by thehubcrate, but by therinfcrate which is a dependency(maybe withbuild.rs).I will test
rodioon Android and iOS soon, thank you so much for your effort 😃I’m curious, about something rinf related here!
If I make the effort of unpacking the generated apk file and search in the
lib/folder on a vanilla rinf project I find three*.solibrary files. These are:libflutter.so,libhub.soandlibrinf.so. I presume that both the libhub & librinf are related to rinf directly, while the libflutter is in every flutter build.What are in these rinf related libraries? Is it something that rinf generates based on the crates used in the rust code?
If adding libc++ would that then be embedded in one of these files or added separately? In my solution of the project, I added it separately, so I have an extra file in my unpacked apk.
Apparently Xcode for MacOS is needed to build iOS apps D’oh! I thought I could solve this by some Linux magic, but didn’t find anything good enough today.
I’ll put this on hold until I can borrow a Mac
I thought I should mention that I’m totally open to embedding
libc++by default in therinfcrate. We just need to determine for which platforms we should, and what consequences it will have 😃Will do!
Okay, take your time 😄 Please feel free to mention me here when you have any updates.
Yeah, that’s an interesting idea 😃
Maybe we can optionally let the users include
libc++by adding a field topubspec.yaml, just like what we do with message generation paths.However, it’s unclear that if the absense of
libc++library is only problematic on Android, or is problematic on other platforms like iOS as well. Do you mind testing if your app withrodioworks well on iOS(Without the solution we’ve found above)?Thanks! feel free to re-open it when you are facing problems related to Rinf 😃
Very thankful for your speedy troubleshooting @temeddix! Keep up the good work, rinf is great and the new 6.0.0 version very easy to deal with.
Great to see that the solution is coming into sight!
It is possible to conditionally add a dependency, and use it conditionally like this in Rust.
In Dart, you can achieve something like that with this code:
Your idea looks intersting, but platform-dependent logic could be achieved without the help of Rinf, in my humble opinion.
Thank you very much for the valuable report, If I’ve missed something, please let me know 😃
Very interesting and unexpected. This also explains why it gets triggered in the Rinf.initialize() function.
I’ll take a took at that suggested solution in the code you linked.
ps: I wonder… Would it be useful/feasible if it was possible to tell rinf to do some platform specific stuff? The rodio crate solves a problem that the
pub get audioplayerscouldn’t solve for my windows application, but that I think would work out of the box for the android version. So in my case it might work to useaudioplayersin dart for android, and my rinfrodiosolution on windows. But yes, Ideally I could use the rinf solution cross-platform!ds.
It looks like there is an issue with
rodiowhen building it for Android. Some people are talking about the exact same problem that you’re facing. They also have solutions to fix this.Could you try that simple solution written there, and tell me if the problem persists?
<del>I do think that it would be better if this could be solved by Rinf’s build process, if it’s related.</del> This seems to be the issue with
rodioitself 😐Okay, thanks for sharing the problem. So for some reason,
rodiocrate is preventing our Rust crate from being compiled into.sodynamic library. I will try to inspectrodio.Alright, now I have reimplemented the project by slowly adding things until the problem reiterated. And I’ve found that it comes down to me adding this
rodio = "0.17.3"in mynative\hub\Cargo.tomland then implementing it within a function in a custom.rs file that runs apub async fnvery similar to the one given in the tutorial page at native/hub/src/sample_functions.rs. I didn’t notice this cargo crate to be of importance until now.Just as before and reported in the issue, the desktop version of this app works just fine. And if I remove this rust crate from the rust_backend code then it also works to run in an android emulator again without the given error concerning
Failed to load dynamic library 'libhub.so'.Or possibly, could you try the example app, located in
./flutter_ffi_plugin/exampleof this repository? (as well as creating a new app)Following the guide in the given link:
ndkVersioninandroid/app/build.gradle, check!All these things was already in place. There must be something more hidden at play.
I’ll give a clean flutter-rinf app a try
I don’t think it’s likely that it’s a migration problem since the build process is identical between version 5 and 6. However, using a clean Flutter app might be worth a try, I’ll be waiting 😃
I’ll have a look, now!
On a side note, what I’m thinking is that I might have come across some migration problem between rinf 5.4 and rinf 6.0 since I also managed to load a clean flutter-rinf project template into an android apk. I’ll go through the linked guide and see if it helps.
The app is also working fine on Pixel 7 API 31 on my Windows 11 environment.
Also, the ‘user installation’ GitHub action is telling us that building the app is working fine.
I’ll need to investigate deeper. Please tell me if you have any updates!
Hi, nice to see you here 😃
I’m on Windows 11, with Pixel 5 API 34 emulator running. It seems that the example app(
./flutter_ffi_plugin/example) is working fine on my environment. I also checked my custom app using Rinf 6 alpha version, and it was running fine on Android.It looks like the app builds properly, but somehow loading the dynamic library(
.so) is problematic in your environment.I’ll try to check on Pixel 7 API 31 like you, will be back soon.