-
Notifications
You must be signed in to change notification settings - Fork 307
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
Empty Android context #2466
Comments
Hi! Thanks for opening your first issue here! 😄 |
This part seems to be contributed by @powpingdone. Thus cc @powpingdone - do you have any suggestions? :) |
Thanks fzyz. @Sitin Check in the |
It seems that logger is not configured at this stage and I only can fail by returning |
The I forgot to mention that I've already used #[cfg(target_os = "android")]
#[no_mangle]
pub extern "C" fn JNI_OnLoad(vm: JavaVM, res: *mut std::os::raw::c_void) -> jni::sys::jint {
use std::ffi::c_void;
log::warn!("JNI_OnLoad called");
if res.is_null() {
return jni::sys::JNI_ERR;
}
let vm = vm.get_java_vm_pointer() as *mut c_void;
unsafe {
ndk_context::initialize_android_context(vm, res);
}
jni::JNIVersion::V6.into()
} |
I can reproduce this and think that Method 1 from the docs should be removed; the only way I've managed to correctly initialize the context is with method 2; from reading elsewhere, it seems that |
Feel free to update doc! |
@fzyzcjy, yes, the docs definitely need updating, in Method 2, the most important part (initializing the NDK context on the Rust side is not shown). Having just debugged this there are a couple of important points to get this to work.
Here is how I ended up implementing #[cfg(target_os = "android")]
mod init_android_context {
use jni::{objects::JClass, objects::JObject, objects::GlobalRef, JNIEnv};
use std::sync::OnceLock;
use std::ffi::c_void;
static CTX: OnceLock<GlobalRef> = OnceLock::new();
#[no_mangle]
pub extern "system" fn Java_com_example_local_1auth_MyPlugin_init_1android(
env: JNIEnv,
_class: JClass,
ctx: JObject,
) {
let global_ref = env.new_global_ref(&ctx).expect("to make global reference");
let vm = env.get_java_vm().unwrap();
let vm = vm.get_java_vm_pointer() as *mut c_void;
unsafe {
ndk_context::initialize_android_context(vm, global_ref.as_obj().as_raw() as _);
}
CTX.get_or_init(|| global_ref);
}
} Also I think on the Kotlin side it makes sense to add the plugin before calling class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(
@NonNull flutterEngine: FlutterEngine,
) {
flutterEngine.plugins.add(MyPlugin())
super.configureFlutterEngine(flutterEngine)
}
} Perhaps other people could verify this fixes android context initialization issues before we update the docs to be certain. |
Looks great! Feel free to PR for this. |
Describe the bug
I've configured Android context as it was described in the docs (Method 1). Still, the context returned by
ndk_context::android_context()
isnull
.Steps to reproduce
Create a project and set up Android context as descibed in the docs (Method 1).
Create a simple function that discovers the current android context:
Call it from Flutter.
You will see that context is null (
0x0
):Logs
The text was updated successfully, but these errors were encountered: