Rust panic - android context was not initialized: Flutter-Rinf-Rust [ hub -> rodio -> cpal -> oboe -> ndk-context ] #530
-
Good Day! I had been reading and rereading issues about this known difficulties, like:
But without success. On Linux everything is OK. The error with shared c++ lib was successfully resolved. Rust panic at ndk-context-0.1.1/src/lib.rs:72:30 all the time... The way seems to be: I found the explanation and it seems to be very near to true: "Typically, in a normal application, the Android JVM would System.loadLibrary() the library through the Activity inside the JVM. It looks for symbols related to the JNI and executes them in accordance with the JNI standard. This would initialize the ndk_context normally via JNI_OnLoad. However, using the DartVM this step is skipped while loading the library, as the DartVM is not the JVM. " There are also some suggestions but they are not applicable for my case. Also, I tried to insert this snippet: #[cfg(target_os = "android")]
#[no_mangle]
pub extern "C" fn JNI_OnLoad(vm: jni::JavaVM, res: *mut std::os::raw::c_void) -> jni::sys::jint {
use std::ffi::c_void;
let vm = vm.get_java_vm_pointer() as *mut c_void;
unsafe {
ndk_context::initialize_android_context(vm, res);
}
jni::JNIVersion::V6.into()
} Before and after "rinf::write_interface!();" Result of all my experiments is the same: flutter app works fine (both on Android device via USB and Emulator) And on Linux everything is perfect! :) P.S.: I cloned crates "cpal" and "ndk-context" and tried to debug place with Android context initialization. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @sergey-serov :) Thank you for the report. I'm aware that there are issues related to dynamic library symbols on Android. The cause is usually the different behaivor of In my opinion, the code snippet you provided #[cfg(target_os = "android")]
#[no_mangle]
pub extern "C" fn JNI_OnLoad(vm: jni::JavaVM, res: *mut std::os::raw::c_void) -> jni::sys::jint {
use std::ffi::c_void;
let vm = vm.get_java_vm_pointer() as *mut c_void;
unsafe {
ndk_context::initialize_android_context(vm, res);
}
jni::JNIVersion::V6.into()
} or similar might be able to help us, and I'm open to putting this code inside the
Does this mean the panic still occurs when trying to call |
Beta Was this translation helpful? Give feedback.
Greetings!
The answer was in front of me all this time.
On the link from my question: https://cjycode.com/flutter_rust_bridge/guides/how-to/ndk-init - I was just confused that there is code on Kotlin and thought that it's appropriate only for "flutter_rust_bridge".
Finally, it is enough just to add 3 lines:
in "MainActivity.kt".
Additionally to function "JNI_OnLoad" in "hub/src/lib.rs".