diff options
| author | lizzie <lizzie@eden-emu.dev> | 2025-10-19 04:57:47 +0200 |
|---|---|---|
| committer | crueter <crueter@eden-emu.dev> | 2025-10-19 04:57:47 +0200 |
| commit | df653d6ca469de48574193eab4398eb04f678916 (patch) | |
| tree | eaae9b929fca6b6a71466d748d7985ad49b88b63 /tools | |
| parent | a53823646c6b7043579918fc7dd732ca82336acf (diff) | |
[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>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/README.md | 7 | ||||
| -rwxr-xr-x | tools/stale-translations.sh | 34 | ||||
| -rwxr-xr-x | tools/unused-strings.sh | 48 |
3 files changed, 89 insertions, 0 deletions
diff --git a/tools/README.md b/tools/README.md index 35fd7a390c..061e95bc9c 100644 --- a/tools/README.md +++ b/tools/README.md @@ -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)
\ No newline at end of file diff --git a/tools/stale-translations.sh b/tools/stale-translations.sh new file mode 100755 index 0000000000..791b857a36 --- /dev/null +++ b/tools/stale-translations.sh @@ -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"
\ No newline at end of file diff --git a/tools/unused-strings.sh b/tools/unused-strings.sh new file mode 100755 index 0000000000..ed8c5d9584 --- /dev/null +++ b/tools/unused-strings.sh @@ -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" |
