Skip to content

Commit 2969d1c

Browse files
authored
refactor(core): use absolute path to plugin Android project (#6773)
1 parent dfa407f commit 2969d1c

4 files changed

Lines changed: 39 additions & 19 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"tauri-build": patch
4+
---
5+
6+
Use absolute path to each Android plugin project instead of copying the files to enhance developer experience.

core/tauri-build/src/lib.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use tauri_utils::{
1515
};
1616

1717
use std::{
18+
collections::HashMap,
1819
env::var_os,
19-
fs::{read_dir, write},
20+
fs::{read_to_string, write},
2021
path::{Path, PathBuf},
2122
};
2223

@@ -298,22 +299,24 @@ val implementation by configurations
298299
dependencies {"
299300
.to_string();
300301

301-
for entry in read_dir(project_dir.join(".tauri").join("plugins"))? {
302-
let pkg_name = entry?
303-
.path()
304-
.file_name()
305-
.unwrap()
306-
.to_string_lossy()
307-
.into_owned();
308-
gradle_settings.push_str(&format!("include ':{pkg_name}'"));
302+
let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
303+
let plugins: HashMap<String, mobile::PluginMetadata> = if plugins_json_path.exists() {
304+
let s = read_to_string(&plugins_json_path)?;
305+
serde_json::from_str(&s)?
306+
} else {
307+
Default::default()
308+
};
309+
for (plugin_name, plugin) in plugins {
310+
gradle_settings.push_str(&format!("include ':{plugin_name}'"));
309311
gradle_settings.push('\n');
310312
gradle_settings.push_str(&format!(
311-
"project(':{pkg_name}').projectDir = new File('./.tauri/plugins/{pkg_name}')"
313+
"project(':{plugin_name}').projectDir = new File('{}')",
314+
plugin.path.display()
312315
));
313316
gradle_settings.push('\n');
314317

315318
app_build_gradle.push('\n');
316-
app_build_gradle.push_str(&format!(r#" implementation(project(":{pkg_name}"))"#));
319+
app_build_gradle.push_str(&format!(r#" implementation(project(":{plugin_name}"))"#));
317320
}
318321
app_build_gradle.push_str("\n}");
319322

core/tauri-build/src/mobile.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
// SPDX-License-Identifier: MIT
44

55
use std::{
6+
collections::HashMap,
67
env::{var, var_os},
7-
fs::{copy, create_dir, create_dir_all, remove_dir_all, rename},
8+
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, rename, write},
89
path::{Path, PathBuf},
910
};
1011

1112
use anyhow::{Context, Result};
13+
use serde::{Deserialize, Serialize};
14+
15+
#[derive(Deserialize, Serialize)]
16+
pub(crate) struct PluginMetadata {
17+
pub path: PathBuf,
18+
}
1219

1320
#[derive(Default)]
1421
pub struct PluginBuilder {
@@ -49,6 +56,7 @@ impl PluginBuilder {
4956

5057
let tauri_library_path = std::env::var("DEP_TAURI_ANDROID_LIBRARY_PATH")
5158
.expect("missing `DEP_TAURI_ANDROID_LIBRARY_PATH` environment variable. Make sure `tauri` is a dependency of the plugin.");
59+
println!("cargo:rerun-if-env-changed=DEP_TAURI_ANDROID_LIBRARY_PATH");
5260

5361
create_dir_all(source.join(".tauri")).context("failed to create .tauri directory")?;
5462
copy_folder(
@@ -62,12 +70,15 @@ impl PluginBuilder {
6270
let pkg_name = var("CARGO_PKG_NAME").unwrap();
6371
println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
6472

65-
inject_android_project(
66-
&source,
67-
project_dir.join(".tauri").join("plugins").join(pkg_name),
68-
&["tauri-api"],
69-
)
70-
.context("failed to inject plugin Android project")?;
73+
let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
74+
let mut plugins: HashMap<String, PluginMetadata> = if plugins_json_path.exists() {
75+
let s = read_to_string(&plugins_json_path)?;
76+
serde_json::from_str(&s)?
77+
} else {
78+
Default::default()
79+
};
80+
plugins.insert(pkg_name, PluginMetadata { path: source });
81+
write(&plugins_json_path, serde_json::to_string(&plugins)?)?;
7182
}
7283
}
7384
}

tooling/cli/src/mobile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
316316
)
317317
} else {
318318
if target == Target::Android {
319-
create_dir_all(project_dir.join(".tauri").join("plugins"))?;
319+
create_dir_all(project_dir.join(".tauri"))?;
320320
}
321321
Ok(())
322322
}

0 commit comments

Comments
 (0)