diff --git a/.changes/generate-tauri-activity.md b/.changes/generate-tauri-activity.md new file mode 100644 index 000000000..000c44a2b --- /dev/null +++ b/.changes/generate-tauri-activity.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Generate `TauriActivity` Kotlin class on the build script. diff --git a/.github/workflows/test-android.yml b/.github/workflows/test-android.yml index f82e7d998..fb2dccc68 100644 --- a/.github/workflows/test-android.yml +++ b/.github/workflows/test-android.yml @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: MIT -name: test mobile +name: test android on: pull_request: @@ -14,6 +14,7 @@ on: - '!tooling/cli/src/mobile/ios/**' - 'core/tauri-build/src/mobile.rs' - 'core/tauri/mobile/android/**' + - 'core/tauri/mobile/android-codegen/**' workflow_dispatch: concurrency: diff --git a/core/tauri-runtime-wry/Cargo.toml b/core/tauri-runtime-wry/Cargo.toml index fe64ed0b3..8949b4b54 100644 --- a/core/tauri-runtime-wry/Cargo.toml +++ b/core/tauri-runtime-wry/Cargo.toml @@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ] readme = "README.md" [dependencies] -wry = { version = "0.28.1", default-features = false, features = [ "file-drop", "protocol" ] } +wry = { version = "0.28.2", default-features = false, features = [ "file-drop", "protocol" ] } tauri-runtime = { version = "0.13.0-alpha.4", path = "../tauri-runtime" } tauri-utils = { version = "2.0.0-alpha.4", path = "../tauri-utils" } uuid = { version = "1", features = [ "v4" ] } diff --git a/core/tauri/build.rs b/core/tauri/build.rs index e88935f42..0051d425b 100644 --- a/core/tauri/build.rs +++ b/core/tauri/build.rs @@ -9,6 +9,9 @@ use heck::ToSnakeCase; use once_cell::sync::OnceCell; use std::env::var_os; +use std::fs::read_dir; +use std::fs::read_to_string; +use std::fs::write; use std::{ env::var, path::{Path, PathBuf}, @@ -150,7 +153,46 @@ fn main() { } if target_os == "android" { - if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) { + if let Ok(kotlin_out_dir) = std::env::var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR") { + fn env_var(var: &str) -> String { + std::env::var(var).unwrap_or_else(|_| { + panic!( + "`{}` is not set, which is needed to generate the kotlin files for android.", + var + ) + }) + } + + let package = env_var("WRY_ANDROID_PACKAGE"); + let library = env_var("WRY_ANDROID_LIBRARY"); + + let kotlin_out_dir = PathBuf::from(&kotlin_out_dir) + .canonicalize() + .unwrap_or_else(move |_| { + panic!("Failed to canonicalize `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` path {kotlin_out_dir}") + }); + + let kotlin_files_path = + PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("mobile/android-codegen"); + println!("cargo:rerun-if-changed={}", kotlin_files_path.display()); + let kotlin_files = + read_dir(kotlin_files_path).expect("failed to read Android codegen directory"); + + for file in kotlin_files { + let file = file.unwrap(); + + let content = read_to_string(file.path()) + .expect("failed to read kotlin file as string") + .replace("{{package}}", &package) + .replace("{{library}}", &library); + + let out_path = kotlin_out_dir.join(file.file_name()); + write(&out_path, content).expect("Failed to write kotlin file"); + println!("cargo:rerun-if-changed={}", out_path.display()); + } + } + + if let Some(project_dir) = var_os("WRY_ANDROID_PROJECT_PATH").map(PathBuf::from) { let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace( "$PACKAGE", &var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"), diff --git a/tooling/cli/templates/mobile/android/app/src/main/generated/TauriActivity.kt b/core/tauri/mobile/android-codegen/TauriActivity.kt similarity index 61% rename from tooling/cli/templates/mobile/android/app/src/main/generated/TauriActivity.kt rename to core/tauri/mobile/android-codegen/TauriActivity.kt index 765817ba9..b0ca3bba8 100644 --- a/tooling/cli/templates/mobile/android/app/src/main/generated/TauriActivity.kt +++ b/core/tauri/mobile/android-codegen/TauriActivity.kt @@ -1,4 +1,10 @@ -package {{reverse-domain app.domain}}.{{snake-case app.name}} +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +/* THIS FILE IS AUTO-GENERATED. DO NOT MODIFY!! */ + +package {{package}} import android.os.Bundle import app.tauri.plugin.PluginManager diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 8e222ffce..15a670c5a 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -4001,9 +4001,9 @@ dependencies = [ [[package]] name = "wry" -version = "0.28.1" +version = "0.28.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bc4072414a89ef95559d499f3b1930c773fb4457759ec78fe3a9849883ee22d" +checksum = "77dbd1fe0556bf87517db301c9be2bdc18f6311c0826c73b2c80a7f79ac0e31f" dependencies = [ "base64 0.13.1", "block",