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:

The error:

image

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

  1. set up a basic flutter app with a button that makes a [RINF:DART-SIGNAL] call.
  2. Make sure it compiles to a desktop app and everything works as intended.
  3. Try building an android apk using an android studio emulator plus visual code debugger
  4. –> 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)

Most upvoted comments

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.rs file.

This information was added to the docs, please feel free to continue the discussion when you have any updates 😃

I’ve been doing more research, and found out that there was an exact crate for that.

* https://crates.io/crates/link-cplusplus

What do you think, @VictorieeMan , I think we should guide that you need to link libc++ in some cases in the docs, including the direct link to this issue. It would explain how and why you should use the link-cplusplus crate, in such cases.

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.co is 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-cplusplus crate 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.

I was thinking about linking libc++ by default in Rinf, but multiple native library files having the same symbols getting loaded into Flutter app can be problematic, especially on iOS and macOS where static linking is used. The issue below describes how multiple Flutter packages that depend on the same allo-isolate crate creates a conflict(though this is not libc++, the mechanism is the same).

* [bug: Cannot use allo-isolate in two libraries with static linking because of symbol name duplication shekohex/allo-isolate#55](https://github.com/shekohex/allo-isolate/issues/55)

Would it be possible to “by some rinf config option” have it toggle the inclusion of libc++_shared.so exclusively 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.toml file to link libc++ to their hub crate.

This can be much easier to implement than using pubspec.yaml in 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

Okay, so I’ve tested Rinf with rodio on all 6 platforms:

* Linux ✅

* Android ❌

* Windows ✅

* macOS ✅

* iOS ✅

* Web ✅

It turned out that Android is the only OS that rodio was not working on, with those symbol errors that you’ve mentioned.

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 rodio on all 6 platforms:

  • Linux ✅
  • Android ❌
  • Windows ✅
  • macOS ✅
  • iOS ✅
  • Web ✅

It turned out that Android is the only OS that rodio was 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 rodio repository to link libc++ with build.rs file. Including libc++ by default in Rinf might be bloated, what do you think, @VictorieeMan ?

I assume that librinf.so should have been generated by Flutter, since Rinf at pub.dev is a ‘flutter ffi plugin’. This file doesn’t really have any functionality.

libhub.so is the one generated by Rinf’s Rust builder(Cargokit), including the main Rust logic inside the hub crate.

I believe libc++ could be embedded in libhub.so, but not by the hub crate, but by the rinf crate which is a dependency(maybe with build.rs).

I will test rodio on Android and iOS soon, thank you so much for your effort 😃

I thought I should mention that I’m totally open to embedding libc++ by default in the rinf crate. We just need to determine for which platforms we should, and what consequences it will have 😃

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 *.so library files. These are: libflutter.so, libhub.so and librinf.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.

That would be interesting. But ofc. we must first as you say figure out if it could cause any troubles. I’ll look into build testing rodio to an iOS app tomorrow!

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.

image

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 the rinf crate. We just need to determine for which platforms we should, and what consequences it will have 😃

Okay, take your time 😄 Please feel free to mention me here when you have any updates.

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 to pubspec.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 with rodio works 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.

# native/hub/Cargo.toml

[target.'cfg(not(target_os = "android"))'.dependencies]
rodio = "0.17.3"
#[cfg(not(target_os = "android"))]
rodio.some_function();

In Dart, you can achieve something like that with this code:

import 'package:universal_platform/universal_platform.dart';

if (Platform.isAndroid) {
  // Android-specific code
} else {
  // Non-Android 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 audioplayers couldn’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 use audioplayers in dart for android, and my rinf rodio solution on windows. But yes, Ideally I could use the rinf solution cross-platform!

  • The pro of such platform specific suggestions for rinf would be that it could be a saviour for platform specific solutions that people find no other solution for in that os environment.
  • Then con of platform specific suggestions for rinf would be increased complexity in both rinf and every time someone implements such platform specific rinf specs.

ds.

It looks like there is an issue with rodio when 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 rodio itself 😐

Okay, thanks for sharing the problem. So for some reason, rodio crate is preventing our Rust crate from being compiled into .so dynamic library. I will try to inspect rodio.

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 my native\hub\Cargo.toml and then implementing it within a function in a custom.rs file that runs a pub async fn very 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/example of this repository? (as well as creating a new app)

Following the guide in the given link:

  • Rust 1.68 or higher, check!
  • Flutter SDK 3.10 or higher, check!
  • Presence of ndkVersion in android/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 😃

Hmm, this is odd. Could you try following the guides in this section?

* https://rinf.cunarist.com/frequently-asked-questions/#android-app-build-has-failed-what-should-i-do

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.

image

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.

image

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.