Files
elizaos--eliza/packages/app/scripts/android-adb-install.mjs
T
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

249 lines
8.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Command-line helper for the Android Adb Install app packaging, mobile, or
* Playwright automation lane.
*/
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { resolveApk } from "./lib/android-device.mjs";
import { assertAndroidApkRendererFresh } from "./lib/android-renderer-stamp.mjs";
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const appRoot = path.resolve(scriptDir, "..");
const repoRoot = path.resolve(appRoot, "..", "..");
function readFlag(name) {
const prefix = `${name}=`;
const value = process.argv.find((arg) => arg.startsWith(prefix));
if (value) return value.slice(prefix.length);
const index = process.argv.indexOf(name);
if (index !== -1) return process.argv[index + 1];
return null;
}
const args = new Set(process.argv.slice(2));
const serial = readFlag("--serial") ?? process.env.ANDROID_SERIAL ?? null;
const apkArg = readFlag("--apk");
const shouldBuild = args.has("--build");
const shouldLaunch = !args.has("--no-launch");
function run(command, commandArgs, options = {}) {
return spawnSync(command, commandArgs, {
cwd: options.cwd ?? appRoot,
encoding: "utf8",
shell: process.platform === "win32",
stdio: options.stdio ?? "pipe",
});
}
function fail(message, detail = "") {
console.error(`android-adb-install: ${message}`);
if (detail) console.error(detail.trim());
process.exit(1);
}
function commandExists(command) {
// `command -v` is a shell builtin — must run through a shell, or spawnSync
// looks for a binary literally named "command" and always fails on Linux.
const result = spawnSync(
process.platform === "win32" ? "where" : "command",
process.platform === "win32" ? [command] : ["-v", command],
{ encoding: "utf8", shell: true, stdio: "ignore" },
);
return result.status === 0;
}
/** Locate aapt2/aapt from the Android SDK build-tools (newest first), or PATH. */
function findAapt() {
for (const name of ["aapt2", "aapt"]) {
if (commandExists(name)) return name;
}
const sdk =
process.env.ANDROID_HOME ||
process.env.ANDROID_SDK_ROOT ||
path.join(process.env.HOME ?? "", "Android", "Sdk");
const buildTools = path.join(sdk, "build-tools");
if (!fs.existsSync(buildTools)) return null;
const versions = fs.readdirSync(buildTools).sort().reverse();
for (const v of versions) {
for (const name of ["aapt2", "aapt"]) {
const p = path.join(buildTools, v, name);
if (fs.existsSync(p)) return p;
}
}
return null;
}
function adbArgs(extra) {
return serial ? ["-s", serial, ...extra] : extra;
}
function readAppId() {
const src = fs.readFileSync(path.join(appRoot, "app.config.ts"), "utf8");
const appId = src.match(/appId:\s*["']([^"']+)["']/)?.[1];
if (!appId) fail("could not parse appId from packages/app/app.config.ts");
return appId;
}
/**
* The package name baked INTO the APK. We assert it matches the expected appId
* before installing — otherwise a wrong-brand build (for example, a custom app APK
* produced when the repoRoot resolved to the wrapper) silently installs a
* different package and never touches the one we think we're updating. Prefer
* aapt; fall back to scanning the (binary) AndroidManifest for the package id.
*/
function readApkPackage(apkPath) {
const aapt = findAapt();
if (aapt) {
const r = run(aapt, ["dump", "badging", apkPath]);
const m = r.stdout?.match(/package:\s+name='([^']+)'/);
if (m) return m[1];
}
// Fallback when aapt is absent: the package id lives in the binary
// AndroidManifest string pool — UTF-16LE on older aapt, UTF-8 on newer. Try
// both string encodings.
for (const enc of ["l", "s"]) {
const r = run("sh", [
"-c",
`unzip -p ${JSON.stringify(apkPath)} AndroidManifest.xml | strings -e ${enc} | grep -oE '[a-z][a-z0-9_]*(\\.[a-z0-9_]+)+\\.app' | head -1`,
]);
if (r.stdout?.trim()) return r.stdout.trim();
}
return null;
}
function sha256File(filePath) {
return createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
}
function currentHeadCommit() {
const result = run("git", ["rev-parse", "HEAD"], { cwd: repoRoot });
return result.status === 0 ? result.stdout.trim() || null : null;
}
if (!commandExists("adb")) {
fail(
"adb was not found",
"Install Android SDK platform-tools or set ANDROID_HOME/ANDROID_SDK_ROOT so adb is on PATH.",
);
}
if (shouldBuild) {
const build = run("bun", ["run", "build:android"], { stdio: "inherit" });
if (build.status !== 0) {
fail("Android build failed");
}
}
const devices = run("adb", ["devices"]);
if (devices.status !== 0) {
fail("adb devices failed", devices.stderr || devices.stdout);
}
const onlineDevices = devices.stdout
.split("\n")
.slice(1)
.map((line) => line.trim().split(/\s+/))
.filter((parts) => parts.length >= 2 && parts[1] === "device")
.map((parts) => parts[0]);
if (serial && !onlineDevices.includes(serial)) {
fail(
`ANDROID_SERIAL ${serial} is not online`,
`Online devices: ${onlineDevices.join(", ") || "none"}`,
);
}
if (!serial && onlineDevices.length !== 1) {
fail(
onlineDevices.length === 0
? "no online Android device found"
: "multiple Android devices found; pass --serial",
devices.stdout,
);
}
let apkPath;
try {
apkPath = resolveApk(apkArg);
} catch (error) {
fail("APK not found", error instanceof Error ? error.message : String(error));
}
const appId = readAppId();
// Guard against installing the wrong brand/package: the APK's own package id must
// match the appId we expect. Catches the custom-package-built-while-expecting-
// `ai.elizaos.app` trap, where `install -r` "succeeds" by writing a different
// package and the app we're testing is never updated.
const apkPackage = readApkPackage(apkPath);
if (apkPackage && apkPackage !== appId) {
fail(
`APK package mismatch — refusing to install`,
`app.config.ts expects "${appId}" but ${path.relative(process.cwd(), apkPath)} is package "${apkPackage}".\n` +
`You likely built the wrong brand (check repoRoot / ELIZA_ANDROID_USE_APP_DIR). Pass the matching --apk.`,
);
}
assertAndroidApkRendererFresh({
apkPath,
repoRoot,
expectedCommit: currentHeadCommit(),
label: path.relative(process.cwd(), apkPath),
log: (message) => console.log(`android-adb-install: ${message}`),
});
console.log(
`Installing ${path.relative(process.cwd(), apkPath)} (${apkPackage ?? appId}) to ${serial ?? onlineDevices[0]}`,
);
const install = run("adb", adbArgs(["install", "-r", apkPath]), {
stdio: "inherit",
});
if (install.status !== 0) {
fail("adb install failed");
}
const packageCheck = run("adb", adbArgs(["shell", "pm", "path", appId]));
if (packageCheck.status !== 0 || !packageCheck.stdout.includes(appId)) {
fail(`installed package ${appId} was not found`, packageCheck.stderr);
}
// Verify the bytes actually landed: the on-device base.apk must hash-match the
// APK we just installed. This is the definitive "the install is what we expect"
// check — a stale/cached/redirected install fails here instead of silently
// running old code.
const onDevicePath = packageCheck.stdout
.split("\n")
.map((line) => line.replace("package:", "").trim())
.find((line) => line.endsWith("base.apk"));
if (onDevicePath) {
const deviceHash = run("adb", adbArgs(["shell", "sha256sum", onDevicePath]))
.stdout?.trim()
.split(/\s+/)[0];
const localHash = sha256File(apkPath);
if (deviceHash && deviceHash !== localHash) {
fail(
`on-device APK does not match the installed file`,
`device sha256=${deviceHash}\nlocal sha256=${localHash}\nThe install did not replace the on-device APK (storage/permission/installer issue).`,
);
}
console.log(
`Verified on-device APK hash matches (${localHash.slice(0, 12)}…).`,
);
}
if (shouldLaunch) {
const launch = run("adb", adbArgs(["shell", "monkey", "-p", appId, "1"]), {
stdio: "inherit",
});
if (launch.status !== 0) {
fail("installed app, but launch failed");
}
}
console.log(`Android install verified for ${appId}.`);