Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@YASHRDX0001
Copy link

Bug fix
Fixes #14634

Description

When productName in tauri.conf.json contains special characters like single quotes (e.g., "Aircraft's Hub"), the generated Android strings.xml file causes build failures because these characters aren't properly escaped according to Android XML string resource rules.

Problem

Android XML string resources require certain characters to be escaped with a backslash:

  • Single quotes (') must be escaped as \'
  • Backslashes (\) must be escaped as \\
  • Double quotes (") must be escaped as \"
  • Special whitespace characters (newlines, tabs, etc.) must also be escaped

The Handlebars template was directly inserting {{app.stylized-name}} without any escaping, causing XML parsing errors during Android builds.

Solution
This PR adds proper escaping for Android XML strings by:

  1. Created android_escape helper function (crates/tauri-cli/src/mobile/init.rs):

    • Escapes single quotes: '\'
    • Escapes backslashes: \\\
    • Escapes double quotes: "\"
    • Escapes newlines: \n\\n
    • Escapes carriage returns: \r\\r
    • Escapes tabs: \t\\t
  2. Registered the helper in the Handlebars instance

  3. Updated the template (crates/tauri-cli/templates/mobile/android/app/src/main/res/values/strings.xml):

    • Changed {{app.stylized-name}} to {{android-escape app.stylized-name}}
    • Applied to both app_name and main_activity_title strings

Changes Made
Modified Files:

  • crates/tauri-cli/src/mobile/init.rs (+19 lines)
  • crates/tauri-cli/templates/mobile/android/app/src/main/res/values/strings.xml (2 lines modified)

Testing

Before Fix:

{
  "productName": "Aircraft's Hub"
}
<resources>
    <string name="app_name">Aircraft's Hub</string>
    <string name="main_activity_title">Aircraft's Hub</string>
</resources>
Result: Build fails with XML parsing error

After Fix:
<resources>
    <string name="app_name">Aircraft\'s Hub</string>
    <string name="main_activity_title">Aircraft\'s Hub</string>
    
</resources>
Result: Builds successfully

This fix follows Android's official XML string resource formatting rules as documented in the [Android Developer Documentation](vscode-file://vscode-app/Applications/Visual%20Studio%20Code%203.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html).

The escaping logic is minimal and focused, only affecting the display strings in [strings.xml](vscode-file://vscode-app/Applications/Visual%20Studio%20Code%203.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) without impacting any other Android generation code or Kotlin identifiers.





Fixes tauri-apps#14634

When productName contains special characters like single quotes (e.g., "Aircraft's Hub"),
the generated Android strings.xml file would fail to build because these characters
weren't being escaped according to Android XML string resource rules.

This commit adds an 'android-escape' Handlebars helper that properly escapes:
- Single quotes (') -> (\')
- Backslashes (\) -> (\\)
- Double quotes (") -> (\")
- Newlines, carriage returns, and tabs

The helper is now applied to app.stylized-name in the strings.xml template.
Copilot AI review requested due to automatic review settings December 10, 2025 07:42
@YASHRDX0001 YASHRDX0001 requested a review from a team as a code owner December 10, 2025 07:42
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Dec 10, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where product names containing special characters (like single quotes) in tauri.conf.json caused Android build failures due to improper XML escaping in the generated strings.xml file.

Key Changes:

  • Added android_escape Handlebars helper function to properly escape special characters for Android XML string resources
  • Updated the strings.xml template to apply escaping to both app_name and main_activity_title strings

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/tauri-cli/src/mobile/init.rs Implements the android_escape helper function with proper character escaping logic (backslashes, quotes, special whitespace) and registers it with the Handlebars instance
crates/tauri-cli/templates/mobile/android/app/src/main/res/values/strings.xml Updates template to use {{android-escape app.stylized-name}} instead of unescaped {{app.stylized-name}} for both string resources

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

out: &mut dyn Output,
) -> HelperResult {
// Escape special characters for Android XML strings
// Single quotes must be escaped with a backslash
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incomplete. It only mentions single quotes, but the function also escapes backslashes, double quotes, newlines, carriage returns, and tabs. Consider updating to:

// Escape special characters for Android XML strings
// Backslashes, quotes, and special whitespace must be escaped with a backslash
Suggested change
// Single quotes must be escaped with a backslash
// Backslashes, quotes, and special whitespace must be escaped with a backslash

Copilot uses AI. Check for mistakes.
Comment on lines +238 to +242
let escaped = input.replace('\\', "\\\\").replace('\'', "\\'")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t");
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent formatting: line 238 chains two replacements together while lines 239-242 have one replacement per line. For better readability and maintainability, consider formatting consistently with one replacement per line:

let escaped = input
  .replace('\\', "\\\\")
  .replace('\'', "\\'")
  .replace('"', "\\\"")
  .replace('\n', "\\n")
  .replace('\r', "\\r")
  .replace('\t', "\\t");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📬Proposal

Development

Successfully merging this pull request may close these issues.

[bug] Android gen doesn't escape single quote character

1 participant