soul-browser/scripts/compile-ocr-runtime.sh
KaKi87 e2c18960ec
Fix PDF translation stuck on "Updating the text module" (#22)
* Bundle on-device ML Kit OCR to fix PDF translation hang

Sideloaded builds never receive Play Services install-time OCR modules, so
image/PDF translation could sit forever on "Updating the text module".
Ship bundled text-recognition models/native code via the build script, and
cap DialogOcrLoad retries so the wait dialog cannot loop indefinitely.
Bump version to 2.0.0-testing.36.

Co-authored-by: KaKi87 <KaKi87@pm.me>

* Add GMS dynamic API aliases for bundled OCR

Bundled text-recognition-common calls IObjectWrapper.Stub.asInterface and
ObjectWrapper.unwrap, but R8 renamed those to I1/f2 in this APK. Add public
aliases so OCR init no longer throws NoSuchMethodError. Bump to 2.0.0-testing.37.

Co-authored-by: KaKi87 <KaKi87@pm.me>

* Restore ImageUtils APIs required by bundled OCR

text-recognition-bundled-common calls ImageUtils.getInstance and
getUprightRotationMatrix, which R8 had stripped from vision-common.
Restore those methods, add ImageConvertUtils buffer helpers and
ObjectWrapper.wrap for the same public API surface. Bump to 2.0.0-testing.38.

Co-authored-by: KaKi87 <KaKi87@pm.me>

* Move restored ImageUtils helpers to smali_classes4

classes3 is at the DEX method-ref ceiling; relocating ImageUtils and
ImageConvertUtils keeps apktool builds reliable after restoring the
bundled-OCR public APIs.

Co-authored-by: KaKi87 <KaKi87@pm.me>

* Download ML Kit OCR models at runtime from Maven

Stop packaging OCR assets and native libs into the APK. DialogOcrLoad now
fetches text-recognition AARs from Google Maven, extracts models + the
pipeline .so, and activates them via System.load / AssetManager.addAssetPath.
Keep only the small module dex in the APK (JVM jars cannot be loaded on-device).
Bump to 2.0.0-testing.39.

Co-authored-by: KaKi87 <KaKi87@pm.me>

* Move OCR install helpers to smali_classes4

Avoid DEX method-ref overflow in classes3 when assembling DialogOcrLoad
install task/progress classes.

Co-authored-by: KaKi87 <KaKi87@pm.me>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-08-12 16:53:38 +02:00

39 lines
1.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Compile the OCR runtime installer to a dex file for injection into the APK.
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC_DIR="${ROOT_DIR}/sources/ocr-runtime"
OUT_DIR="${ROOT_DIR}/tools/ocr-runtime"
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 || true)"
if [[ -z "${ANDROID_JAR}" ]]; then
echo "error: android.jar not found under ${ANDROID_HOME}/platforms" >&2
exit 1
fi
if [[ -z "${D8}" ]]; then
echo "error: d8 not found under ${ANDROID_HOME}/build-tools" >&2
exit 1
fi
mkdir -p "${OUT_DIR}/classes"
find "${OUT_DIR}/classes" -type f -name '*.class' -delete
echo "Compiling OCR runtime installer..."
javac --release 11 -classpath "${ANDROID_JAR}" -d "${OUT_DIR}/classes" \
"${SRC_DIR}/com/mycompany/app/ocr/OcrRuntimeInstaller.java"
STAGE="$(mktemp -d)"
cleanup() { rm -rf "${STAGE}"; }
trap cleanup EXIT
(
cd "${OUT_DIR}/classes"
jar cf "${STAGE}/installer.jar" .
)
"${D8}" --release --output "${STAGE}" "${STAGE}/installer.jar"
cp "${STAGE}/classes.dex" "${OUT_DIR}/classes_ocr_installer.dex"
echo "Wrote ${OUT_DIR}/classes_ocr_installer.dex ($(du -h "${OUT_DIR}/classes_ocr_installer.dex" | awk '{print $1}'))"