Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): use absolute path for the tauri Android library #6774

Merged
merged 1 commit into from Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/refactor-tauri-android-dependency.md
@@ -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
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
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
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
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
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'