Skip to content

Commit

Permalink
refactor(core): use absolute path for the tauri Android library (#6774)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Apr 23, 2023
1 parent 2969d1c commit cdad6e0
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .changes/refactor-tauri-android-dependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-build": patch
---

Changed how the `tauri-android` dependency is injected. This requires the `gen/android` project to be recreated.
10 changes: 9 additions & 1 deletion core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,20 @@ dependencies {"
.to_string();

let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
let plugins: HashMap<String, mobile::PluginMetadata> = if plugins_json_path.exists() {
let mut plugins: HashMap<String, mobile::PluginMetadata> = if plugins_json_path.exists() {
let s = read_to_string(&plugins_json_path)?;
serde_json::from_str(&s)?
} else {
Default::default()
};

plugins.insert(
"tauri-android".into(),
mobile::PluginMetadata {
path: var_os("DEP_TAURI_ANDROID_LIBRARY_PATH").map(PathBuf::from).expect("missing `DEP_TAURI_ANDROID_LIBRARY_PATH` environment variable; did you add `tauri` as a dependency to this crate?"),
},
);

for (plugin_name, plugin) in plugins {
gradle_settings.push_str(&format!("include ':{plugin_name}'"));
gradle_settings.push('\n');
Expand Down
41 changes: 1 addition & 40 deletions core/tauri-build/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::{
collections::HashMap,
env::{var, var_os},
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, rename, write},
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, write},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -127,45 +127,6 @@ pub fn link_swift_library(name: &str, source: impl AsRef<Path>) {
}
}

#[doc(hidden)]
pub fn inject_android_project(
source: impl AsRef<Path>,
target: impl AsRef<Path>,
ignore_paths: &[&str],
) -> Result<()> {
let source = source.as_ref();
let target = target.as_ref();

// keep build folder if it exists
let build_path = target.join("build");
let out_dir = if build_path.exists() {
let out_dir = target.parent().unwrap().join(".tauri-tmp-build");
let _ = remove_dir_all(&out_dir);
rename(&build_path, &out_dir).context("failed to rename build directory")?;
Some(out_dir)
} else {
None
};

copy_folder(source, target, ignore_paths).context("failed to copy Android project")?;

if let Some(out_dir) = out_dir {
rename(out_dir, &build_path).context("failed to restore build directory")?;
}

let rerun_path = target.join("build.gradle.kts");
let metadata = source.join("build.gradle.kts").metadata()?;
filetime::set_file_mtime(
&rerun_path,
filetime::FileTime::from_last_modification_time(&metadata),
)
.context("failed to update build.gradle.kts mtime")?;

println!("cargo:rerun-if-changed={}", rerun_path.display());

Ok(())
}

fn copy_folder(source: &Path, target: &Path, ignore_paths: &[&str]) -> Result<()> {
let _ = remove_dir_all(target);

Expand Down
6 changes: 0 additions & 6 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ fn main() {

if target_os == "android" {
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
tauri_build::mobile::inject_android_project(
"./mobile/android",
project_dir.join(".tauri").join("tauri-api"),
&[],
)
.expect("failed to copy tauri-api Android project");
let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
"$PACKAGE",
&var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
Expand Down
1 change: 0 additions & 1 deletion tooling/cli/templates/mobile/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.4")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
implementation(project(":tauri-android"))
}

apply(from = "tauri.build.gradle.kts")
Expand Down
3 changes: 0 additions & 3 deletions tooling/cli/templates/mobile/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ include ':app'
{{~#each asset-packs}}
include ':{{this}}'{{/each}}

include ':tauri-android'
project(':tauri-android').projectDir = new File('./.tauri/tauri-api')

apply from: 'tauri.settings.gradle'

0 comments on commit cdad6e0

Please sign in to comment.