fix: pass off env vars to android apps (#3621)

* fix: pass off env vars to android apps

* move to jni_onload

* fix clippy
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-01-21 18:52:56 -08:00
1 parent ce2b223af9
commit 03c2d6711d
7 files changed
+65 -10

No files matched your search

+22
View File
@@ -128,9 +128,31 @@ pub extern "C" fn JNI_OnLoad(
panic!("Failed to find main symbol");
}
// Set the env vars that rust code might expect, passed off to us by the android app
// Doing this before main emulates the behavior of a regular executable
if cfg!(target_os = "android") && cfg!(debug_assertions) {
load_env_file_from_session_cache();
}
let main_fn: extern "C" fn() = std::mem::transmute(main_fn_ptr);
main_fn();
};
jni::sys::JNI_VERSION_1_6
}
/// Load the env file from the session cache if we're in debug mode and on android
///
/// This is a slightly hacky way of being able to use std::env::var code in android apps without
/// going through their custom java-based system.
#[cfg(target_os = "android")]
fn load_env_file_from_session_cache() {
let env_file = dioxus_cli_config::android_session_cache_dir().join(".env");
if let Some(env_file) = std::fs::read_to_string(&env_file).ok() {
for line in env_file.lines() {
if let Some((key, value)) = line.trim().split_once('=') {
std::env::set_var(key, value);
}
}
}
}