Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load dynamic library with FFI on some devices #73318

Closed
jaween opened this issue Jan 5, 2021 · 3 comments
Closed

Unable to load dynamic library with FFI on some devices #73318

jaween opened this issue Jan 5, 2021 · 3 comments
Labels
dependency: dart Dart team may need to help us platform-android Android applications specifically

Comments

@jaween
Copy link

jaween commented Jan 5, 2021

My Flutter app distributed on the Google Play store seems to be unable to load a packaged dynamic library on a few devices. They are ARM devices I believe, and the APK distributed to those devices do seem to contain the libraster.so library (built and packaged using Cmake/NDK. It is difficult for me to test this as I don't have these particular devices to try to reproduce the test on.

Steps to Reproduce

  1. Create a Flutter app that packages in an NDK built library
  2. Run the app on one of the devices listed below
  3. Load the library at runtime in Flutter using the FFI
  4. Experience an ArgumentException

Expected results:
The library is loaded successfully.

Actual results:
The library is unable to be found and so can not be loaded with the following error:

ArgumentException
 Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libraster.so" not found)

Affected devices

Model Name Android version
SM-G928F Samsung Galaxy S6 Edge+ 6.0.1
SM-J500M Samsung Galaxy J5 6.0.1
SM-J710GN Samsung Galaxy J7 2016 6.0.1
SM-T110 Samsung Galaxy Tab 3 Lite 7.0 4.2.2
SM-T111M Samsung Galaxy Tab 3 Lite 7.0 4.2.2
GT-I8262 Samsung Galaxy Core Duos 4.1.2
GT-I8552 Samsung Galaxy Win Duos 4.1.2
GT-I8552B Samsung Galaxy Win Duos 4.1.2
GT-I9082L Samsung Galaxy Grand Duos 4.2.2
GT-I9300 Samsung Galaxy S III 4.1.2
GT-N8000 Samsung Galaxy Note 10.1 4.1.2
GT-N8010 Samsung Galaxy Note 10.1 4.1.2
GT-P3110 Samsung Galaxy Tab 2 7.0 4.1.2
GT-P5110 Samsung Galaxy Tab 2 10.1 4.2.2
SO-03E Sony Xperia Tablet Z 4.1.2
B1-A71 Acer Iconia Tab B1-A71 4.1.2
F-01F Fujitsu Arrows NX F-01F 4.2.2
ME173X Asus Memo Pad HD7 4.2.2

Since the library does load successfully on many other devices, I'm unsure what the cause of this could be. Assuming this is a Dart/Flutter related issue, I have excluded some details of my Gradle build here. There are more details on my StackOverflow question.

Flutter doctor output

flutter doctor --verbose
[✓] Flutter (Channel stable, 1.22.5, on Linux, locale en_AU.UTF-8)
    • Flutter version 1.22.5 at /opt/flutter
    • Framework revision 7891006299 (4 weeks ago), 2020-12-10 11:54:40 -0800
    • Engine revision ae90085a84
    • Dart version 2.10.4

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /home/jaween/Android/Sdk
    • Platform android-30, build-tools 29.0.3
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[!] Android Studio
    • Android Studio at /opt/android-studio
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)
    • SM A520F (mobile) • 5200676a4a815579 • android-arm64 • Android 8.0.0 (API 26)
@dcharkes
Copy link
Contributor

dcharkes commented Jan 5, 2021

We also have other users reporting issues with Android <= 6. Though, we've not been able to pinpoint a solution.

Possible workarounds:

/**
 * The loader for the X native library.
 *
 * This plugin is purely a workaround. On API versions <=23, it is possible to
 * call an Android SDK method that loads a native library that changes the paths searched when
 * `DynamicLibrary.open` is called in Dart.
 *
 * As such, this plugin is configured to load the shared library when it is registered, so that
 * when `DynamicLibrary.open('libX.so')` is called subsequently through Dart, it will be a
 * no-op.
 */
public final class XLoader implements FlutterPlugin {
  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
    loadLibrary();
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {}

  private static boolean isLibraryLoaded = false;

  private static void loadLibrary() {
    if (isLibraryLoaded) {
      return;
    }

    // Loads `libX.so`.
    System.loadLibrary("X");
    isLibraryLoaded = true;
  }
}

@dcharkes dcharkes added dependency: dart Dart team may need to help us platform-android Android applications specifically labels Jan 5, 2021
@jaween
Copy link
Author

jaween commented Jan 10, 2021

@dcharkes Thank you for your helpful answer.

I have now implemented the second workaround that you suggested (calling dlopen early outside Dart). It does indeed seem to have fixed the issue for the affected devices.

My specific problem is now solved, so I will close this issue. From what I understand Dart SDK issue #37771 and Flutter issue #59834 address the cause of the problem, so when those issues are solved, this workaround can be removed from the application.

@github-actions
Copy link

github-actions bot commented Aug 7, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
dependency: dart Dart team may need to help us platform-android Android applications specifically
Projects
None yet
Development

No branches or pull requests

3 participants