[android, tools] remove unused XML strings; add script to find unused XML strings (#2777)

Signed-off-by: lizzie <lizzie@eden-emu.dev>
Co-authored-by: crueter <crueter@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2777
Reviewed-by: crueter <crueter@eden-emu.dev>
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Co-authored-by: lizzie <lizzie@eden-emu.dev>
Co-committed-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2025-10-19 04:57:47 +02:00 committed by crueter
parent a53823646c
commit df653d6ca4
No known key found for this signature in database
GPG key ID: 425ACD2D4830EBC6
27 changed files with 89 additions and 1800 deletions

View file

@ -8,6 +8,7 @@ Tools for Eden and other subprojects.
## Eden
- `find-unused-strings.pl`: Find unused strings (for Android XML files).
- `shellcheck.sh`: Ensure POSIX compliance (and syntax sanity) for all tools in this directory and subdirectories.
- `llvmpipe-run.sh`: Sets environment variables needed to run any command (or Eden) with llvmpipe.
- `optimize-assets.sh`: Optimize PNG assets with OptiPng.
@ -20,6 +21,12 @@ Tools for Eden and other subprojects.
- `clang-format.sh`: Runs `clang-format` on the entire codebase.
* Requires: clang
## Android
It's recommended to run these scritps after almost any Android change, as they are relatively fast and important both for APK bloat and CI.
- `unused-strings.sh`: Finds unused strings in `strings.xml` files.
- `stale-translations.sh`: Finds translated strings that aren't present in the source `strings.xml` file.
## Translations
- [Translation Scripts](./translations)

34
tools/stale-translations.sh Executable file
View file

@ -0,0 +1,34 @@
#!/bin/sh -e
# SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later
ANDROID=src/android/app/src/main
STRINGS=$ANDROID/res/values/strings.xml
TMP_DIR=$(mktemp -d)
SRC="$TMP_DIR"/src
LOCALES="$TMP_DIR"/locales
TRANSLATED="$TMP_DIR"/translated
COMBINED="$TMP_DIR"/combined
# We start out by getting the list of source strings so we can compare all locale strings to this
grep -e "string name" $STRINGS | grep -v 'translatable="false"' | cut -d'"' -f2 > "$SRC"
# ... then we determine what locales to check
find $ANDROID/res/values-* -name 'strings.xml' > "$LOCALES"
while IFS= read -r locale; do
echo "Locale $(echo "$locale" | cut -d"-" -f2- | cut -d"/" -f1)"
grep -e "string name" "$locale" | cut -d'"' -f2 > "$TRANSLATED"
cat "$TRANSLATED" "$SRC" | sort | uniq -u > "$COMBINED"
# This will also include strings that are present in the source but NOT translated, so we filter those out now:
while IFS= read -r unused; do
{ grep -e "string name=\"$unused\"" "$STRINGS" >/dev/null; } || \
{ echo "-- Removing unused translation $unused" && sed "/string name=\"$unused\"/d" "$locale" > "$locale.new" && mv "$locale.new" "$locale"; }
done < "$COMBINED"
echo "-- done"
done < "$LOCALES"

48
tools/unused-strings.sh Executable file
View file

@ -0,0 +1,48 @@
#!/bin/sh -e
# SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later
ANDROID=src/android/app/src/main
STRINGS=$ANDROID/res/values/strings.xml
TMP_DIR=$(mktemp -d)
USED="$TMP_DIR"/used
UNUSED="$TMP_DIR"/unused
FILTERED="$TMP_DIR"/filtered
ALL_STRINGS="$TMP_DIR"/all
# First we find what files to search and what strings are defined
find $ANDROID -type f -iname '*.xml' -a -not -iname '*strings.xml' -o -iname '*.kt' | grep -v drawable >"$TMP_DIR"/files
grep -e "string name" $STRINGS | cut -d'"' -f2 >"$ALL_STRINGS"
# then get a list of all strings that are actually used
set +e
while IFS= read -r file; do
grep -o 'R\.string\.[a-zA-Z0-9_]\+' "$file" >> "$USED"
grep -o '@string/[a-zA-Z0-9_]\+' "$file" >> "$USED"
done <"$TMP_DIR"/files
set -e
# filter out "@string/" and "R.string." from the strings to get the raw names
sed 's/R.string.\|@string\///' "$USED" | sort -u | grep -v app_name_suffixed > "$FILTERED"
# now we run a sort + uniq -u pass - this basically removes all strings that are
# present in BOTH the used strings list AND strings.xml
# thus giving us strings that are either ONLY used in strings.xml OR in the files
# NB: This also gives strings that may be found in comments but nowhere else,
# as well as some android builtins, but sed-ing those out of strings.xml is a no-op
# e.g. appbar_scrolling_view_behavior
cat "$FILTERED" "$ALL_STRINGS" | sort | uniq -u > "$UNUSED"
# finally, print out all unused strings and remove them from ALL strings.xml definitions
while IFS= read -r string; do
echo "$string"
find $ANDROID/res -iname '*strings.xml' | while IFS= read -r file; do
sed "/string name=\"$string\"/d" "$file" > "$file.new"
mv "$file.new" "$file"
done
done < "$UNUSED"