-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(android): escape special characters in product name for strings.xml #14640
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
base: dev
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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_escapeHandlebars helper function to properly escape special characters for Android XML string resources - Updated the
strings.xmltemplate to apply escaping to bothapp_nameandmain_activity_titlestrings
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 |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
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| // Single quotes must be escaped with a backslash | |
| // Backslashes, quotes, and special whitespace must be escaped with a backslash |
| let escaped = input.replace('\\', "\\\\").replace('\'', "\\'") | ||
| .replace('"', "\\\"") | ||
| .replace('\n', "\\n") | ||
| .replace('\r', "\\r") | ||
| .replace('\t', "\\t"); |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
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");
Bug fix
Fixes #14634
Description
When
productNameintauri.conf.jsoncontains special characters like single quotes (e.g., "Aircraft's Hub"), the generated Androidstrings.xmlfile 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:
') must be escaped as\'\) must be escaped as\\") must be escaped as\"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:
Created
android_escapehelper function (crates/tauri-cli/src/mobile/init.rs):'→\'\→\\"→\"\n→\\n\r→\\r\t→\\tRegistered the helper in the Handlebars instance
Updated the template (
crates/tauri-cli/templates/mobile/android/app/src/main/res/values/strings.xml):{{app.stylized-name}}to{{android-escape app.stylized-name}}app_nameandmain_activity_titlestringsChanges 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.