mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-26 00:06:02 +00:00
feat(lab): build Lab builds on the device and report the APK commit to agents
This commit is contained in:
1 parent
f3f3f9ce08
commit
5d63cc3b9e
8 files changed
+252
-1
No files matched your search
@@ -284,6 +284,8 @@ class AgentBridgeProvider : ContentProvider() {
|
|||||||
return JSONObject()
|
return JSONObject()
|
||||||
.put("schema", SCHEMA)
|
.put("schema", SCHEMA)
|
||||||
.put("build", BuildConfig.BUILD_LABEL)
|
.put("build", BuildConfig.BUILD_LABEL)
|
||||||
|
.put("apkCommit", com.droiddeck.launcher.lab.LabOverlay.apkCommit)
|
||||||
|
.put("lab", com.droiddeck.launcher.lab.LabOverlay.active?.label ?: JSONObject.NULL)
|
||||||
.put("appVersion", context.packageManager.getPackageInfo(context.packageName, 0).versionName)
|
.put("appVersion", context.packageManager.getPackageInfo(context.packageName, 0).versionName)
|
||||||
.put("runtime", JSONObject()
|
.put("runtime", JSONObject()
|
||||||
.put("installed", LinuxRuntime.isInstalled(context))
|
.put("installed", LinuxRuntime.isInstalled(context))
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ object LabOverlay {
|
|||||||
private const val MAX_FAILED_STARTS = 2
|
private const val MAX_FAILED_STARTS = 2
|
||||||
private const val STABLE_AFTER_MS = 10_000L
|
private const val STABLE_AFTER_MS = 10_000L
|
||||||
|
|
||||||
|
/** The installed APK's commit; this class always comes from the APK, never from a Lab build. */
|
||||||
|
val apkCommit: String = BuildConfig.SOURCE_COMMIT
|
||||||
|
|
||||||
@Volatile var active: Info? = null
|
@Volatile var active: Info? = null
|
||||||
private set
|
private set
|
||||||
@Volatile var skipped: String? = null
|
@Volatile var skipped: String? = null
|
||||||
@@ -60,7 +63,7 @@ object LabOverlay {
|
|||||||
if (!apk.isFile || !meta.isFile) return
|
if (!apk.isFile || !meta.isFile) return
|
||||||
val info = parse(meta.readText()) ?: return refuse(root, mainProcess, "unreadable overlay.json")
|
val info = parse(meta.readText()) ?: return refuse(root, mainProcess, "unreadable overlay.json")
|
||||||
// The APK's path changes with every install and update of the app.
|
// The APK's path changes with every install and update of the app.
|
||||||
if (info.baseCommit != BuildConfig.SOURCE_COMMIT || info.baseApk != app.applicationInfo.sourceDir)
|
if (info.baseCommit != apkCommit || info.baseApk != app.applicationInfo.sourceDir)
|
||||||
return refuse(root, mainProcess, "built for another install")
|
return refuse(root, mainProcess, "built for another install")
|
||||||
if (Build.VERSION.SDK_INT < 30) return skip("needs Android 11")
|
if (Build.VERSION.SDK_INT < 30) return skip("needs Android 11")
|
||||||
// Classes in an app image are loaded before attachBaseContext and would ignore the overlay;
|
// Classes in an app image are loaded before attachBaseContext and would ignore the overlay;
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# Lab builds
|
||||||
|
|
||||||
|
A Lab build is DroidDeck app code and resources built on the device itself, by the on-device agent
|
||||||
|
or by hand, and loaded over the installed APK at the next start. Nothing is reinstalled or
|
||||||
|
re-signed, so a tester on a release build keeps their games, sign-ins and settings while trying a
|
||||||
|
change.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
- `tools/agent/dd-lab-build` builds the checkout with Gradle on the device (`processReleaseResources`
|
||||||
|
and `mergeDexRelease`), packs the linked resources and dex files into `overlay.apk`, writes
|
||||||
|
`overlay.json`, and stages both in the app's `files/lab/next`. It then recompiles the app without
|
||||||
|
an app image and restarts DroidDeck.
|
||||||
|
- At start, `LabOverlay` (called from `App.attachBaseContext`) promotes `next` to `active`, checks
|
||||||
|
that the build was made for this install, puts its dex files ahead of the APK's, and adds its
|
||||||
|
resources through `ResourcesLoader` to the app and every activity. Contexts made from them
|
||||||
|
(presentations on the second screen) inherit the loader.
|
||||||
|
- The watermark shows `LAB | <branch> @ <commit>`, and Updates has a Lab build card that goes back
|
||||||
|
to the installed version.
|
||||||
|
|
||||||
|
## What a Lab build cannot change
|
||||||
|
|
||||||
|
The manifest, native code (`cpp`, `jniLibs`, Rust), Gradle files, `App`, and `LabOverlay` itself:
|
||||||
|
they are loaded before a Lab build can be, or are not part of it. `dd-lab-build` refuses those
|
||||||
|
changes; they need a new APK.
|
||||||
|
|
||||||
|
## Safety
|
||||||
|
|
||||||
|
- A build is only used on the install it was built for: `overlay.json` carries the APK's commit
|
||||||
|
(`apkCommit` in `droiddeckctl state`) and the APK's path, which changes with every install and
|
||||||
|
update. A build for another install is turned off.
|
||||||
|
- Two starts in a row that do not reach a resumed activity for 10 seconds turn the build off.
|
||||||
|
- It needs Android 11 or later (`ResourcesLoader`) and no app image (classes in one load before the
|
||||||
|
Lab build). `dd-lab-build` runs `cmd package compile -m verify` before restarting.
|
||||||
|
|
||||||
|
## Setting up a device
|
||||||
|
|
||||||
|
From the DroidDeck terminal, in a checkout of this repository:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
tools/agent/setup
|
||||||
|
```
|
||||||
|
|
||||||
|
It installs Claude Code, git, gh and loopback adb, plus the build toolchain: JDK 17, the Android
|
||||||
|
SDK platform, and Google's x86-64 `aapt2` run under `qemu-user`. The first Lab build compiles the
|
||||||
|
whole app and takes about 20 minutes on a Snapdragon 865; later ones are incremental.
|
||||||
|
`~/.cache/dd-lab-build.json` reports the stage, task count and current task while it runs.
|
||||||
Executable
+9
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Opens a URL in Android's browser on the bottom screen.
|
||||||
|
url=$1
|
||||||
|
[ -n "$url" ] || exit 1
|
||||||
|
serial=${ADB_SERIAL:-127.0.0.1:5555}
|
||||||
|
adb connect "$serial" >/dev/null 2>&1
|
||||||
|
quoted=$(printf "%s" "$url" | sed "s/'/'\\''/g")
|
||||||
|
adb -s "$serial" shell "am start --display 2 -a android.intent.action.VIEW -d '$quoted'" >/dev/null 2>&1 ||
|
||||||
|
echo "Open this on any device: $url" >&2
|
||||||
Executable
+4
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Claude Code in the DroidDeck checkout, with sign-in links opened in Android's browser.
|
||||||
|
export ADB_SERIAL=${ADB_SERIAL:-127.0.0.1:5555} PATH="$HOME/.local/bin:$PATH" BROWSER="$HOME/.local/bin/android-open"
|
||||||
|
cd "$HOME/DroidDeck" && exec claude "$@"
|
||||||
Executable
+23
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# GitHub sign-in through Android's browser on the bottom screen, with the one-time code typed in.
|
||||||
|
serial=${ADB_SERIAL:-127.0.0.1:5555}
|
||||||
|
adb connect "$serial" >/dev/null 2>&1
|
||||||
|
coproc GH { BROWSER=true gh auth login --hostname github.com --git-protocol https --web 2>&1; }
|
||||||
|
typed=
|
||||||
|
while IFS= read -r line <&"${GH[0]}"; do
|
||||||
|
printf '%s\n' "$line"
|
||||||
|
code=$(grep -oE '[A-Z0-9]{4}-[A-Z0-9]{4}' <<<"$line" | head -1)
|
||||||
|
if [ -n "$code" ] && [ -z "$typed" ]; then
|
||||||
|
typed=1
|
||||||
|
echo >&"${GH[1]}"
|
||||||
|
adb -s "$serial" shell "am start --display 2 -a android.intent.action.VIEW -d https://github.com/login/device" >/dev/null 2>&1
|
||||||
|
# Chrome drops characters from `input text` and from fast key events on this page.
|
||||||
|
keys=
|
||||||
|
for ((i = 0; i < ${#code}; i++)); do
|
||||||
|
c=${code:i:1}
|
||||||
|
[ "$c" = - ] || keys+="input -d 2 keyevent KEYCODE_$c; sleep 0.4; "
|
||||||
|
done
|
||||||
|
(sleep 7; adb -s "$serial" shell "$keys" >/dev/null 2>&1) &
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
wait "$GH_PID"
|
||||||
Executable
+104
@@ -0,0 +1,104 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Builds this checkout as a Lab build of the installed DroidDeck and restarts the app on it.
|
||||||
|
# Usage: dd-lab-build [--no-restart]
|
||||||
|
set -euo pipefail
|
||||||
|
pkg=com.droiddeck.launcher
|
||||||
|
serial=${ADB_SERIAL:-127.0.0.1:5555}
|
||||||
|
restart=1
|
||||||
|
[ "${1:-}" = --no-restart ] && restart=0
|
||||||
|
started=$(date +%s)
|
||||||
|
progress_file=$HOME/.cache/dd-lab-build.json
|
||||||
|
mkdir -p "$HOME/.cache"
|
||||||
|
# The Agent tab and the agent read this file to show how far the build has got.
|
||||||
|
progress() { # stage [done total task]
|
||||||
|
printf '{"stage":"%s","done":%d,"total":%d,"task":"%s","elapsed":%d}\n' \
|
||||||
|
"$1" "${2:-0}" "${3:-0}" "${4:-}" $(( $(date +%s) - started )) > "$progress_file.part"
|
||||||
|
mv "$progress_file.part" "$progress_file"
|
||||||
|
}
|
||||||
|
fail() { progress failed; echo "dd-lab-build: $*" >&2; exit 1; }
|
||||||
|
device() { adb -s "$serial" shell "$@" | tr -d '\r'; }
|
||||||
|
|
||||||
|
progress checking
|
||||||
|
repo=$(git rev-parse --show-toplevel) || fail "run it inside the DroidDeck checkout"
|
||||||
|
cd "$repo"
|
||||||
|
adb connect "$serial" >/dev/null 2>&1 || true
|
||||||
|
git diff --quiet && git diff --cached --quiet || fail "commit your changes first"
|
||||||
|
|
||||||
|
state=$(python3 tools/droiddeckctl state 2>/dev/null | sed -n '/^{/,$p')
|
||||||
|
# A Lab build reports its own label, so the APK's commit comes from apkCommit when the app has it.
|
||||||
|
label=$(python3 -c 'import json,sys; s=json.load(sys.stdin); print(s.get("apkCommit") or s["build"].rsplit(" at ", 1)[-1])' <<<"$state")
|
||||||
|
base=$(git rev-parse --verify --quiet "$label^{commit}") || fail "the installed build ($label) is not in this checkout; fetch it first"
|
||||||
|
git merge-base --is-ancestor "$base" HEAD || fail "HEAD does not build on the installed commit ${base:0:9}; branch from it"
|
||||||
|
blocked=$(git diff --name-only "$base" HEAD | grep -E 'AndroidManifest\.xml$|/cpp/|/jniLibs/|\.rs$|(^|/)build\.gradle$|/App\.kt$|/lab/LabOverlay\.kt$' || true)
|
||||||
|
[ -z "$blocked" ] || fail "these need a new APK, not a Lab build: $(echo $blocked)"
|
||||||
|
apk=$(device pm path $pkg | sed -n 's/^package://p' | grep 'base\.apk$' | head -1)
|
||||||
|
[ -n "$apk" ] || fail "DroidDeck is not installed"
|
||||||
|
|
||||||
|
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64 ANDROID_HOME=$HOME/android-sdk
|
||||||
|
printf 'sdk.dir=%s\n' "$ANDROID_HOME" > local.properties
|
||||||
|
if [ ! -d "$ANDROID_HOME/platforms/android-34" ]; then
|
||||||
|
progress sdk
|
||||||
|
yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null
|
||||||
|
"$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Google publishes aapt2 for x86-64 Linux only; setup wraps it in qemu-user at ~/aapt2/aapt2.
|
||||||
|
gradle=(./gradlew :app:processReleaseResources :app:mergeDexRelease --max-workers=2 -PskipRust=true -x buildVkBridgeServer
|
||||||
|
-Pandroid.aapt2FromMavenOverride=$HOME/aapt2/aapt2 -Pkotlin.compiler.execution.strategy=in-process
|
||||||
|
-Dorg.gradle.jvmargs=-Xmx1800m --console=plain)
|
||||||
|
progress planning
|
||||||
|
total=$("${gradle[@]}" --dry-run -q 2>/dev/null | grep -c '^:' || true)
|
||||||
|
current=$(mktemp)
|
||||||
|
echo "0 starting" > "$current"
|
||||||
|
(
|
||||||
|
while sleep 60; do
|
||||||
|
read -r n task < "$current"
|
||||||
|
echo "[build $n/$total] still on $task, $(( ($(date +%s) - started) / 60 ))m in"
|
||||||
|
progress building "$n" "$total" "$task"
|
||||||
|
done
|
||||||
|
) &
|
||||||
|
heartbeat=$!
|
||||||
|
trap 'kill $heartbeat 2>/dev/null || true' EXIT
|
||||||
|
set +o pipefail
|
||||||
|
"${gradle[@]}" 2>&1 | {
|
||||||
|
n=0
|
||||||
|
while IFS= read -r line; do
|
||||||
|
printf '%s\n' "$line"
|
||||||
|
case $line in
|
||||||
|
"> Task "*)
|
||||||
|
n=$((n + 1)); task=${line#> Task }; task=${task%% *}
|
||||||
|
echo "$n $task" > "$current"
|
||||||
|
echo "[build $n/$total] $task"
|
||||||
|
progress building "$n" "$total" "$task" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
status=${PIPESTATUS[0]}
|
||||||
|
set -o pipefail
|
||||||
|
kill $heartbeat 2>/dev/null || true
|
||||||
|
[ "$status" = 0 ] || fail "Gradle failed (exit $status); the reason is above"
|
||||||
|
|
||||||
|
progress packaging
|
||||||
|
linked=$(ls app/build/intermediates/linked_resources_binary_format/release/processReleaseResources/*.ap_ | head -1)
|
||||||
|
work=$(mktemp -d)
|
||||||
|
cp "$linked" "$work/overlay.apk"
|
||||||
|
(cd app/build/intermediates/dex/release/mergeDexRelease && zip -q "$work/overlay.apk" classes*.dex)
|
||||||
|
name="$(git branch --show-current) @ $(git rev-parse --short HEAD)"
|
||||||
|
python3 -c 'import json,sys; print(json.dumps({"label": sys.argv[1], "baseCommit": sys.argv[2], "baseApk": sys.argv[3]}))' \
|
||||||
|
"$name" "$base" "$apk" > "$work/overlay.json"
|
||||||
|
|
||||||
|
lab=/data/user/0/$pkg/files/lab
|
||||||
|
mkdir -p "$lab"
|
||||||
|
rm -rf "$lab/next.part" "$lab/next"
|
||||||
|
mv "$work" "$lab/next.part"
|
||||||
|
mv "$lab/next.part" "$lab/next"
|
||||||
|
progress ready
|
||||||
|
echo "Lab build ready: $name ($(du -h "$lab/next/overlay.apk" | cut -f1)) in $(( ($(date +%s) - started) / 60 ))m"
|
||||||
|
[ $restart = 1 ] || exit 0
|
||||||
|
|
||||||
|
# Classes in an app image load before the Lab build can, so the app is recompiled without one.
|
||||||
|
device cmd package compile -m verify -f $pkg >/dev/null
|
||||||
|
progress restarting
|
||||||
|
echo "Restarting DroidDeck on the Lab build"
|
||||||
|
device "nohup sh -c 'sleep 3; monkey -p $pkg -c android.intent.category.LAUNCHER 1' >/dev/null 2>&1 &"
|
||||||
|
for pid in $(device pidof $pkg); do kill "$pid" 2>/dev/null || true; done
|
||||||
Executable
+59
@@ -0,0 +1,59 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Sets up the on-device agent inside DroidDeck's Linux userland: Claude Code, git and gh, loopback
|
||||||
|
# adb for droiddeckctl, and the toolchain dd-lab-build uses to build Lab builds on the device.
|
||||||
|
# Run it from the DroidDeck terminal, inside a checkout of the DroidDeck repository.
|
||||||
|
set -euo pipefail
|
||||||
|
here=$(cd "$(dirname "$0")" && pwd)
|
||||||
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
apt-get install -y --no-install-recommends git gh adb python3 zip unzip curl openjdk-17-jdk-headless qemu-user zipalign apksigner
|
||||||
|
|
||||||
|
command -v claude >/dev/null || [ -x "$HOME/.local/bin/claude" ] || curl -fsSL https://claude.ai/install.sh | bash
|
||||||
|
|
||||||
|
mkdir -p "$HOME/.local/bin"
|
||||||
|
for tool in dd-agent dd-lab-build dd-gh-login android-open; do install -m755 "$here/$tool" "$HOME/.local/bin/$tool"; done
|
||||||
|
grep -q 'droiddeck-agent-path' "$HOME/.bashrc" 2>/dev/null ||
|
||||||
|
printf '\n# droiddeck-agent-path\nexport PATH="$HOME/.local/bin:$PATH" ADB_SERIAL=127.0.0.1:5555 BROWSER="$HOME/.local/bin/android-open"\n' >> "$HOME/.bashrc"
|
||||||
|
|
||||||
|
# Google publishes aapt2 for x86-64 Linux only; it runs here under qemu-user with Ubuntu's x86-64 runtime.
|
||||||
|
agp=$(sed -n "s/.*com.android.tools.build:gradle:\([0-9.]*\).*/\1/p" build.gradle | head -1)
|
||||||
|
aapt2=$(curl -fsSL "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/maven-metadata.xml" |
|
||||||
|
grep -o "<version>$agp-[0-9]*</version>" | tail -1 | sed 's/<[^>]*>//g')
|
||||||
|
x86=$HOME/x86root
|
||||||
|
mkdir -p "$HOME/aapt2/x86" "$x86/etc"
|
||||||
|
if [ ! -x "$HOME/aapt2/x86/aapt2" ]; then
|
||||||
|
curl -fsSL -o /tmp/aapt2.jar "https://dl.google.com/android/maven2/com/android/tools/build/aapt2/$aapt2/aapt2-$aapt2-linux.jar"
|
||||||
|
unzip -o -q /tmp/aapt2.jar aapt2 -d "$HOME/aapt2/x86"
|
||||||
|
chmod +x "$HOME/aapt2/x86/aapt2"
|
||||||
|
fi
|
||||||
|
if [ ! -e "$x86/lib64/ld-linux-x86-64.so.2" ]; then
|
||||||
|
codename=$(. /etc/os-release && echo "$VERSION_CODENAME")
|
||||||
|
curl -fsSL "http://archive.ubuntu.com/ubuntu/dists/$codename/main/binary-amd64/Packages.gz" | gunzip > /tmp/amd64-packages
|
||||||
|
for package in libc6 libgcc-s1 libstdc++6 zlib1g; do
|
||||||
|
file=$(awk -v p="$package" '$0 == "Package: " p {found = 1} found && /^Filename:/ {print $2; exit}' /tmp/amd64-packages)
|
||||||
|
curl -fsSL -o /tmp/$package.deb "http://archive.ubuntu.com/ubuntu/$file"
|
||||||
|
dpkg-deb -x /tmp/$package.deb "$x86"
|
||||||
|
done
|
||||||
|
ln -sfn usr/lib64 "$x86/lib64"
|
||||||
|
ln -sfn usr/lib "$x86/lib"
|
||||||
|
: > "$x86/etc/ld.so.preload"
|
||||||
|
fi
|
||||||
|
# AGP only accepts a custom aapt2 whose file is named aapt2.
|
||||||
|
printf '#!/bin/sh\nexec qemu-x86_64 -L %s %s/aapt2/x86/aapt2 "$@"\n' "$x86" "$HOME" > "$HOME/aapt2/aapt2"
|
||||||
|
chmod +x "$HOME/aapt2/aapt2"
|
||||||
|
"$HOME/aapt2/aapt2" version
|
||||||
|
|
||||||
|
sdk=$HOME/android-sdk
|
||||||
|
if [ ! -x "$sdk/cmdline-tools/latest/bin/sdkmanager" ]; then
|
||||||
|
mkdir -p "$sdk/cmdline-tools"
|
||||||
|
curl -fsSL -o /tmp/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-13114758_latest.zip
|
||||||
|
unzip -q -o /tmp/cmdline-tools.zip -d "$sdk/cmdline-tools"
|
||||||
|
rm -rf "$sdk/cmdline-tools/latest"
|
||||||
|
mv "$sdk/cmdline-tools/cmdline-tools" "$sdk/cmdline-tools/latest"
|
||||||
|
fi
|
||||||
|
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-arm64
|
||||||
|
yes | "$sdk/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null
|
||||||
|
"$sdk/cmdline-tools/latest/bin/sdkmanager" "platforms;android-34" "build-tools;35.0.0" >/dev/null
|
||||||
|
|
||||||
|
adb connect 127.0.0.1:5555 || true
|
||||||
|
echo "Done. Approve the USB debugging prompt if one appears, then run dd-agent."
|
||||||
Reference in new issue
Block a user