soul-browser/scripts/prepare-bundled-ocr.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

100 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Stage the small OCR module dex (ModuleDescriptors + BundledTextRecognizerCreator)
# used by the thick ML Kit path. Model assets and native libs are NOT packaged;
# OcrRuntimeInstaller downloads those from Google Maven at runtime.
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUT_DIR="${ROOT_DIR}/bundled-ocr"
CACHE_DIR="${ROOT_DIR}/tools/mlkit-aar-cache"
ANDROID_HOME="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-${HOME}/android-sdk}}"
MAVEN="https://dl.google.com/dl/android/maven2/com/google/mlkit"
TEXT_RECOGNITION_VERSION="16.0.1"
BUNDLED_COMMON_VERSION="17.0.0"
mkdir -p "${CACHE_DIR}" "${OUT_DIR}"
download() {
local url="$1"
local dest="$2"
if [[ -f "${dest}" ]]; then
return 0
fi
echo " downloading $(basename "${dest}")..."
curl -fsSL -o "${dest}.tmp" "${url}"
mv "${dest}.tmp" "${dest}"
}
echo "Preparing OCR module dex (runtime downloads models/libs)..."
download \
"${MAVEN}/text-recognition/${TEXT_RECOGNITION_VERSION}/text-recognition-${TEXT_RECOGNITION_VERSION}.aar" \
"${CACHE_DIR}/text-recognition.aar"
download \
"${MAVEN}/text-recognition-bundled-common/${BUNDLED_COMMON_VERSION}/text-recognition-bundled-common-${BUNDLED_COMMON_VERSION}.aar" \
"${CACHE_DIR}/text-recognition-bundled-common.aar"
for lang in chinese japanese korean devanagari; do
download \
"${MAVEN}/text-recognition-${lang}/${TEXT_RECOGNITION_VERSION}/text-recognition-${lang}-${TEXT_RECOGNITION_VERSION}.aar" \
"${CACHE_DIR}/text-recognition-${lang}.aar"
done
STAGE="$(mktemp -d)"
cleanup() { rm -rf "${STAGE}"; }
trap cleanup EXIT
extract_aar() {
local aar="$1"
local dest="$2"
mkdir -p "${dest}"
unzip -qo "${aar}" -d "${dest}"
find "${dest}" -type f -exec chmod u+w {} \;
}
extract_aar "${CACHE_DIR}/text-recognition.aar" "${STAGE}/latin"
extract_aar "${CACHE_DIR}/text-recognition-bundled-common.aar" "${STAGE}/common"
for lang in chinese japanese korean devanagari; do
extract_aar "${CACHE_DIR}/text-recognition-${lang}.aar" "${STAGE}/${lang}"
done
# Do not stage assets/ or lib/ — those are downloaded on-device by OcrRuntimeInstaller.
rm -rf "${OUT_DIR}/assets" "${OUT_DIR}/lib"
# Merge Java classes: bundled pipeline + ModuleDescriptors for each script.
JAR_MERGE="${STAGE}/jar-merge"
mkdir -p "${JAR_MERGE}"
(
cd "${JAR_MERGE}"
jar xf "${STAGE}/common/classes.jar"
jar xf "${STAGE}/latin/classes.jar"
for lang in chinese japanese korean devanagari; do
jar xf "${STAGE}/${lang}/classes.jar"
done
jar cf "${STAGE}/ocr-bundled.jar" .
)
D8="$(find "${ANDROID_HOME}/build-tools" -name d8 -type f 2>/dev/null | sort -V | tail -1 || true)"
if [[ -z "${D8}" ]]; then
echo "error: d8 not found under ${ANDROID_HOME}/build-tools" >&2
exit 1
fi
DEX_OUT="${STAGE}/dex-out"
mkdir -p "${DEX_OUT}"
"${D8}" --release --output "${DEX_OUT}" "${STAGE}/ocr-bundled.jar"
cp "${DEX_OUT}/classes.dex" "${OUT_DIR}/classes_ocr.dex"
cat > "${OUT_DIR}/README.md" << 'EOF'
# OCR module dex (not full models)
`classes_ocr.dex` contains ModuleDescriptors + BundledTextRecognizerCreator so the
thick ML Kit path works without Play Services. Model assets and
`libmlkit_google_ocr_pipeline.so` are downloaded at runtime from Google Maven by
`OcrRuntimeInstaller` (see DialogOcrLoad).
Regenerate with: `./scripts/prepare-bundled-ocr.sh`
EOF
echo "OCR module dex ready at ${OUT_DIR} ($(du -sh "${OUT_DIR}" | awk '{print $1}'))"