@@ -62,6 +62,7 @@ use std::sync::Arc;
62
62
use std:: sync:: RwLock ;
63
63
use std:: time:: Duration ;
64
64
65
+ use libc:: c_void;
65
66
use ndk:: asset:: AssetManager ;
66
67
use ndk:: native_window:: NativeWindow ;
67
68
@@ -472,6 +473,49 @@ impl AndroidApp {
472
473
self . inner . read ( ) . unwrap ( ) . native_window ( )
473
474
}
474
475
476
+ /// Returns a pointer to the Java Virtual Machine, for making JNI calls
477
+ ///
478
+ /// This returns a pointer to the Java Virtual Machine which can be used
479
+ /// with the [`jni`] crate (or similar crates) to make JNI calls that bridge
480
+ /// between native Rust code and Java/Kotlin code running within the JVM.
481
+ ///
482
+ /// If you use the [`jni`] crate you can wrap this as a [`JavaVM`] via:
483
+ /// ```ignore
484
+ /// # use jni::JavaVM;
485
+ /// # let app: AndroidApp = todo!();
486
+ /// let vm = unsafe { JavaVM::from_raw(app.vm_as_ptr()) };
487
+ /// ```
488
+ ///
489
+ /// [`jni`]: https://crates.io/crates/jni
490
+ /// [`JavaVM`]: https://docs.rs/jni/latest/jni/struct.JavaVM.html
491
+ pub fn vm_as_ptr ( & self ) -> * mut c_void {
492
+ self . inner . read ( ) . unwrap ( ) . vm_as_ptr ( )
493
+ }
494
+
495
+ /// Returns a JNI object reference for this application's JVM `Activity` as a pointer
496
+ ///
497
+ /// If you use the [`jni`] crate you can wrap this as an object reference via:
498
+ /// ```ignore
499
+ /// # use jni::objects::JObject;
500
+ /// # let app: AndroidApp = todo!();
501
+ /// let activity = unsafe { JObject::from_raw(app.activity_as_ptr()) };
502
+ /// ```
503
+ ///
504
+ /// # JNI Safety
505
+ ///
506
+ /// Note that the object reference will be a JNI global reference, not a
507
+ /// local reference and it should not be deleted. Don't wrap the reference
508
+ /// in an [`AutoLocal`] which would try to explicitly delete the reference
509
+ /// when dropped. Similarly, don't wrap the reference as a [`GlobalRef`]
510
+ /// which would also try to explicitly delete the reference when dropped.
511
+ ///
512
+ /// [`jni`]: https://crates.io/crates/jni
513
+ /// [`AutoLocal`]: https://docs.rs/jni/latest/jni/objects/struct.AutoLocal.html
514
+ /// [`GlobalRef`]: https://docs.rs/jni/latest/jni/objects/struct.GlobalRef.html
515
+ pub fn activity_as_ptr ( & self ) -> * mut c_void {
516
+ self . inner . read ( ) . unwrap ( ) . activity_as_ptr ( )
517
+ }
518
+
475
519
/// Polls for any events associated with this [AndroidApp] and processes those events
476
520
/// (such as lifecycle events) via the given `callback`.
477
521
///
0 commit comments