Skip to content

Commit

Permalink
ndk-glue: Initialize ndk-context on 0.4 stable releases (#226)
Browse files Browse the repository at this point in the history
In order to show that [#223] solves our problem ([#211]) with multiple
ndk-glue versions in tree, all having their own `static` globals,
backport the `ndk-context` initialization to `ndk-glue 0.4` so that
everyone on this older crate can be compatible with other crates using
the Java VM and/or Android Context.  These crates will need to migrate
to `ndk-context` first though.

[#211]: #211
[#223]: #223
  • Loading branch information
MarijnS95 committed Feb 15, 2022
1 parent 074eb06 commit 4cb7b51
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Publish

on:
push:
branches: [master]
branches: [master, '*-stable']
paths: "**/Cargo.toml"

jobs:
Expand Down
1 change: 1 addition & 0 deletions ndk-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false
jni = "0.18.0"
log = "0.4.14"
ndk = { path = "../ndk", features = ["trace"] }
ndk-context = "0.1.0"
ndk-glue = { path = "../ndk-glue", features = ["logger"] }

[[example]]
Expand Down
7 changes: 3 additions & 4 deletions ndk-examples/examples/jni_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const GET_DEVICES_OUTPUTS: jni::sys::jint = 2;

fn enumerate_audio_devices() -> Result<(), Box<dyn std::error::Error>> {
// 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) }?;
let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
let env = vm.attach_current_thread()?;

// Query the global Audio Service
Expand All @@ -18,7 +17,7 @@ fn enumerate_audio_devices() -> Result<(), Box<dyn std::error::Error>> {

let audio_manager = env
.call_method(
native_activity.activity(),
ctx.context().cast(),
"getSystemService",
// JNI type signature needs to be derived from the Java API
// (ArgTys)ResultTy
Expand Down
4 changes: 4 additions & 0 deletions ndk-glue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 0.4.1 (2022-02-15)

- Initialize `ndk-context` for cross-version access to the Java `VM` and Android `Context`.

# 0.4.0 (2021-08-02)

- Looper is now created before returning from `ANativeActivity_onCreate`, solving
Expand Down
5 changes: 3 additions & 2 deletions ndk-glue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ndk-glue"
version = "0.4.0"
version = "0.4.1"
authors = ["The Rust Windowing contributors"]
edition = "2018"
description = "Startup code for android binaries"
Expand All @@ -13,8 +13,9 @@ repository = "https://github.com/rust-windowing/android-ndk-rs"

[dependencies]
ndk = { path = "../ndk", version = "0.4.0" }
ndk-sys = { path = "../ndk-sys", version = "0.2.1" }
ndk-context = "0.1.0"
ndk-macro = { path = "../ndk-macro", version = "0.2.0" }
ndk-sys = { path = "../ndk-sys", version = "0.2.1" }
lazy_static = "1.4.0"
libc = "0.2.84"
log = "0.4.14"
Expand Down
5 changes: 5 additions & 0 deletions ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ lazy_static! {

static mut NATIVE_ACTIVITY: Option<NativeActivity> = None;

// Intentionally omitting this deprecation warning to not suddenly break peoples' CI builds
// with a deprecation warning: especially since we don't have the full solution yet for applications
// like winit that do need access to these globals.
// #[deprecated = "Use `ndk_context::android_context().vm()` instead."]
pub fn native_activity() -> &'static NativeActivity {
unsafe { NATIVE_ACTIVITY.as_ref().unwrap() }
}
Expand Down Expand Up @@ -154,6 +158,7 @@ pub unsafe fn init(
callbacks.onLowMemory = Some(on_low_memory);

let activity = NativeActivity::from_ptr(activity);
ndk_context::initialize_android_context(activity.vm().cast(), activity.activity().cast());
NATIVE_ACTIVITY = Some(activity);

let mut logpipe: [RawFd; 2] = Default::default();
Expand Down

0 comments on commit 4cb7b51

Please sign in to comment.