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

android: Use ndk-context instead of ndk-glue to access JVM #52

Merged
merged 1 commit into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Expand Up @@ -28,8 +28,8 @@ winapi = { version = "0.3", features = ["combaseapi", "objbase", "shellapi", "wi
widestring = { version = ">= 0.5, <=1.0" }

[target.'cfg(target_os = "android")'.dependencies]
jni = "0.19"
ndk-glue = { version = ">= 0.3, <= 0.7" }
jni = "0.20"
ndk-context = "0.1"

[target.'cfg(target_os = "ios")'.dependencies]
raw-window-handle = "0.5.0"
Expand All @@ -41,3 +41,6 @@ actix-files = "0.6"
crossbeam-channel = "0.5"
tokio = { version = "1", features = ["full"] }
urlencoding = "2.1"

[target.'cfg(target_os = "android")'.dev-dependencies]
ndk-glue = { version = ">= 0.3, <= 0.7" }
10 changes: 6 additions & 4 deletions src/android.rs
Expand Up @@ -24,9 +24,11 @@ pub fn open_browser_default(url: &str, options: &BrowserOptions) -> Result<()> {
}

// Create a VM for executing Java calls
let native_activity = ndk_glue::native_activity();
let vm_ptr = native_activity.vm();
let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }.unwrap();
let ctx = ndk_context::android_context();
let vm = unsafe {
jni::JavaVM::from_raw(ctx.vm() as _).expect("Expected to find JVM via ndk_context crate")
amodm marked this conversation as resolved.
Show resolved Hide resolved
};
let activity = unsafe { jni::objects::JObject::from_raw(ctx.context() as _) };
let env = vm.attach_current_thread().map_err(|_| -> Error {
Error::new(ErrorKind::Other, "Failed to attach current thread")
})?;
Expand Down Expand Up @@ -71,7 +73,7 @@ pub fn open_browser_default(url: &str, options: &BrowserOptions) -> Result<()> {

// Start the intent activity.
env.call_method(
native_activity.activity(),
activity,
"startActivity",
"(Landroid/content/Intent;)V",
&[JValue::Object(intent)],
Expand Down