1
0

android: build BarcodeScannerView from src

Adds a script `make_barcode_scanner.sh` which builds the
`BarcodeScannerView` library and its dependencies, `zxing-cpp` and
`CameraView` from source. Builds `zxing-cpp` architecture dependent
reducing the final apk size.
This commit is contained in:
f321x
2025-06-26 15:42:49 +02:00
parent 5ae2deb704
commit 10b75ebed7
4 changed files with 172 additions and 54 deletions

View File

@@ -1,50 +0,0 @@
#!/bin/bash
# script to fetch and pin https://github.com/markusfisch/BarcodeScannerView and its dependencies,
# https://github.com/markusfisch/CameraView/ and https://github.com/markusfisch/zxing-cpp
# which are being used as barcode scanner in the Android app.
# To bump the version of BarcodeScannerView, get the newest version tag from the github repo,
# then get the required dependencies from
# https://jitpack.io/com/github/markusfisch/BarcodeScannerView/**NEWEST_VERSION**/BarcodeScannerView-**NEWEST_VERSION**.pom
# then fetch the aars from jitpack and update the versions and sha256s below. Also update kotlin-stdlib
# in buildozer_qml.spec
BARCODE_SCANNER_VIEW_VERSION="1.6.0"
BARCODE_SCANNER_VIEW_AAR_SHA256="2be6c9a5ab86f7198683af4a6c0e5acd3e8fe6a02df2d12c3b716dc422537789"
CAMERA_VIEW_VERSION="1.9.2"
CAMERA_VIEW_AAR_SHA256="3c9be35d29b84637d2a2b0e0e7253bc5a35408fafb26c5cb7225aeb7326e2be4"
ZXING_CPP_VERSION="v2.2.0.1"
ZXING_CPP_AAR_SHA256="7991381f181ff16555c4ac9c5d83e6a0d3a7da896efb8c3807897305ca33b957"
DOWNLOAD_REPOSITORY_ROOT="https://jitpack.io/com/github/markusfisch"
set -e
CONTRIB_ANDROID="$(dirname "$(readlink -e "$0")")"
CONTRIB="$CONTRIB_ANDROID"/..
CACHEDIR="$CONTRIB_ANDROID/.cache"
. "$CONTRIB"/build_tools_util.sh
# check if $CACHEDIR/aars exists, create it if not
if [ ! -d "$CACHEDIR/aars" ]; then
mkdir -p "$CACHEDIR/aars"
fi
info "Fetching BarcodeScannerView..."
download_if_not_exist "$CACHEDIR/aars/BarcodeScannerView.aar" \
"${DOWNLOAD_REPOSITORY_ROOT}/BarcodeScannerView/${BARCODE_SCANNER_VIEW_VERSION}/BarcodeScannerView-${BARCODE_SCANNER_VIEW_VERSION}.aar"
verify_hash "$CACHEDIR/aars/BarcodeScannerView.aar" "$BARCODE_SCANNER_VIEW_AAR_SHA256"
info "Fetching CameraView..."
download_if_not_exist "$CACHEDIR/aars/CameraView.aar" \
"${DOWNLOAD_REPOSITORY_ROOT}/CameraView/${CAMERA_VIEW_VERSION}/CameraView-${CAMERA_VIEW_VERSION}.aar"
verify_hash "$CACHEDIR/aars/CameraView.aar" "$CAMERA_VIEW_AAR_SHA256"
info "Fetching zxing-cpp..."
download_if_not_exist "$CACHEDIR/aars/zxing-cpp.aar" \
"${DOWNLOAD_REPOSITORY_ROOT}/zxing-cpp/${ZXING_CPP_VERSION}/zxing-cpp-${ZXING_CPP_VERSION}.aar"
verify_hash "$CACHEDIR/aars/zxing-cpp.aar" "$ZXING_CPP_AAR_SHA256"

View File

@@ -27,10 +27,6 @@ info "preparing electrum-locale."
rm -r "$PROJECT_ROOT/electrum/locale/locale"/*/electrum.po
)
# fetch barcode scanner aars
info "fetching barcode scanner aars."
"$CONTRIB_ANDROID"/fetch_barcode_scanner.sh || fail "fetch_barcode_scanner.sh failed"
pushd "$CONTRIB_ANDROID"
info "apk building phase starts."
@@ -90,16 +86,22 @@ if [[ "$2" == "all" ]] ; then
# FIXME failures are not propagated out: we should fail the script if any arch build fails
export APP_ANDROID_ARCHS=armeabi-v7a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
export APP_ANDROID_ARCHS=arm64-v8a
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
export APP_ANDROID_ARCHS=x86_64
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
else
export APP_ANDROID_ARCHS=$2
export APP_ANDROID_NUMERIC_VERSION=$("$CONTRIB_ANDROID"/get_apk_versioncode.py "$APP_ANDROID_ARCHS")
"$CONTRIB_ANDROID"/make_barcode_scanner.sh "$APP_ANDROID_ARCHS" || fail "make_barcode_scanner.sh failed"
make $TARGET
fi

View File

@@ -0,0 +1,144 @@
#!/bin/bash
# script to clone and build https://github.com/markusfisch/BarcodeScannerView and its dependencies,
# https://github.com/markusfisch/CameraView/ and https://github.com/markusfisch/zxing-cpp
# which are being used as barcode scanner in the Android app.
# To bump the version of BarcodeScannerView, get the newest version tag from the github repo,
# then get the required dependencies from
# https://github.com/markusfisch/BarcodeScannerView/blob/**VERSION_TAG**/barcodescannerview/build.gradle
# then update the commit hashes below. Also update kotlin-stdlib in buildozer_qml.spec to the
# "kotlin-version" specified in the used zxing-cpp commit:
# https://github.com/markusfisch/zxing-cpp/blob/master/wrappers/aar/build.gradle
BARCODE_SCANNER_VIEW_COMMIT_HASH="a4928bf83c0aae8ecb80e665d93f10b70232455b" # 1.6.3
BARCODE_SCANNER_VIEW_REPO="https://github.com/markusfisch/BarcodeScannerView.git"
CAMERA_VIEW_COMMIT_HASH="745597d05bc6abfdb3637a09a8ecaf30fdce7b6e" # 1.10.0
CAMERA_VIEW_REPO="https://github.com/markusfisch/CameraView.git"
ZXING_CPP_COMMIT_HASH="0741a597409ff69a96a326f3a65fe6440d87ad99" # v2.2.0.5 using kotlin-stdlib 1.8.22
ZXING_CPP_REPO="https://github.com/markusfisch/zxing-cpp.git"
########################################################################################################
set -e
CONTRIB_ANDROID="$(dirname "$(readlink -e "$0")")"
CONTRIB="$CONTRIB_ANDROID"/..
CACHEDIR="$CONTRIB_ANDROID/.cache"
BUILDDIR="$CACHEDIR/builds"
. "$CONTRIB"/build_tools_util.sh
# target architecture passed as argument by`make_apk.sh`
TARGET_ARCH="$1"
# check if TARGET_ARCH is set and supported
if [[ "$TARGET_ARCH" != "armeabi-v7a" \
&& "$TARGET_ARCH" != "arm64-v8a" \
&& "$TARGET_ARCH" != "x86_64" ]]; then
fail "build_barcode_scanner.sh invalid target architecture argument: $TARGET_ARCH"
fi
info "Building BarcodeScannerView and deps for architecture: $TARGET_ARCH"
# check if directories exist, create them if not
if [ ! -d "$CACHEDIR/aars" ]; then
mkdir -p "$CACHEDIR/aars"
fi
if [ ! -d "$BUILDDIR" ]; then
mkdir -p "$BUILDDIR"
fi
####### zxing-cpp ########
# check if zxing-cpp aar is already in cachedir, else build it
ZXING_CPP_BUILD_ID="$TARGET_ARCH-$ZXING_CPP_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar" ]; then
info "zxing-cpp for $ZXING_CPP_BUILD_ID already exists in cache, skipping build."
cp "$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar" "$CACHEDIR/aars/zxing-cpp.aar"
else
info "Building zxing-cpp for $ZXING_CPP_BUILD_ID..."
ZXING_CPP_DIR="$BUILDDIR/zxing-cpp"
clone_or_update_repo "$ZXING_CPP_REPO" "$ZXING_CPP_COMMIT_HASH" "$ZXING_CPP_DIR"
cd "$ZXING_CPP_DIR/wrappers/aar"
chmod +x gradlew
# Set local.properties to use SDK of docker container
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
# gradlew will install a specific NDK version required by zxing-cpp
./gradlew :zxingcpp:assembleRelease -Pandroid.injected.build.abi="$TARGET_ARCH"
# Copy the built AAR to cache directory
ZXING_AAR_SOURCE="$ZXING_CPP_DIR/wrappers/aar/zxingcpp/build/outputs/aar/zxingcpp-release.aar"
ZXING_AAR_DEST_GENERIC="$CACHEDIR/aars/zxing-cpp.aar"
ZXING_AAR_DEST_SPECIFIC="$CACHEDIR/aars/zxing-cpp-$ZXING_CPP_BUILD_ID.aar"
if [ ! -f "$ZXING_AAR_SOURCE" ]; then
fail "zxing-cpp AAR not found at $ZXING_AAR_SOURCE, build failed?"
fi
cp "$ZXING_AAR_SOURCE" "$ZXING_AAR_DEST_GENERIC"
# keeping an arch specific copy allows to skip the build later if it already exists
cp "$ZXING_AAR_SOURCE" "$ZXING_AAR_DEST_SPECIFIC"
info "zxing-cpp AAR copied to $ZXING_AAR_DEST_GENERIC"
fi
########### CameraView ###########
CAMERA_VIEW_BUILD_ID="$CAMERA_VIEW_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar" ]; then
info "CameraView AAR already exists in cache, skipping build."
cp "$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar" "$CACHEDIR/aars/CameraView.aar"
else
info "Building CameraView..."
CAMERA_VIEW_DIR="$BUILDDIR/CameraView"
clone_or_update_repo "$CAMERA_VIEW_REPO" "$CAMERA_VIEW_COMMIT_HASH" "$CAMERA_VIEW_DIR"
cd "$CAMERA_VIEW_DIR"
chmod +x gradlew
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
./gradlew :cameraview:assembleRelease
CAMERA_AAR_SOURCE="$CAMERA_VIEW_DIR/cameraview/build/outputs/aar/cameraview-release.aar"
CAMERA_AAR_DEST_GENERIC="$CACHEDIR/aars/CameraView.aar"
CAMERA_AAR_DEST_SPECIFIC="$CACHEDIR/aars/CameraView-$CAMERA_VIEW_BUILD_ID.aar"
if [ ! -f "$CAMERA_AAR_SOURCE" ]; then
fail "CameraView AAR not found at $CAMERA_AAR_SOURCE"
fi
cp "$CAMERA_AAR_SOURCE" "$CAMERA_AAR_DEST_GENERIC"
cp "$CAMERA_AAR_SOURCE" "$CAMERA_AAR_DEST_SPECIFIC"
info "CameraView AAR copied to $CAMERA_AAR_DEST"
fi
########### BarcodeScannerView ###########
BARCODE_SCANNER_VIEW_BUILD_ID="$BARCODE_SCANNER_VIEW_COMMIT_HASH"
if [ -f "$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar" ]; then
info "BarcodeScannerView AAR already exists in cache, skipping build."
cp "$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar" "$CACHEDIR/aars/BarcodeScannerView.aar"
else
info "Building BarcodeScannerView..."
BARCODE_SCANNER_VIEW_DIR="$BUILDDIR/BarcodeScannerView"
clone_or_update_repo "$BARCODE_SCANNER_VIEW_REPO" "$BARCODE_SCANNER_VIEW_COMMIT_HASH" "$BARCODE_SCANNER_VIEW_DIR"
cd "$BARCODE_SCANNER_VIEW_DIR"
chmod +x gradlew
echo "sdk.dir=${ANDROID_SDK_HOME}" > local.properties
./gradlew :barcodescannerview:assembleRelease
BARCODE_AAR_SOURCE="$BARCODE_SCANNER_VIEW_DIR/barcodescannerview/build/outputs/aar/barcodescannerview-release.aar"
BARCODE_AAR_DEST_GENERIC="$CACHEDIR/aars/BarcodeScannerView.aar"
BARCODE_AAR_DEST_SPECIFIC="$CACHEDIR/aars/BarcodeScannerView-$BARCODE_SCANNER_VIEW_BUILD_ID.aar"
if [ ! -f "$BARCODE_AAR_SOURCE" ]; then
fail "BarcodeScannerView AAR not found at $BARCODE_AAR_SOURCE"
fi
cp "$BARCODE_AAR_SOURCE" "$BARCODE_AAR_DEST_GENERIC"
cp "$BARCODE_AAR_SOURCE" "$BARCODE_AAR_DEST_SPECIFIC"
info "BarcodeScannerView AAR copied to $BARCODE_AAR_DEST"
fi
info "All barcode scanner libraries built successfully for $TARGET_ARCH"

View File

@@ -50,6 +50,28 @@ function download_if_not_exist() {
fi
}
# Function to clone or update a git repository to a specific commit
clone_or_update_repo() {
local repo_url=$1
local commit_hash=$2
local repo_dir=$3
if [ -z "$repo_url" ] || [ -z "$commit_hash" ] || [ -z "$repo_dir" ]; then
fail "clone_or_update_repo: invalid arguments: repo_url='$repo_url', commit_hash='$commit_hash', repo_dir='$repo_dir'"
fi
if [ -d "$repo_dir" ]; then
info "Repository $repo_url exists in $repo_dir, updating..."
git -C "$repo_dir" clean -ffxd >/dev/null 2>&1 || fail "Failed to clean repository $repo_dir"
git -C "$repo_dir" fetch --all >/dev/null 2>&1 || fail "Failed to fetch from repository"
git -C "$repo_dir" reset --hard "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to reset to commit $commit_hash"
else
info "Cloning repository: $repo_url to $repo_dir"
git clone "$repo_url" "$repo_dir" >/dev/null 2>&1 || fail "Failed to clone repository $repo_url"
git -C "$repo_dir" checkout "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to checkout commit $commit_hash"
fi
}
# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh
function retry() {
local result=0