Skip to content

Commit 71a5e2b

Browse files
fix(android): allow and escape Kotlin keywords as package identifier, closes #9743 (#9799)
* fix(android): escape kotlin only keyword in template * fix: escape keywords in wry templates aswell * chore: add changelog * chore: remove unused code * fix(android): wry template, package name should reverse * update cargo-mobile2 --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 4754786 commit 71a5e2b

6 files changed

Lines changed: 37 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-cli": "patch:enhance"
3+
---
4+
5+
On Android, allow using Kotlin keywords as identifiers and escape them in templates.

tooling/cli/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.

tooling/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ name = "cargo-tauri"
3939
path = "src/main.rs"
4040

4141
[dependencies]
42-
cargo-mobile2 = { version = "0.12", default-features = false }
42+
cargo-mobile2 = { version = "0.12.1", default-features = false }
4343
jsonrpsee = { version = "0.22", features = [ "server" ] }
4444
jsonrpsee-core = "0.22"
4545
jsonrpsee-client-transport = { version = "0.22", features = [ "ws" ] }

tooling/cli/src/mobile/android/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ pub fn get_config(
133133
..Default::default()
134134
};
135135

136-
set_var("WRY_ANDROID_PACKAGE", app.reverse_identifier());
136+
set_var(
137+
"WRY_ANDROID_PACKAGE",
138+
app.android_identifier_escape_kotlin_keyword(),
139+
);
137140
set_var("WRY_ANDROID_LIBRARY", app.lib_name());
138141
set_var("TAURI_ANDROID_PROJECT_PATH", config.project_dir());
139142

tooling/cli/src/mobile/init.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cargo_mobile2::{
1414
},
1515
config::app::App,
1616
dot_cargo,
17+
reserved_names::KOTLIN_ONLY_KEYWORDS,
1718
target::TargetTrait as _,
1819
util::{
1920
self,
@@ -215,6 +216,7 @@ fn handlebars(app: &App) -> (Handlebars<'static>, JsonMap) {
215216
"reverse-domain-snake-case",
216217
Box::new(reverse_domain_snake_case),
217218
);
219+
h.register_helper("escape-kotlin-keyword", Box::new(escape_kotlin_keyword));
218220
// don't mix these up or very bad things will happen to all of us
219221
h.register_helper("prefix-path", Box::new(prefix_path));
220222
h.register_helper("unprefix-path", Box::new(unprefix_path));
@@ -360,6 +362,28 @@ fn reverse_domain_snake_case(
360362
.map_err(Into::into)
361363
}
362364

365+
fn escape_kotlin_keyword(
366+
helper: &Helper,
367+
_: &Handlebars,
368+
_: &Context,
369+
_: &mut RenderContext,
370+
out: &mut dyn Output,
371+
) -> HelperResult {
372+
let escaped_result = get_str(helper)
373+
.split('.')
374+
.map(|s| {
375+
if KOTLIN_ONLY_KEYWORDS.contains(&s) {
376+
format!("`{}`", s)
377+
} else {
378+
s.to_string()
379+
}
380+
})
381+
.collect::<Vec<_>>()
382+
.join(".");
383+
384+
out.write(&escaped_result).map_err(Into::into)
385+
}
386+
363387
fn app_root(ctx: &Context) -> Result<&str, RenderError> {
364388
let app_root = ctx
365389
.data()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package {{reverse-domain app.identifier}}
1+
package {{escape-kotlin-keyword (reverse-domain app.identifier)}}
22

33
class MainActivity : TauriActivity()

0 commit comments

Comments
 (0)