soul-browser/scripts/compile-text-select-extras.sh
KaKi87 d618fe707b Add Search in preview and Find in page to text selection.
Append the two items onto the stock ActionMode instead of rebuilding it, so Cut/Paste and process-text apps stay available.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-05 16:02:04 +02:00

87 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC="${ROOT_DIR}/sources/java/com/mycompany/app/web/TextSelectExtras.java"
OUT_SMALI="${ROOT_DIR}/app/smali_classes4/com/mycompany/app/web"
TOOLS="${ROOT_DIR}/tools"
ANDROID_HOME="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-${HOME}/android-sdk}}"
ANDROID_JAR="$(ls -1 "${ANDROID_HOME}/platforms"/android-*/android.jar 2>/dev/null | sort -V | tail -1)"
D8="$(find "${ANDROID_HOME}/build-tools" -name d8 -type f 2>/dev/null | sort -V | tail -1)"
APK="$(ls -1 "${ROOT_DIR}/dist"/soul-browser-*-merged-unsigned.apk 2>/dev/null | sort | tail -1 || true)"
if [[ -z "${APK}" || ! -f "${APK}" ]]; then
APK="$(ls -1 /home/kaki/.t3/worktrees/soul-browser/*/dist/soul-browser-*-merged-unsigned.apk 2>/dev/null | sort | tail -1 || true)"
fi
DEX_TOOLS_ZIP="${TOOLS}/dex-tools-v2.4.zip"
DEX_TOOLS_DIR="${TOOLS}/dex-tools-v2.4"
mkdir -p "${TOOLS}" "${OUT_SMALI}"
if [[ ! -f "${ANDROID_JAR}" ]]; then
echo "error: android.jar not found" >&2
exit 1
fi
if [[ -z "${D8}" ]]; then
echo "error: d8 not found" >&2
exit 1
fi
if [[ -z "${APK}" || ! -f "${APK}" ]]; then
echo "error: merged unsigned APK not found; run ./scripts/build.sh first" >&2
exit 1
fi
if [[ ! -f "${DEX_TOOLS_DIR}/d2j-baksmali.sh" ]]; then
if [[ ! -f "${DEX_TOOLS_ZIP}" ]]; then
echo "Downloading dex-tools v2.4..."
curl -fsSL -o "${DEX_TOOLS_ZIP}" \
"https://github.com/pxb1988/dex2jar/releases/download/v2.4/dex-tools-v2.4.zip"
fi
unzip -qo "${DEX_TOOLS_ZIP}" -d "${TOOLS}"
fi
chmod +x "${DEX_TOOLS_DIR}/"*.sh "${DEX_TOOLS_DIR}/d2j_invoke.sh" 2>/dev/null || true
D2J="${DEX_TOOLS_DIR}/d2j-dex2jar.sh"
STAGE="$(mktemp -d)"
cleanup() { rm -rf "${STAGE}"; }
trap cleanup EXIT
echo "Building classpath from APK dex files..."
CP="${ANDROID_JAR}"
for dex in classes.dex classes2.dex classes3.dex classes4.dex; do
unzip -p "${APK}" "${dex}" > "${STAGE}/${dex}" 2>/dev/null || true
if [[ -s "${STAGE}/${dex}" ]]; then
jar="${STAGE}/${dex%.dex}.jar"
bash "${D2J}" --force "${STAGE}/${dex}" -o "${jar}" >/dev/null
CP="${CP}:${jar}"
fi
done
# search_in_preview may be missing from the baseline APK R$string.
STUBS="${STAGE}/stubs"
mkdir -p "${STUBS}/com/mycompany/app/soulbrowser"
cat > "${STUBS}/com/mycompany/app/soulbrowser/R.java" <<'EOF'
package com.mycompany.app.soulbrowser;
public final class R {
public static final class string {
public static int search_in_preview = 0x7f1205b5;
public static int find_word = 0x7f1201c4;
public static int empty = 0x7f120195;
}
}
EOF
javac --release 11 -d "${STUBS}" "${STUBS}/com/mycompany/app/soulbrowser/R.java"
CP="${STUBS}:${CP}"
echo "Compiling TextSelectExtras..."
javac --release 11 -classpath "${CP}" -d "${STAGE}/classes" "${SRC}"
mkdir -p "${STAGE}/dex"
mapfile -t CLASS_FILES < <(find "${STAGE}/classes" -name '*.class' -type f)
"${D8}" --min-api 23 --lib "${ANDROID_JAR}" --output "${STAGE}/dex" "${CLASS_FILES[@]}"
bash "${DEX_TOOLS_DIR}/d2j-baksmali.sh" -f -o "${STAGE}/out" "${STAGE}/dex/classes.dex"
find "${STAGE}/out" -name '*.smali' -print0 | xargs -0 sed -i \
-e 's/\\u002d\$\$Nest\$/\-$$Nest$/g' \
-e 's|Lcom/mycompany/app/soulbrowser/R\$|Lnet/kaki87/soul2/testing/R\$|g'
rm -f "${OUT_SMALI}"/TextSelectExtras*.smali
cp -a "${STAGE}/out/com/mycompany/app/web/TextSelectExtras"* "${OUT_SMALI}/"
echo "Wrote ${OUT_SMALI}/TextSelectExtras*.smali"