Mulberry/android/build.sh
astar76 320f644bc8 Android app: buildable project recovered from the 1.2.2 APK
The original Android source was lost; android/ holds the jadx-decompiled
Java (com.mulberry.player) and resources from Android 1.2.2, fixed so it
compiles again (broken methods rewritten by hand), plus:

- android/build.sh: builds and signs Mulberry-<version>-Android.apk with the
  plain SDK tools (aapt2, javac, d8, apksigner), no Gradle; fetches
  jaudiotagger 2.0.3 (the version the old APK bundled) with a pinned checksum
- versionName 2.0.4, versionCode 6
- recoloured launcher icon (pink cookie, berry note) to match assets/mulberry.svg

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-02 12:32:30 -06:00

70 lines
3.4 KiB
Shell
Executable file

#!/bin/bash
# Sources in src/ and res/ were recovered (jadx) from the Android 1.2.2 APK, whose original project was lost;
# methods the decompiler broke were rewritten by hand and are marked "Rewritten by hand".
# Builds Mulberry for Android with the plain SDK tools (aapt2, javac, d8, apksigner), no Gradle.
# Outputs dist/Mulberry-<version>-Android.apk, signed with the release keystore in $KEYSTORE.
# Needs: a JDK, and the SDK at $ANDROID_HOME with platforms;android-34 and build-tools;35.0.0:
# sdkmanager "platforms;android-34" "build-tools;35.0.0"
set -euo pipefail
cd "$(dirname "$0")"
VERSION=${VERSION:-$(cat VERSION)}
VERSION_CODE=${VERSION_CODE:-$(cat VERSION_CODE)}
ANDROID_HOME=${ANDROID_HOME:-$HOME/.local/opt/android-sdk}
BT=${BT:-$ANDROID_HOME/build-tools/35.0.0}
PLATFORM=${PLATFORM:-$ANDROID_HOME/platforms/android-34/android.jar}
OUT=${OUT:-$HOME/.cache/mulberry-android/build}
KEYDIR=${KEYDIR:-$HOME/.config/mulberry-android}
KEYSTORE=${KEYSTORE:-$KEYDIR/release.jks}
KEYPASS_FILE=${KEYPASS_FILE:-$KEYDIR/keystore.pass}
APK=dist/Mulberry-$VERSION-Android.apk
LIBS=${LIBS:-$HOME/.cache/mulberry-android/libs}
JAT=$LIBS/jaudiotagger-2.0.3.jar
JAT_SHA256=b558a96bb256f957bb471d1f33d8f10240282ce44050ff52d628e48661e396f7
for f in "$BT/aapt2" "$BT/d8" "$BT/zipalign" "$BT/apksigner" "$PLATFORM"; do
[ -e "$f" ] || { echo "missing $f (see the header of this script)" >&2; exit 1; }
done
if [ ! -f "$JAT" ]; then
echo "== fetching jaudiotagger"
mkdir -p "$LIBS"
curl -fsSL -o "$JAT.part" https://repo1.maven.org/maven2/org/jaudiotagger/2.0.3/jaudiotagger-2.0.3.jar
mv "$JAT.part" "$JAT"
fi
echo "$JAT_SHA256 $JAT" | sha256sum -c --quiet - || { echo "bad checksum: $JAT" >&2; exit 1; }
if [ ! -f "$KEYSTORE" ]; then
echo "!! no release keystore at $KEYSTORE: making a new one."
echo "!! APKs signed with it cannot update ones signed with another key. Back it up."
mkdir -p "$KEYDIR"; chmod 700 "$KEYDIR"
head -c 24 /dev/urandom | base64 | tr -d '/+=' > "$KEYPASS_FILE"; chmod 600 "$KEYPASS_FILE"
keytool -genkeypair -keystore "$KEYSTORE" -storetype PKCS12 -alias mulberry -keyalg RSA -keysize 4096 \
-validity 10000 -dname "CN=Mulberry, O=Mulberry" -storepass:file "$KEYPASS_FILE" >/dev/null
chmod 600 "$KEYSTORE"
fi
rm -rf "$OUT"
mkdir -p "$OUT/gen" "$OUT/classes" "$OUT/dex" dist
echo "== resources"
"$BT/aapt2" compile --dir res -o "$OUT/res.zip"
"$BT/aapt2" link -I "$PLATFORM" --manifest AndroidManifest.xml --java "$OUT/gen" \
--version-code "$VERSION_CODE" --version-name "$VERSION" \
-o "$OUT/unsigned.apk" "$OUT/res.zip"
echo "== java"
javac -nowarn -Xlint:none -encoding UTF-8 -source 17 -target 17 -cp "$PLATFORM:$JAT" -d "$OUT/classes" \
$(find src "$OUT/gen" -name '*.java') 2> "$OUT/javac.log" || { cat "$OUT/javac.log" >&2; echo "javac failed" >&2; exit 1; }
echo "== dex"
"$BT/d8" --release --min-api 24 --lib "$PLATFORM" --output "$OUT/dex" \
$(find "$OUT/classes" -name '*.class') "$JAT" 2> "$OUT/d8.log" || { cat "$OUT/d8.log" >&2; echo "d8 failed" >&2; exit 1; }
(cd "$OUT/dex" && zip -q "$OUT/unsigned.apk" classes*.dex)
echo "== sign"
"$BT/zipalign" -f -p 4 "$OUT/unsigned.apk" "$OUT/aligned.apk"
"$BT/apksigner" -J-enable-native-access=ALL-UNNAMED sign --ks "$KEYSTORE" --ks-pass "file:$KEYPASS_FILE" --out "$APK" "$OUT/aligned.apk"
"$BT/apksigner" -J-enable-native-access=ALL-UNNAMED verify "$APK"
rm -f "$APK.idsig"
echo "== $APK ($(du -h "$APK" | cut -f1))"