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

fix(android): allow and escape Kotlin keyword as package identifier #328

Merged
merged 23 commits into from
May 27, 2024

Conversation

pewsheen
Copy link
Contributor

@pewsheen pewsheen commented May 16, 2024

Issue: tauri-apps/tauri#9743
Depend by: tauri-apps/tauri#9799

We can't escape Java's keywords in .java but we can escape Kotlin's in .kt

src/config/app/domain.rs Outdated Show resolved Hide resolved
@lucasfernog-crabnebula
Copy link

Needed changes to test this on tauri:

diff --git a/tooling/cli/Cargo.lock b/tooling/cli/Cargo.lock
index 23e589bf6..4ad31ab74 100644
--- a/tooling/cli/Cargo.lock
+++ b/tooling/cli/Cargo.lock
@@ -483,9 +483,7 @@ dependencies = [
 
 [[package]]
 name = "cargo-mobile2"
-version = "0.11.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e6eb3459f8fb1fd0e12ca229db09cd763befe00827061cc5aaf6bdf173dad37c"
+version = "0.11.1"
 dependencies = [
  "colored",
  "core-foundation",
@@ -495,7 +493,7 @@ dependencies = [
  "embed-resource",
  "english-numbers",
  "freedesktop_entry_parser",
- "handlebars 4.5.0",
+ "handlebars",
  "heck 0.5.0",
  "home",
  "ignore",
@@ -1795,20 +1793,6 @@ dependencies = [
  "crunchy",
 ]
 
-[[package]]
-name = "handlebars"
-version = "4.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "faa67bab9ff362228eb3d00bd024a4965d8231bbb7921167f0cfa66c6626b225"
-dependencies = [
- "log",
- "pest",
- "pest_derive",
- "serde",
- "serde_json",
- "thiserror",
-]
-
 [[package]]
 name = "handlebars"
 version = "5.1.0"
@@ -4862,7 +4846,7 @@ dependencies = [
  "dunce",
  "flate2",
  "glob",
- "handlebars 5.1.0",
+ "handlebars",
  "heck 0.5.0",
  "hex",
  "image",
@@ -4912,7 +4896,7 @@ dependencies = [
  "dunce",
  "env_logger",
  "glob",
- "handlebars 5.1.0",
+ "handlebars",
  "heck 0.5.0",
  "html5ever",
  "ignore",
@@ -5872,15 +5856,14 @@ checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
 
 [[package]]
 name = "which"
-version = "5.0.0"
+version = "6.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9bf3ea8596f3a0dd5980b46430f2058dfe2c36a27ccfbb1845d6fbfcd9ba6e14"
+checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7"
 dependencies = [
  "either",
  "home",
- "once_cell",
  "rustix 0.38.31",
- "windows-sys 0.48.0",
+ "winsafe",
 ]
 
 [[package]]
@@ -5916,11 +5899,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 
 [[package]]
 name = "windows"
-version = "0.54.0"
+version = "0.56.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49"
+checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132"
 dependencies = [
- "windows-core 0.54.0",
+ "windows-core 0.56.0",
  "windows-targets 0.52.5",
 ]
 
@@ -5935,14 +5918,38 @@ dependencies = [
 
 [[package]]
 name = "windows-core"
-version = "0.54.0"
+version = "0.56.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65"
+checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6"
 dependencies = [
+ "windows-implement",
+ "windows-interface",
  "windows-result",
  "windows-targets 0.52.5",
 ]
 
+[[package]]
+name = "windows-implement"
+version = "0.56.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.52",
+]
+
+[[package]]
+name = "windows-interface"
+version = "0.56.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.52",
+]
+
 [[package]]
 name = "windows-result"
 version = "0.1.1"
@@ -6148,6 +6155,12 @@ dependencies = [
  "windows-sys 0.48.0",
 ]
 
+[[package]]
+name = "winsafe"
+version = "0.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
+
 [[package]]
 name = "x25519-dalek"
 version = "2.0.1"
diff --git a/tooling/cli/Cargo.toml b/tooling/cli/Cargo.toml
index 715a804d8..dd5a3b9a1 100644
--- a/tooling/cli/Cargo.toml
+++ b/tooling/cli/Cargo.toml
@@ -39,7 +39,7 @@ name = "cargo-tauri"
 path = "src/main.rs"
 
 [dependencies]
-cargo-mobile2 = { version = "0.11", default-features = false }
+cargo-mobile2 = { path = "../../../cargo-mobile2/", default-features = false }
 jsonrpsee = { version = "0.22", features = [ "server" ] }
 jsonrpsee-core = "0.22"
 jsonrpsee-client-transport = { version = "0.22", features = [ "ws" ] }
diff --git a/tooling/cli/src/mobile/android/mod.rs b/tooling/cli/src/mobile/android/mod.rs
index 2764e83c4..2c445f5cf 100644
--- a/tooling/cli/src/mobile/android/mod.rs
+++ b/tooling/cli/src/mobile/android/mod.rs
@@ -135,7 +135,7 @@ pub fn get_config(
 
   set_var(
     "WRY_ANDROID_PACKAGE",
-    format!("{}.{}", app.reverse_domain(), app.name_snake()),
+    app.android_identifier_escape_kotlin_keyword(),
   );
   set_var("WRY_ANDROID_LIBRARY", app.lib_name());
   set_var("TAURI_ANDROID_PROJECT_PATH", config.project_dir());
diff --git a/tooling/cli/src/mobile/init.rs b/tooling/cli/src/mobile/init.rs
index c1db20e86..60ca9f834 100644
--- a/tooling/cli/src/mobile/init.rs
+++ b/tooling/cli/src/mobile/init.rs
@@ -14,6 +14,7 @@ use cargo_mobile2::{
   },
   config::app::App,
   dot_cargo,
+  reserved_names::KOTLIN_ONLY_KEYWORDS,
   target::TargetTrait as _,
   util::{
     self,
@@ -215,6 +216,7 @@ fn handlebars(app: &App) -> (Handlebars<'static>, JsonMap) {
     "reverse-domain-snake-case",
     Box::new(reverse_domain_snake_case),
   );
+  h.register_helper("escape-kotlin-keyword", Box::new(escape_kotlin_keyword));
   // don't mix these up or very bad things will happen to all of us
   h.register_helper("prefix-path", Box::new(prefix_path));
   h.register_helper("unprefix-path", Box::new(unprefix_path));
@@ -360,6 +362,28 @@ fn reverse_domain_snake_case(
     .map_err(Into::into)
 }
 
+fn escape_kotlin_keyword(
+  helper: &Helper,
+  _: &Handlebars,
+  _: &Context,
+  _: &mut RenderContext,
+  out: &mut dyn Output,
+) -> HelperResult {
+  let escaped_result = get_str(helper)
+    .split('.')
+    .map(|s| {
+      if KOTLIN_ONLY_KEYWORDS.contains(&s) {
+        format!("`{}`", s)
+      } else {
+        s.to_string()
+      }
+    })
+    .collect::<Vec<_>>()
+    .join(".");
+
+  out.write(&escaped_result).map_err(Into::into)
+}
+
 fn app_root(ctx: &Context) -> Result<&str, RenderError> {
   let app_root = ctx
     .data()
diff --git a/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt b/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt
index 8425f5e2e..d401498ab 100644
--- a/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt
+++ b/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt
@@ -1,3 +1,3 @@
-package {{reverse-domain app.domain}}.{{snake-case app.name}}
+package {{escape-kotlin-keyword (reverse-domain app.domain)}}.{{escape-kotlin-keyword (snake-case app.name)}}
 
 class MainActivity : TauriActivity()

hampuslidin and others added 16 commits May 27, 2024 11:10
…s#330)

* fix(android): use tauri.config.json > identifier as package name

* chore: add changelog

* rename `domain` to `identifier`

* rename domain mod

* lint

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: lucasfernog <lucasfernog@users.noreply.github.com>
@pewsheen
Copy link
Contributor Author

Needed changes to test this on tauri:

diff --git a/tooling/cli/Cargo.lock b/tooling/cli/Cargo.lock
...

I've created a PR for this: tauri-apps/tauri#9799

Just a noted that if you're creating a new app with CTA, remember to change the version to at least 0.0.1 in tarui.conf.json

@lucasfernog lucasfernog merged commit 7d260ba into tauri-apps:dev May 27, 2024
12 checks passed
@github-actions github-actions bot mentioned this pull request May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants