Skip to content

Commit 9422490

Browse files
authored
refactor(core): generate TauriActivity on build script (#6783)
1 parent ecc9ac9 commit 9422490

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Generate `TauriActivity` Kotlin class on the build script.

.github/workflows/test-android.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# SPDX-License-Identifier: MIT
44

5-
name: test mobile
5+
name: test android
66

77
on:
88
pull_request:
@@ -14,6 +14,7 @@ on:
1414
- '!tooling/cli/src/mobile/ios/**'
1515
- 'core/tauri-build/src/mobile.rs'
1616
- 'core/tauri/mobile/android/**'
17+
- 'core/tauri/mobile/android-codegen/**'
1718
workflow_dispatch:
1819

1920
concurrency:

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [ "CHANGELOG.md", "/target" ]
1313
readme = "README.md"
1414

1515
[dependencies]
16-
wry = { version = "0.28.1", default-features = false, features = [ "file-drop", "protocol" ] }
16+
wry = { version = "0.28.2", default-features = false, features = [ "file-drop", "protocol" ] }
1717
tauri-runtime = { version = "0.13.0-alpha.4", path = "../tauri-runtime" }
1818
tauri-utils = { version = "2.0.0-alpha.4", path = "../tauri-utils" }
1919
uuid = { version = "1", features = [ "v4" ] }

core/tauri/build.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use heck::ToSnakeCase;
99
use once_cell::sync::OnceCell;
1010

1111
use std::env::var_os;
12+
use std::fs::read_dir;
13+
use std::fs::read_to_string;
14+
use std::fs::write;
1215
use std::{
1316
env::var,
1417
path::{Path, PathBuf},
@@ -150,7 +153,46 @@ fn main() {
150153
}
151154

152155
if target_os == "android" {
153-
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
156+
if let Ok(kotlin_out_dir) = std::env::var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR") {
157+
fn env_var(var: &str) -> String {
158+
std::env::var(var).unwrap_or_else(|_| {
159+
panic!(
160+
"`{}` is not set, which is needed to generate the kotlin files for android.",
161+
var
162+
)
163+
})
164+
}
165+
166+
let package = env_var("WRY_ANDROID_PACKAGE");
167+
let library = env_var("WRY_ANDROID_LIBRARY");
168+
169+
let kotlin_out_dir = PathBuf::from(&kotlin_out_dir)
170+
.canonicalize()
171+
.unwrap_or_else(move |_| {
172+
panic!("Failed to canonicalize `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` path {kotlin_out_dir}")
173+
});
174+
175+
let kotlin_files_path =
176+
PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("mobile/android-codegen");
177+
println!("cargo:rerun-if-changed={}", kotlin_files_path.display());
178+
let kotlin_files =
179+
read_dir(kotlin_files_path).expect("failed to read Android codegen directory");
180+
181+
for file in kotlin_files {
182+
let file = file.unwrap();
183+
184+
let content = read_to_string(file.path())
185+
.expect("failed to read kotlin file as string")
186+
.replace("{{package}}", &package)
187+
.replace("{{library}}", &library);
188+
189+
let out_path = kotlin_out_dir.join(file.file_name());
190+
write(&out_path, content).expect("Failed to write kotlin file");
191+
println!("cargo:rerun-if-changed={}", out_path.display());
192+
}
193+
}
194+
195+
if let Some(project_dir) = var_os("WRY_ANDROID_PROJECT_PATH").map(PathBuf::from) {
154196
let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
155197
"$PACKAGE",
156198
&var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),

tooling/cli/templates/mobile/android/app/src/main/generated/TauriActivity.kt renamed to core/tauri/mobile/android-codegen/TauriActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
package {{reverse-domain app.domain}}.{{snake-case app.name}}
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
/* THIS FILE IS AUTO-GENERATED. DO NOT MODIFY!! */
6+
7+
package {{package}}
28

39
import android.os.Bundle
410
import app.tauri.plugin.PluginManager

examples/api/src-tauri/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)