Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Android: a Standard and an HD APK, each updating to its own kind

Duarte Santos0559184e2f9e
diff --git a/frontend/src/lib/update.js b/frontend/src/lib/update.js
index 793191663..70c3b837c 100644
--- a/frontend/src/lib/update.js
+++ b/frontend/src/lib/update.js
@@ -10,6 +10,7 @@
// against its SHA-256 by the native Install plugin and handed to the system installer.
import { MOBILE } from './mobile.js'
+import { BUILTIN_PX } from './anim-quality.js'
const REPO = 'DuarteSantos8/openGym'
const LATEST_URL = `https://api.github.com/repos/${REPO}/releases/latest`
@@ -63,12 +64,31 @@ async function fetchLatest() {
const latestVersion = String(latest.tag_name || '').replace(/^v/, '')
if (!latestVersion) return { hasUpdate: false, latestVersion: __APP_VERSION__, apkUrl: null, hashUrl: null }
const hasUpdate = compareSemver(latestVersion, __APP_VERSION__) > 0
- const assets = latest.assets || []
- const apk = assets.find(a => /\.apk$/i.test(a.name))
- const hash = assets.find(a => /\.apk\.sha256$/i.test(a.name))
+ const { apk, hash } = pickApk(latest.assets || [])
return { hasUpdate, latestVersion, apkUrl: apk?.browser_download_url || null, hashUrl: hash?.browser_download_url || null }
}
+/**
+ * Since v1.4.2 a release carries two sideloaded APKs: the Standard one (180 px animations built in,
+ * openGym-<v>.apk) and the HD one (sharper animations, openGym-HD-<v>.apk). Each installed APK
+ * only ever updates to its own kind, so a phone that picked HD keeps HD, and nobody is handed a
+ * download twice the size they never asked for. The build says which kind it is (VITE_APK_VARIANT, set
+ * by scripts/android/build-release.sh); a build without it goes by what it has built in.
+ * The checksum is the one named after the chosen APK, never just the first .sha256 in the list.
+ * An HD build finds no HD APK in a release: no direct download (the update row then opens the
+ * releases page), rather than quietly swapping it for the Standard one.
+ */
+const ENV = import.meta.env || {}
+export const APK_VARIANT = ENV.VITE_APK_VARIANT === 'hd' || ENV.VITE_APK_VARIANT === 'standard'
+ ? ENV.VITE_APK_VARIANT
+ : (BUILTIN_PX > 180 ? 'hd' : 'standard')
+const isHdApk = name => /(^|[-_.])hd([-_.]|$)/i.test(name)
+export function pickApk(assets, variant = APK_VARIANT) {
+ const apk = assets.find(a => /\.apk$/i.test(a.name) && isHdApk(a.name) === (variant === 'hd')) || null
+ const hash = apk ? assets.find(a => a.name === `${apk.name}.sha256`) || null : null
+ return { apk, hash }
+}
+
/**
* The SHA-256 the release publishes next to its APK, as 64 hex characters, or null. In the app it
* comes down natively like the APK itself (github.com answers a WebView fetch without CORS).
diff --git a/frontend/src/lib/update.test.js b/frontend/src/lib/update.test.js
index daee19f41..a37c9e5e3 100644
--- a/frontend/src/lib/update.test.js
+++ b/frontend/src/lib/update.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
-import { checkForUpdate, sha256, resetUpdateCheck, fetchChecksum } from './update.js'
+import { checkForUpdate, sha256, resetUpdateCheck, fetchChecksum, pickApk, APK_VARIANT } from './update.js'
// __APP_VERSION__ is defined at build time by vite.config.js (reads package.json).
// In the test environment vitest applies the same define, so it's available here.
@@ -228,3 +228,56 @@ describe('semver comparison (via checkForUpdate behavior)', () => {
expect((await checkForUpdate()).hasUpdate).toBe(false)
})
})
+
+// v1.4.2: a release carries a Standard and an HD APK. GitHub lists assets by name, so for the
+// names we publish the HD file would come first; each build must still find its own.
+describe('pickApk: Standard and HD APKs', () => {
+ const url = n => `https://github.com/DuarteSantos8/openGym/releases/download/v1.4.2/${n}`
+ const a = n => ({ name: n, browser_download_url: url(n) })
+ const RELEASE = ['openGym-1.4.2.apk', 'openGym-1.4.2.apk.sha256', 'openGym-HD-1.4.2.apk', 'openGym-HD-1.4.2.apk.sha256'].map(a)
+
+ it('a test build without a variant is the Standard APK', () => {
+ expect(APK_VARIANT).toBe('standard')
+ })
+
+ it('the Standard APK updates to the Standard APK and its own checksum', () => {
+ const { apk, hash } = pickApk(RELEASE, 'standard')
+ expect(apk.name).toBe('openGym-1.4.2.apk')
+ expect(hash.name).toBe('openGym-1.4.2.apk.sha256')
+ })
+
+ it('the HD APK updates to the HD APK and its own checksum, whatever the order', () => {
+ for (const list of [RELEASE, [...RELEASE].reverse()]) {
+ const { apk, hash } = pickApk(list, 'hd')
+ expect(apk.name).toBe('openGym-HD-1.4.2.apk')
+ expect(hash.name).toBe('openGym-HD-1.4.2.apk.sha256')
+ }
+ })
+
+ it('the Standard APK is never offered the HD one, even when it is listed first', () => {
+ const { apk, hash } = pickApk([...RELEASE].reverse(), 'standard')
+ expect(apk.name).toBe('openGym-1.4.2.apk')
+ expect(hash.name).toBe('openGym-1.4.2.apk.sha256')
+ })
+
+ it('also knows a -hd suffix', () => {
+ expect(pickApk(['openGym-1.5.0-hd.apk', 'openGym-1.5.0.apk'].map(a), 'hd').apk.name).toBe('openGym-1.5.0-hd.apk')
+ expect(pickApk(['openGym-1.5.0-hd.apk', 'openGym-1.5.0.apk'].map(a), 'standard').apk.name).toBe('openGym-1.5.0.apk')
+ })
+
+ it('an HD build is not moved to Standard when a release has no HD APK', () => {
+ const { apk, hash } = pickApk(['openGym-1.3.10.apk', 'openGym-1.3.10.apk.sha256'].map(a), 'hd')
+ expect(apk).toBe(null)
+ expect(hash).toBe(null)
+ })
+
+ it('names that only contain the letters hd are not HD', () => {
+ expect(pickApk([a('openGym-shdw-1.4.2.apk')], 'standard').apk.name).toBe('openGym-shdw-1.4.2.apk')
+ })
+
+ it('a checksum that belongs to another APK is not used', () => {
+ const { apk, hash } = pickApk(['openGym-HD-1.4.2.apk.sha256', 'openGym-1.4.2.apk'].map(a), 'standard')
+ expect(apk.name).toBe('openGym-1.4.2.apk')
+ expect(hash).toBe(null)
+ })
+})
diff --git a/scripts/android/build-release.sh b/scripts/android/build-release.sh
index 51b64927a..6c0850f84 100755
--- a/scripts/android/build-release.sh
+++ b/scripts/android/build-release.sh
@@ -1,10 +1,12 @@
#!/usr/bin/env bash
-# Builds and signs both Android releases on the release machine:
-# openGym-<version>.apk the sideloaded APK (GitHub, opengym.ch): updates itself
-# openGym-<version>.aab the Google Play bundle: Play updates it
-# Each from its own phone build (owner, v1.4.2): the APK carries the box-free 240 px animations
-# built in (quality/240) and has no packs; the Play bundle carries 180 px built in, like the App
-# Store app, and every sharper quality whose folder exists as an on-demand Play asset pack
+# Builds and signs the Android releases on the release machine:
+# openGym-<version>.apk the Standard sideloaded APK (GitHub, opengym.ch): 180 px built in
+# openGym-HD-<version>.apk the HD sideloaded APK: sharper animations built in (HD_PX, 360)
+# openGym-<version>.aab the Google Play bundle: Play updates it
+# Each from its own phone build (owner, v1.4.2). The two APKs have no packs and update themselves,
+# each to its own kind (lib/update.js pickApk, told by VITE_APK_VARIANT); both are the same app
+# with the same key, so installing one over the other keeps the data. The Play bundle carries
+# 180 px built in, like the App Store app, and every sharper quality whose folder exists as an on-demand Play asset pack
# (scripts/android/make-asset-packs.mjs), which Google Play delivers when someone picks it. These
# animations go into the packages and nowhere else (Gym visual licence, Part B), which is why this
# runs here and not in CI; the pack modules are taken out of the project again at the end. Same
@@ -13,7 +15,8 @@
#
# Usage: scripts/android/build-release.sh <keystore> <password-file> <alias-file> [out-dir]
# QUALITY_ROOT the private quality folders, default /mnt/backup-hdd/opengym-media/quality
-# APK_PX the APK's built-in size, default 240; PLAY_PX the bundle's, default 180
+# APK_PX the Standard APK's built-in size, default 180; PLAY_PX the bundle's, default 180
+# HD_PX the HD APK's built-in size, default 360; HD_PX=0 builds no HD APK
# NO_PACKS=1 a Play bundle without asset packs
set -euo pipefail
@@ -21,12 +24,13 @@ KS=${1:?keystore}; PASS=${2:?file with the keystore password}; ALIAS=$(cat "${3:
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
OUT=${4:-$PWD}
QUALITY_ROOT=${QUALITY_ROOT:-/mnt/backup-hdd/opengym-media/quality}
-APK_PX=${APK_PX:-240}
+APK_PX=${APK_PX:-180}
+HD_PX=${HD_PX:-360}
PLAY_PX=${PLAY_PX:-180}
export VITE_CLIP_EXT=webp
: "${ANDROID_HOME:?set ANDROID_HOME}"
BT=$(ls -d "$ANDROID_HOME"/build-tools/* | sort -V | tail -1)
-for px in "$APK_PX" "$PLAY_PX"; do
+for px in "$APK_PX" "$PLAY_PX" $([ "$HD_PX" != 0 ] && echo "$HD_PX"); do
[ -d "$QUALITY_ROOT/$px" ] || { echo "no $QUALITY_ROOT/$px" >&2; exit 1; }
done
@@ -35,40 +39,50 @@ VER=$(node -p "require('./package.json').version")
GRADLE_VER=$(sed -n 's/.*versionName "\(.*\)"/\1/p' android/app/build.gradle)
[ "$VER" = "$GRADLE_VER" ] || { echo "package.json says $VER, build.gradle says $GRADLE_VER" >&2; exit 1; }
-# The phone build with one size of animations built in: the files and the size the page assumes.
+# The phone build with one size of animations built in: the files and the size the page assumes,
+# and for an APK which kind it is, so its updater stays with that kind ($2: standard / hd).
phone_build() {
- APP_MEDIA_DIR="$QUALITY_ROOT/$1" VITE_BUILTIN_CLIP_PX="$1" npm run build:mobile
+ APP_MEDIA_DIR="$QUALITY_ROOT/$1" VITE_BUILTIN_CLIP_PX="$1" VITE_APK_VARIANT="${2:-standard}" npm run build:mobile
local n
n=$(find android/app/src/main/assets/public/exercise-media/clip -name '*.webp' | wc -l)
[ "$n" -gt 6000 ] || { echo "the $1 px phone build has only $n animations" >&2; exit 1; }
}
-# 1. The APK: 240 px built in, no packs.
+# 1. The APKs: Standard (180 px built in) and HD (HD_PX built in), no packs.
node "$ROOT/scripts/android/make-asset-packs.mjs" --clean
-phone_build "$APK_PX"
+phone_build "$APK_PX" standard
(cd android && ./gradlew --no-daemon clean assembleSideloadRelease)
APK_BUILT="$OUT/.openGym-$VER-sideload-unsigned.apk" # beside the outputs: the root disk has no room
cp android/app/build/outputs/apk/sideload/release/app-sideload-release-unsigned.apk "$APK_BUILT"
+HD_BUILT="$OUT/.openGym-HD-$VER-sideload-unsigned.apk"
+if [ "$HD_PX" != 0 ]; then
+ phone_build "$HD_PX" hd
+ (cd android && ./gradlew --no-daemon assembleSideloadRelease)
+ cp android/app/build/outputs/apk/sideload/release/app-sideload-release-unsigned.apk "$HD_BUILT"
+fi
# 2. The Play bundle: 180 px built in, the sharper ones as asset packs.
phone_build "$PLAY_PX"
if [ -z "${NO_PACKS:-}" ]; then
- trap 'node "$ROOT/scripts/android/make-asset-packs.mjs" --clean; rm -f "$APK_BUILT"' EXIT
+ trap 'node "$ROOT/scripts/android/make-asset-packs.mjs" --clean; rm -f "$APK_BUILT" "$HD_BUILT"' EXIT
node "$ROOT/scripts/android/make-asset-packs.mjs" --builtin "$PLAY_PX" --src-root "$QUALITY_ROOT"
else
- trap 'rm -f "$APK_BUILT"' EXIT
+ trap 'rm -f "$APK_BUILT" "$HD_BUILT"' EXIT
fi
(cd android && ./gradlew --no-daemon bundlePlayRelease)
-APK_IN=$APK_BUILT
AAB_IN=android/app/build/outputs/bundle/playRelease/app-play-release.aab
-"$BT/zipalign" -f -p 4 "$APK_IN" "$OUT/aligned.apk"
-"$BT/apksigner" sign --ks "$KS" --ks-pass "file:$PASS" --ks-key-alias "$ALIAS" \
- --out "$OUT/openGym-$VER.apk" "$OUT/aligned.apk"
-rm -f "$OUT/aligned.apk" "$OUT/openGym-$VER.apk.idsig"
-"$BT/apksigner" verify --print-certs "$OUT/openGym-$VER.apk" | grep -i "SHA-256"
-(cd "$OUT" && sha256sum "openGym-$VER.apk" > "openGym-$VER.apk.sha256")
+sign_apk() { # <unsigned> <name>
+ "$BT/zipalign" -f -p 4 "$1" "$OUT/aligned.apk"
+ "$BT/apksigner" sign --ks "$KS" --ks-pass "file:$PASS" --ks-key-alias "$ALIAS" \
+ --out "$OUT/$2" "$OUT/aligned.apk"
+ rm -f "$OUT/aligned.apk" "$OUT/$2.idsig"
+ "$BT/apksigner" verify --print-certs "$OUT/$2" | grep -i "SHA-256"
+ (cd "$OUT" && sha256sum "$2" > "$2.sha256")
+}
+sign_apk "$APK_BUILT" "openGym-$VER.apk"
+[ "$HD_PX" = 0 ] || sign_apk "$HD_BUILT" "openGym-HD-$VER.apk"
# An app bundle is signed like a jar; Play checks it with the upload key and re-signs the APKs
# it builds from it with the app signing key.
@@ -77,7 +91,7 @@ jarsigner -keystore "$KS" -storepass:file "$PASS" -keypass:file "$PASS" \
-sigalg SHA256withRSA -digestalg SHA-256 "$OUT/openGym-$VER.aab" "$ALIAS" >/dev/null
jarsigner -verify "$OUT/openGym-$VER.aab" | grep -q "jar verified" || { echo "the bundle did not verify" >&2; exit 1; }
-ls -la "$OUT/openGym-$VER.apk" "$OUT/openGym-$VER.aab"
+ls -la "$OUT"/openGym-*"$VER".apk "$OUT/openGym-$VER.aab"
# Google Play's limits: 500 MB for the base module, 1.5 GB per asset pack (the packs are split
# below that by make-asset-packs.mjs). The base is what the bundle holds outside the packs.
BASE=$(unzip -l "$OUT/openGym-$VER.aab" | awk '$4 != "" && $4 !~ /^anim_/ {s+=$1} END {print s}')