MCPcopy Create free account
hub / github.com/astonbitecode/j4rs / with_classloader_of_activity

Method with_classloader_of_activity

rust/src/api/mod.rs:1840–1852  ·  view source on GitHub ↗

Instructs j4rs to use the classloader associated to the specified `jobject` when searching for classes. Useful for pure native Android apps. As described in Android documentation, the native libraries that spawn new threads cannot locate and use Java classes correctly. From the [docs]((https://developer.android.com/training/articles/perf-jni#faq:-why-didnt-findclass-find-my-class)): > You can

(
        &'a mut self,
        jobject_within_valid_classloader: jobject,
    )

Source from the content-addressed store, hash-verified

1838 /// proper classloader and use it when needed.
1839 /// #[cfg(target_os = "android")]
1840 pub fn with_classloader_of_activity(
1841 &'a mut self,
1842 jobject_within_valid_classloader: jobject,
1843 ) -> &'a mut JvmBuilder<'a> {
1844 self.jobject_within_valid_classloader_opt = Some(jobject_within_valid_classloader);
1845 // If the `jobject_within_valid_classloader_opt` is provided, it means that the object's classloader
1846 // should be used to load classes in case the traditional `FindClass` invocations fail.
1847 // Apply here the needed configuration for it to happen.
1848 // Do not detach the thread on drop. This would make the Activity to fail.
1849 let tmp = self.detach_thread_on_drop(false);
1850 let tmp = tmp.with_no_implicit_classpath();
1851 tmp
1852 }
1853
1854 /// `j4rs` uses a custom ClassLoader (namely the `J4rsClassLoader`),
1855 /// that allows adding jars to the classpath during runtime, after the underlying `JVM` is initialized.

Callers

nothing calls this directly

Calls 2

detach_thread_on_dropMethod · 0.80

Tested by

no test coverage detected