diff --git a/tooling/cli/src/mobile/init.rs b/tooling/cli/src/mobile/init.rs index 34a0d1847..eeecf08c1 100644 --- a/tooling/cli/src/mobile/init.rs +++ b/tooling/cli/src/mobile/init.rs @@ -20,7 +20,7 @@ use cargo_mobile::{ use clap::Parser; use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError}; -use std::{fs, io, path::PathBuf}; +use std::{env::current_dir, fs, io, path::PathBuf}; #[derive(Debug, Parser)] #[clap(about = "Initializes a Tauri Android project")] @@ -125,14 +125,31 @@ pub fn exec( } let (handlebars, mut map) = handlebars(&config); + + let mut args = std::env::args_os(); // TODO: make this a relative path - map.insert( - "tauri-binary", - std::env::args_os() - .next() - .unwrap_or_else(|| std::ffi::OsString::from("cargo")) - .to_string_lossy(), - ); + let tauri_binary = args + .next() + .unwrap_or_else(|| std::ffi::OsString::from("cargo")); + map.insert("tauri-binary", tauri_binary.to_string_lossy()); + let mut build_args = Vec::new(); + for arg in args { + if arg == "android" { + break; + } + let path = PathBuf::from(&arg); + if path.exists() { + if let Ok(dir) = current_dir() { + let absolute_path = util::prefix_path(dir, path); + build_args.push(absolute_path.to_string_lossy().into_owned()); + continue; + } + } + build_args.push(arg.to_string_lossy().into_owned()); + } + build_args.push("android".into()); + build_args.push("build".into()); + map.insert("tauri-binary-args", build_args); // Generate Android Studio project let android_env = if target == Target::Android { diff --git a/tooling/cli/templates/mobile/android/buildSrc/src/main/kotlin/BuildTask.kt b/tooling/cli/templates/mobile/android/buildSrc/src/main/kotlin/BuildTask.kt index 381080bfe..ea09602f4 100644 --- a/tooling/cli/templates/mobile/android/buildSrc/src/main/kotlin/BuildTask.kt +++ b/tooling/cli/templates/mobile/android/buildSrc/src/main/kotlin/BuildTask.kt @@ -27,7 +27,7 @@ open class BuildTask : DefaultTask() { project.exec { workingDir(File(project.projectDir, rootDirRel.path)) executable("{{ tauri-binary }}") - args(listOf("tauri", "android", "build")) + args(listOf({{quote-and-join tauri-binary-args}})) if (project.logger.isEnabled(LogLevel.DEBUG)) { args("-vv") } else if (project.logger.isEnabled(LogLevel.INFO)) {