再使用Tauri Rust时, 使用了 Command::new("adb"),这样我再打包后,运行打包后的应用程序会自带一个终端
我尝试过在main.rs中添加代码
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
但是这样打包后的程序不会自带终端,但是我调用 Command::new("adb") 的时候会弹出一个终端并且执行完成后就会消失
我该怎么处理呢
根据AI的反馈,我是独立封装的一个adb方法 ,代码如下
#[tauri::command]
pub fn exec(mut command: String) -> Result<String, String> {
command = command.replace("\r\n", "");
let mut child = Command::new("adb")
.arg("shell")
.arg(&command)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| format!("Failed to execute command: {}", e))?;
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
let mut stdout_reader = io::BufReader::new(stdout);
let mut stderr_reader = io::BufReader::new(stderr);
let mut stdout_output = String::new();
stdout_reader
.read_to_string(&mut stdout_output)
.expect("error");
let mut stderr_output = String::new();
stderr_reader
.read_to_string(&mut stderr_output)
.expect("error");
let status = child.wait();
let result_str = stdout_output.clone();
println!("result_str:{}", result_str);
println!("stdout_output:{}", stdout_output);
println!("stderr_output:{}", stderr_output);
println!("{:?}", status);
if stderr_output.is_empty() {
Ok(stdout_output)
} else {
Err(stderr_output)
}
}
在其他地方我是直接调用的该方法
调用cmd时传入一个flag: