Skip to content

Android failed to load dynamic library: dlopen failed: library "libc++shared.so" not found #900

@zhpixel517

Description

@zhpixel517

Hi all,
I'm trying to run cpal on an Android device inside my Flutter application. When I run the app, I am greeted with this error:

Failed to load dynamic library 'librust_lib_flutter_rust_test.so': 
dlopen failed: library "libc++_shared.so" not found: 
needed by /data/app/~~zyZDDuG_hSM2uJQAOeqDBQ==/com.example.flutter_rust_test-sBfBRTor8WZZGQJgZMG0PA==/lib/arm64/librust_lib_flutter_rust_test.so in namespace clns-4

The project builds fine, but I run this code and then the error is thrown:

let host = cpal::default_host();

  let input_device = host.default_input_device().unwrap();
  let output_device = host.default_output_device().unwrap();

  let input_config = cpal::StreamConfig {
      channels: 1,
      sample_rate: cpal::SampleRate(48000),
      buffer_size: cpal::BufferSize::Default,
  };
  // let output_config: cpal::StreamConfig = input_config.clone();
  let output_config = cpal::StreamConfig {
      channels: 1,
      sample_rate: cpal::SampleRate(48000),
      buffer_size: cpal::BufferSize::Default,
  };

  let input_stream = input_device
      .build_input_stream(
          &input_config,
          move |data: &[f32], _: &_| {
              // do something
          },
          move |err| {
              eprintln!("There was an input error: {:?}", err);
          },
          None,
      )
      .unwrap();

I know that this is the same problem as #720 - and I tried this solution from that issue:

I'm using this code to initialize the context. Just make it part of your fancy lib.

use jni::{JNIEnv, JavaVM};
use std::ffi::c_void;

#[no_mangle]
pub extern "C" fn JNI_OnLoad(vm: jni::JavaVM, res: *mut std::os::raw::c_void) -> jni::sys::jint {
    let env = vm.get_env().unwrap();
    let vm = vm.get_java_vm_pointer() as *mut c_void;
    unsafe {
        ndk_context::initialize_android_context(vm, res);
    }
    jni::JNIVersion::V6.into()
}

no success here. I'm also not entirely sure where exactly to put this code. I tried putting it inside my build.rs and also with my main application code as well with no results.

I also tried taking issue #563 's advice here:

This can be solved by enabling the shared-stdcxx feature for oboe

oboe = { version = "0.4", features = [ "java-interface", "shared-stdcxx" ] }

Then I also had to copy libc++_shared.so into my jniLibs directory (there's likely a way to automate this...)

Originally posted by @trobanga in #563 (comment)

Doing this created some strange errors. I see that it has usage of IPHONEOS_DEPLOYMENT_TARGET which wouldn't make sense for compiling to an android application:

  cargo:warning=oboe/src/common/OboeDebug.h:21:10: fatal error: 'android/log.h' file not found
  cargo:warning=#include <android/log.h>
  cargo:warning=         ^~~~~~~~~~~~~~~
  cargo:warning=1 error generated.
  cargo:warning=In file included from oboe/src/common/StabilizedCallback.cpp:18:
  cargo:warning=oboe/src/common/AudioClock.h:50:45: error: use of undeclared identifier 'TIMER_ABSTIME'
  cargo:warning=        return 0 - clock_nanosleep(clockId, TIMER_ABSTIME, &time, NULL);
  cargo:warning=                                            ^
  cargo:warning=oboe/src/common/AudioClock.h:67:24: error: use of undeclared identifier 'clock_nanosleep'
  cargo:warning=            return 0 - clock_nanosleep(clockId, 0, &time, NULL);
  cargo:warning=                       ^
  cargo:warning=2 errors generated.
  exit status: 1
  cargo:warning=ToolExecError: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-static" "-I" "oboe/include" "-I" "oboe/src" "-I" "oboe-ext/include" "-I" "oboe-ext/src" "-Wall" "-Wextra" "-std=c++17" "-Wall" "-Wextra-semi" "-Wshadow" "-Wshadow-field" "-fno-rtti" "-fno-exceptions" "-DOBOE_ENABLE_LOGGING=1" "-o" "/Users/zacharyhaslam/FlutterApplications/flutter_rust_test/rust/target/debug/build/oboe-sys-54157a8a77fee849/out/library/oboe/src/common/StabilizedCallback.o" "-c" "oboe/src/common/StabilizedCallback.cpp" with args "c++" did not execute successfully (status code exit status: 1).
  exit status: 1
  cargo:warning=ToolExecError: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-static" "-I" "oboe/include" "-I" "oboe/src" "-I" "oboe-ext/include" "-I" "oboe-ext/src" "-Wall" "-Wextra" "-std=c++17" "-Wall" "-Wextra-semi" "-Wshadow" "-Wshadow-field" "-fno-rtti" "-fno-exceptions" "-DOBOE_ENABLE_LOGGING=1" "-o" "/Users/zacharyhaslam/FlutterApplications/flutter_rust_test/rust/target/debug/build/oboe-sys-54157a8a77fee849/out/library/oboe/src/common/Trace.o" "-c" "oboe/src/common/Trace.cpp" with args "c++" did not execute successfully (status code exit status: 1).cargo:warning=oboe-ext/src/AudioStreamCallbackWrapper.cpp:3:10: fatal error: 'android/log.h' file not found
  cargo:warning=#include <android/log.h>
  cargo:warning=         ^~~~~~~~~~~~~~~
  cargo:warning=1 error generated.
  exit status: 0
  exit status: 0
  exit status: 0
  exit status: 1
  cargo:warning=ToolExecError: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-static" "-I" "oboe/include" "-I" "oboe/src" "-I" "oboe-ext/include" "-I" "oboe-ext/src" "-Wall" "-Wextra" "-std=c++17" "-Wall" "-Wextra-semi" "-Wshadow" "-Wshadow-field" "-fno-rtti" "-fno-exceptions" "-DOBOE_ENABLE_LOGGING=1" "-o" "/Users/zacharyhaslam/FlutterApplications/flutter_rust_test/rust/target/debug/build/oboe-sys-54157a8a77fee849/out/library/oboe-ext/src/AudioStreamCallbackWrapper.o" "-c" "oboe-ext/src/AudioStreamCallbackWrapper.cpp" with args "c++" did not execute successfully (status code exit status: 1).
  --- stderr
  error occurred: Command env -u IPHONEOS_DEPLOYMENT_TARGET "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-arch" "arm64" "-static" "-I" "oboe/include" "-I" "oboe/src" "-I" "oboe-ext/include" "-I" "oboe-ext/src" "-Wall" "-Wextra" "-std=c++17" "-Wall" "-Wextra-semi" "-Wshadow" "-Wshadow-field" "-fno-rtti" "-fno-exceptions" "-DOBOE_ENABLE_LOGGING=1" "-o" "/Users/zacharyhaslam/FlutterApplications/flutter_rust_test/rust/target/debug/build/oboe-sys-54157a8a77fee849/out/library/oboe-ext/src/AudioStreamCallbackWrapper.o" "-c" "oboe-ext/src/AudioStreamCallbackWrapper.cpp" with args "c++" did not execute successfully (status code exit status: 1).

However, I'm using flutter_rust_bridge which uses a code gen tool that handles most of the compilation for me. I think it automatically compiles for iOS and Android at the same time. I'm not super good at diagnosing configuration errors on Android, so I'm not entirely sure where I could be going wrong here. I also posted this issue on the flutter_rust_bridge github which might provide some more clarity.

I've seen people mention dragging a libc++_shared.so file into a jniLibs directory, but neither are present in my project files. I also tried making my own empty jniLibs folder to no success either.

Long story short, no matter what I seem to try, I can't seem to get rid of this error. Any ideas here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions