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

Using cpal in a library for android causes an error #720

Closed
Flocksserver opened this issue Nov 3, 2022 · 8 comments
Closed

Using cpal in a library for android causes an error #720

Flocksserver opened this issue Nov 3, 2022 · 8 comments

Comments

@Flocksserver
Copy link

Flocksserver commented Nov 3, 2022

Hi all,
I have a Rust library and use it on Android via jni. I load my library into the JVM context as usual.

companion object {
        init {
            System.loadLibrary("myfancylib")
        }
    }

So far so good.
But as soon as I use cpal (e.g. cpal::default_host();) and cross compile it to android, the app crashes when loading my library with the error:
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__gxx_personality_v0" referenced by
Is this an indication that the oboe library is missing? Should I add this library separately? How?


Edit:
Found issue #563 - look similar. After adding c++_shared I get the error when calling cpal:

panicked at 'android context was not initialized': /Users/XXX/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/ndk-context-0.1.1/src/lib.rs:72

I am confused - there is a local path during runtime? 😅

@Flocksserver Flocksserver changed the title Using cpal in a library for android causes a loading error Using cpal in a library for android causes an error Nov 3, 2022
@SuisChan
Copy link

SuisChan commented Nov 3, 2022

Can you provide the output of readelf -d yourfancylib.so or llvm-readelf -d *.so ?

@Flocksserver
Copy link
Author

Sure!

Dynamic section at offset 0x307aa8 contains 26 entries:
  Tag                Type           Name/Value
  0x0000000000000001 (NEEDED)       Shared library: [liblog.so]
  0x0000000000000001 (NEEDED)       Shared library: [libdl.so]
  0x0000000000000001 (NEEDED)       Shared library: [libc.so]
  0x000000000000001e (FLAGS)        BIND_NOW 
  0x000000006ffffffb (FLAGS_1)      NOW 
  0x0000000000000007 (RELA)         0x1310
  0x0000000000000008 (RELASZ)       223680 (bytes)
  0x0000000000000009 (RELAENT)      24 (bytes)
  0x000000006ffffff9 (RELACOUNT)    9318
  0x0000000000000017 (JMPREL)       0x37cd0
  0x0000000000000002 (PLTRELSZ)     1752 (bytes)
  0x0000000000000003 (PLTGOT)       0x30a638
  0x0000000000000014 (PLTREL)       RELA
  0x0000000000000006 (SYMTAB)       0x308
  0x000000000000000b (SYMENT)       24 (bytes)
  0x0000000000000005 (STRTAB)       0xcb8
  0x000000000000000a (STRSZ)        1624 (bytes)
  0x000000006ffffef5 (GNU_HASH)     0xc48
  0x0000000000000019 (INIT_ARRAY)   0x309aa0
  0x000000000000001b (INIT_ARRAYSZ) 8 (bytes)
  0x000000000000001a (FINI_ARRAY)   0x309a90
  0x000000000000001c (FINI_ARRAYSZ) 16 (bytes)
  0x000000006ffffff0 (VERSYM)       0xb48
  0x000000006ffffffe (VERNEED)      0xbf8
  0x000000006fffffff (VERNEEDNUM)   2
  0x0000000000000000 (NULL)         0x0

@Flocksserver
Copy link
Author

i still don't understand the bug but since it has something to do with the oboe "java-interface" feature it is not a direct cpal problem

@trobanga
Copy link

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()
}

@Flocksserver
Copy link
Author

Thanks @trobanga 🙏 that was the missing piece 🤩

@wujian752
Copy link

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()
}

It seems the argument res is always 0x0.

@MachineL102
Copy link

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()
}

It seems the argument res is always 0x0.

I have the same issue. Have you found the solution? very very thanks

@wujian752
Copy link

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()
}

It seems the argument res is always 0x0.

I have the same issue. Have you found the solution? very very thanks

Unfortunately, I haven't found a solution to the issue either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants