From 2295200a01ba498931e56f08819bbf76f6da5e69 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 04:09:44 +0000 Subject: [PATCH 1/2] ci: install the Android SDK through one setup-android-sdk action Six copies of the sdkmanager install handled the `yes` SIGPIPE three ways (one hid real licence failures with `|| true`) and the build-tools version was written nine times. The new composite action owns the version (input `build-tools`), exports AGENT_DEVICE_ANDROID_BUILD_TOOLS, and outputs `build-tools-dir`. The fixture APK scripts read the exported version; a local repack outside CI takes the newest installed build tools. A repo guard keeps the version and sdkmanager inside the action. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4 --- .../setup-android-replay-host/action.yml | 34 +++---------- .github/actions/setup-android-sdk/action.yml | 48 +++++++++++++++++++ .github/actions/setup-fixture-app/action.yml | 23 +-------- .../setup-fixture-app/locate-android-apk.sh | 2 +- .../setup-fixture-app/repack-android-apk.sh | 14 +++++- .../release-android-snapshot-helper.yml | 19 ++------ .github/workflows/release.yml | 11 ++--- .github/workflows/size.yml | 19 ++------ .github/workflows/test-app-build-cache.yml | 18 +------ CONTRIBUTING.md | 3 +- test/ci/trusted-fixture-artifact.test.mjs | 32 ++++++++++--- .../android-sdk-setup-action.test.ts | 41 ++++++++++++++++ 12 files changed, 149 insertions(+), 115 deletions(-) create mode 100644 .github/actions/setup-android-sdk/action.yml create mode 100644 test/repo-guards/android-sdk-setup-action.test.ts diff --git a/.github/actions/setup-android-replay-host/action.yml b/.github/actions/setup-android-replay-host/action.yml index aea6807de4..3db790a624 100644 --- a/.github/actions/setup-android-replay-host/action.yml +++ b/.github/actions/setup-android-replay-host/action.yml @@ -36,19 +36,15 @@ runs: run: | VERSION="$(node -p 'require("./package.json").version')" echo "version=$VERSION" >> "$GITHUB_OUTPUT" - echo "value=${{ hashFiles('android/snapshot-helper/AndroidManifest.xml', 'android/snapshot-helper/debug.keystore', 'android/snapshot-helper/src/**', 'android/ime-helper/AndroidManifest.xml', 'android/ime-helper/res/**', 'android/ime-helper/src/**', 'scripts/build-android-helper.sh', 'scripts/package-android-helper.sh', '.github/actions/setup-android-replay-host/action.yml') }}" >> "$GITHUB_OUTPUT" + echo "value=${{ hashFiles('android/snapshot-helper/AndroidManifest.xml', 'android/snapshot-helper/debug.keystore', 'android/snapshot-helper/src/**', 'android/ime-helper/AndroidManifest.xml', 'android/ime-helper/res/**', 'android/ime-helper/src/**', 'scripts/build-android-helper.sh', 'scripts/package-android-helper.sh', '.github/actions/setup-android-replay-host/action.yml', '.github/actions/setup-android-sdk/action.yml') }}" >> "$GITHUB_OUTPUT" shell: bash # The helper build refuses to guess its build-tools version on CI, and that version decides - # the APK bytes, so both cache keys name it as well. - - name: Declare the pinned Android build tools + # the APK bytes, so both cache keys name it as well. The install is skipped when the runner + # already has the pinned packages; the fixture APK steps need them on a cache hit too. + - name: Setup Android SDK if: inputs.package-helpers == 'true' - id: android-build-tools - run: | - BUILD_TOOLS_VERSION=36.0.0 - echo "AGENT_DEVICE_ANDROID_BUILD_TOOLS=$BUILD_TOOLS_VERSION" >> "$GITHUB_ENV" - echo "version=$BUILD_TOOLS_VERSION" >> "$GITHUB_OUTPUT" - shell: bash + uses: ./.github/actions/setup-android-sdk - name: Restore packaged Android helpers if: inputs.package-helpers == 'true' @@ -58,23 +54,7 @@ runs: path: | android/snapshot-helper/dist android/ime-helper/dist - key: android-helpers-${{ runner.os }}-${{ runner.arch }}-api36-bt${{ steps.android-build-tools.outputs.version }}-v${{ steps.android-helper-source.outputs.version }}-${{ steps.android-helper-source.outputs.value }} - - - name: Install Android helper SDK packages - if: inputs.package-helpers == 'true' && steps.android-helpers-cache.outputs.cache-hit != 'true' - run: | - SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" - SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" - if [ ! -x "$SDKMANAGER" ]; then - SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" - fi - if [ ! -x "$SDKMANAGER" ]; then - echo "sdkmanager not found under $SDK_ROOT" >&2 - exit 1 - fi - yes | "$SDKMANAGER" --licenses >/dev/null || true - "$SDKMANAGER" "platforms;android-36" "build-tools;$AGENT_DEVICE_ANDROID_BUILD_TOOLS" - shell: bash + key: android-helpers-${{ runner.os }}-${{ runner.arch }}-api36-bt${{ env.AGENT_DEVICE_ANDROID_BUILD_TOOLS }}-v${{ steps.android-helper-source.outputs.version }}-${{ steps.android-helper-source.outputs.value }} - name: Package npm-bundled Android helpers if: inputs.package-helpers == 'true' && steps.android-helpers-cache.outputs.cache-hit != 'true' @@ -97,4 +77,4 @@ runs: path: | android/snapshot-helper/dist android/ime-helper/dist - key: android-helpers-${{ runner.os }}-${{ runner.arch }}-api36-bt${{ steps.android-build-tools.outputs.version }}-v${{ steps.android-helper-source.outputs.version }}-${{ steps.android-helper-source.outputs.value }} + key: android-helpers-${{ runner.os }}-${{ runner.arch }}-api36-bt${{ env.AGENT_DEVICE_ANDROID_BUILD_TOOLS }}-v${{ steps.android-helper-source.outputs.version }}-${{ steps.android-helper-source.outputs.value }} diff --git a/.github/actions/setup-android-sdk/action.yml b/.github/actions/setup-android-sdk/action.yml new file mode 100644 index 0000000000..b192aa8365 --- /dev/null +++ b/.github/actions/setup-android-sdk/action.yml @@ -0,0 +1,48 @@ +name: 'Setup Android SDK' +description: 'Install the pinned Android platform and build tools, and export the build-tools version' + +# The one owner of the pinned build-tools version. d8 and aapt2 come from build-tools, so the +# version decides the helper APK bytes. Later steps read it from AGENT_DEVICE_ANDROID_BUILD_TOOLS. + +inputs: + build-tools: + description: 'Android build-tools version to install' + required: false + default: '36.0.0' + +outputs: + build-tools-dir: + description: 'Absolute path of the installed build-tools directory' + value: ${{ steps.install.outputs.build-tools-dir }} + +runs: + using: 'composite' + steps: + - name: Install Android platform and build tools + id: install + shell: bash + env: + BUILD_TOOLS_VERSION: ${{ inputs.build-tools }} + run: | + set -euo pipefail + SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" + BUILD_TOOLS_DIR="$SDK_ROOT/build-tools/$BUILD_TOOLS_VERSION" + if [ ! -x "$BUILD_TOOLS_DIR/aapt" ] || [ ! -x "$BUILD_TOOLS_DIR/apksigner" ] || + [ ! -f "$SDK_ROOT/platforms/android-36/android.jar" ]; then + SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" + if [ ! -x "$SDKMANAGER" ]; then + SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" + fi + if [ ! -x "$SDKMANAGER" ]; then + echo "::error::sdkmanager not found under $SDK_ROOT." >&2 + exit 1 + fi + # `yes` dies of SIGPIPE when sdkmanager stops reading. Without pipefail the pipe + # reports sdkmanager's own status, so a real licence failure still stops the step. + set +o pipefail + yes | "$SDKMANAGER" --licenses >/dev/null + set -o pipefail + "$SDKMANAGER" "platforms;android-36" "build-tools;$BUILD_TOOLS_VERSION" + fi + echo "AGENT_DEVICE_ANDROID_BUILD_TOOLS=$BUILD_TOOLS_VERSION" >> "$GITHUB_ENV" + echo "build-tools-dir=$BUILD_TOOLS_DIR" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/setup-fixture-app/action.yml b/.github/actions/setup-fixture-app/action.yml index 10e7e4376a..adcfde459f 100644 --- a/.github/actions/setup-fixture-app/action.yml +++ b/.github/actions/setup-fixture-app/action.yml @@ -66,28 +66,7 @@ runs: # must not make artifact inspection/repacking depend on the runner image. - name: Ensure Android artifact build tools if: inputs.platform == 'android' - shell: bash - run: | - # sdkmanager intentionally closes stdin after accepting licenses; disable - # the runner's inherited pipefail only around that benign SIGPIPE. - set -eu - SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" - BUILD_TOOLS="$SDK_ROOT/build-tools/36.0.0" - if [ -x "$BUILD_TOOLS/aapt" ] && [ -x "$BUILD_TOOLS/apksigner" ]; then - exit 0 - fi - SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" - if [ ! -x "$SDKMANAGER" ]; then - SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" - fi - if [ ! -x "$SDKMANAGER" ]; then - echo "::error::sdkmanager not found under $SDK_ROOT." >&2 - exit 1 - fi - set +o pipefail - yes | "$SDKMANAGER" --licenses >/dev/null - set -o pipefail - "$SDKMANAGER" "platforms;android-36" "build-tools;36.0.0" + uses: ./.github/actions/setup-android-sdk - name: Fetch the cached Release binary id: fetch diff --git a/.github/actions/setup-fixture-app/locate-android-apk.sh b/.github/actions/setup-fixture-app/locate-android-apk.sh index 1b25711e89..3e979992a8 100644 --- a/.github/actions/setup-fixture-app/locate-android-apk.sh +++ b/.github/actions/setup-fixture-app/locate-android-apk.sh @@ -3,7 +3,7 @@ set -euo pipefail DEST="${1:?fixture directory is required}" SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" -BUILD_TOOLS="$SDK_ROOT/build-tools/36.0.0" +BUILD_TOOLS="$SDK_ROOT/build-tools/${AGENT_DEVICE_ANDROID_BUILD_TOOLS:?AGENT_DEVICE_ANDROID_BUILD_TOOLS must name the build-tools version setup-android-sdk installed}" APKS=() while IFS= read -r apk; do diff --git a/.github/actions/setup-fixture-app/repack-android-apk.sh b/.github/actions/setup-fixture-app/repack-android-apk.sh index 9628a55283..3394983805 100644 --- a/.github/actions/setup-fixture-app/repack-android-apk.sh +++ b/.github/actions/setup-fixture-app/repack-android-apk.sh @@ -4,7 +4,19 @@ set -euo pipefail APK="${1:?source APK is required}" OUT="${2:?output APK is required}" SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" -BUILD_TOOLS="$SDK_ROOT/build-tools/36.0.0" +# CI names the version setup-android-sdk installed. A local repack may take the newest installed. +BUILD_TOOLS_VERSION="${AGENT_DEVICE_ANDROID_BUILD_TOOLS:-}" +if [ -z "$BUILD_TOOLS_VERSION" ]; then + if [ "${CI:-}" = "true" ]; then + echo "::error::AGENT_DEVICE_ANDROID_BUILD_TOOLS must name the build-tools version setup-android-sdk installed." >&2 + exit 1 + fi + NEWEST_BUILD_TOOLS_DIR="$( + find "$SDK_ROOT/build-tools" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | sort -V | tail -n 1 + )" + BUILD_TOOLS_VERSION="${NEWEST_BUILD_TOOLS_DIR##*/}" +fi +BUILD_TOOLS="$SDK_ROOT/build-tools/$BUILD_TOOLS_VERSION" if [ ! -x "$BUILD_TOOLS/aapt" ] || [ ! -x "$BUILD_TOOLS/apksigner" ]; then echo "::error::Android build tools were not found at $BUILD_TOOLS." >&2 exit 1 diff --git a/.github/workflows/release-android-snapshot-helper.yml b/.github/workflows/release-android-snapshot-helper.yml index 3efb6fd347..ea0998fa07 100644 --- a/.github/workflows/release-android-snapshot-helper.yml +++ b/.github/workflows/release-android-snapshot-helper.yml @@ -27,9 +27,6 @@ jobs: name: Publish Android Snapshot Helper runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 - env: - # The helper build refuses to guess its build-tools version on CI. - AGENT_DEVICE_ANDROID_BUILD_TOOLS: 36.0.0 steps: - name: Checkout uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 @@ -41,19 +38,9 @@ jobs: with: node-version: '24.13' - - name: Install Android SDK packages - run: | - SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" - SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" - if [ ! -x "$SDKMANAGER" ]; then - SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" - fi - if [ ! -x "$SDKMANAGER" ]; then - echo "sdkmanager not found under $SDK_ROOT" >&2 - exit 1 - fi - yes | "$SDKMANAGER" --licenses >/dev/null - "$SDKMANAGER" "platforms;android-36" "build-tools;$AGENT_DEVICE_ANDROID_BUILD_TOOLS" + # Exports AGENT_DEVICE_ANDROID_BUILD_TOOLS, which the helper build requires on CI. + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk - name: Check Java toolchain run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dc01e82d23..510895096d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -74,8 +74,6 @@ jobs: permissions: contents: read env: - # The helper build refuses to guess its build-tools version on CI. - AGENT_DEVICE_ANDROID_BUILD_TOOLS: 36.0.0 VERSION: ${{ needs.plan.outputs.version }} steps: - name: Checkout @@ -89,12 +87,9 @@ jobs: with: cache-store: 'false' - - name: Install Android helper toolchain - run: | - set -euo pipefail - SDKMANAGER="$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" - yes | "$SDKMANAGER" --licenses >/dev/null || true - "$SDKMANAGER" "platforms;android-36" "build-tools;$AGENT_DEVICE_ANDROID_BUILD_TOOLS" + # Exports AGENT_DEVICE_ANDROID_BUILD_TOOLS, which the helper build requires on CI. + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk - name: Stamp release version run: node scripts/release-version.mjs stamp "$VERSION" diff --git a/.github/workflows/size.yml b/.github/workflows/size.yml index be7c51abab..6a90047017 100644 --- a/.github/workflows/size.yml +++ b/.github/workflows/size.yml @@ -26,9 +26,6 @@ jobs: if: github.event.pull_request.head.repo.full_name == github.repository runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 10 - env: - # The helper build refuses to guess its build-tools version on CI. - AGENT_DEVICE_ANDROID_BUILD_TOOLS: 36.0.0 steps: - name: Checkout uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 @@ -44,19 +41,9 @@ jobs: cp scripts/size-report*.mjs /tmp/agent-device-size-report/ cp scripts/prepare-publish-assets.mjs /tmp/agent-device-size-report/ - - name: Install Android helper toolchain - run: | - SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" - SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" - if [ ! -x "$SDKMANAGER" ]; then - SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" - fi - if [ ! -x "$SDKMANAGER" ]; then - echo "sdkmanager not found under $SDK_ROOT" >&2 - exit 1 - fi - yes | "$SDKMANAGER" --licenses >/dev/null || true - "$SDKMANAGER" "platforms;android-36" "build-tools;$AGENT_DEVICE_ANDROID_BUILD_TOOLS" + # Exports AGENT_DEVICE_ANDROID_BUILD_TOOLS, which the helper build requires on CI. + - name: Setup Android SDK + uses: ./.github/actions/setup-android-sdk # dist is fully determined by the base commit, so reuse it across PR runs # against the same base. Startup medians are still measured fresh on this diff --git a/.github/workflows/test-app-build-cache.yml b/.github/workflows/test-app-build-cache.yml index 11c15b1ad2..500c6a7bbe 100644 --- a/.github/workflows/test-app-build-cache.yml +++ b/.github/workflows/test-app-build-cache.yml @@ -162,23 +162,9 @@ jobs: --no-bundler \ --output "${{ github.workspace }}/.tmp/test-app-build" - # Deliberately no `set -o pipefail`: `yes` is killed by SIGPIPE once - # sdkmanager stops reading, and pipefail would report that 141 as the - # step's own failure. - - name: Install Android SDK packages + - name: Setup Android SDK if: matrix.platform == 'android' && matrix.build - run: | - SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" - SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" - if [ ! -x "$SDKMANAGER" ]; then - SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" - fi - if [ ! -x "$SDKMANAGER" ]; then - echo "sdkmanager not found under $SDK_ROOT" >&2 - exit 1 - fi - yes | "$SDKMANAGER" --licenses >/dev/null - "$SDKMANAGER" "platforms;android-36" "build-tools;36.0.0" + uses: ./.github/actions/setup-android-sdk # CNG's generated Android project exposes the ordinary Gradle Release task; # it builds and packages every ABI without device resolution. Installation diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ab6df60e21..534613e420 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -84,7 +84,8 @@ when a running daemon needs to pick up a new TypeScript build. That Android leg needs `AGENT_DEVICE_ANDROID_BUILD_TOOLS` naming the build-tools version to compile with. An unpinned build takes the newest version installed on the machine, which CI refuses to do, -so name the version the CI lanes install and the published helper matches the CI-built one. +so name the version `.github/actions/setup-android-sdk` installs and the published helper matches +the CI-built one. `pnpm package:npm` is a release guard, not a routine development command. Use the specific commands above while iterating. diff --git a/test/ci/trusted-fixture-artifact.test.mjs b/test/ci/trusted-fixture-artifact.test.mjs index 0a67e43147..1ecfd16b13 100644 --- a/test/ci/trusted-fixture-artifact.test.mjs +++ b/test/ci/trusted-fixture-artifact.test.mjs @@ -48,9 +48,7 @@ test('producer, consumers, upload, and concurrency use the canonical platform-sc assert.match(fetchStep.run, /"\$\{\{ inputs\.platform \}\}" "\$DEST"/); assert.match(fetchArtifact, /resolve-artifact-name\.sh" "\$PLATFORM"/); assert.match(fetchArtifact, /find "\$REPOSITORY" "\$NAME" "\$EXPECTED_HEAD_SHA"/); - assert.match(androidBuildToolsStep.run, /build-tools;36\.0\.0/); - assert.match(androidBuildToolsStep.run, /apksigner/); - assert.match(androidBuildToolsStep.run, /set \+o pipefail/); + assert.equal(androidBuildToolsStep.uses, './.github/actions/setup-android-sdk'); assert.match(androidFallbackStep.if, /inputs\.platform == 'android'/); assert.match(androidFallbackStep.run, /expo prebuild --platform android --no-install/); assert.match(androidFallbackStep.run, /:app:assembleRelease/); @@ -237,7 +235,7 @@ test('Android APK locator emits an exact APK path and package id, and rejects co const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'fixture-android-apk-')); t.after(() => fs.rmSync(tempRoot, { force: true, recursive: true })); const apkDir = path.join(tempRoot, 'apk'); - const binDir = path.join(tempRoot, 'build-tools', '36.0.0'); + const binDir = path.join(tempRoot, 'build-tools', 'test-build-tools'); const outputPath = path.join(tempRoot, 'output'); fs.mkdirSync(apkDir); fs.mkdirSync(binDir, { recursive: true }); @@ -253,12 +251,13 @@ test('Android APK locator emits an exact APK path and package id, and rejects co ].join('\n'), ); fs.chmodSync(path.join(binDir, 'aapt'), 0o755); - const runLocator = (packageId, aaptFails = false) => + const runLocator = (packageId, aaptFails = false, buildTools = 'test-build-tools') => spawnSync('bash', ['.github/actions/setup-fixture-app/locate-android-apk.sh', apkDir], { cwd: process.cwd(), encoding: 'utf8', env: { ...process.env, + AGENT_DEVICE_ANDROID_BUILD_TOOLS: buildTools, ANDROID_HOME: tempRoot, GITHUB_OUTPUT: outputPath, PATH: `${binDir}:${process.env.PATH}`, @@ -288,12 +287,16 @@ test('Android APK locator emits an exact APK path and package id, and rejects co result = runLocator('com.example.fixture', true); assert.notEqual(result.status, 0); assert.match(result.stderr, /it may be malformed/); + + result = runLocator('com.example.fixture', false, ''); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /AGENT_DEVICE_ANDROID_BUILD_TOOLS must name/); }); test('Android APK repack signs the output and preserves its package id', (t) => { const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'fixture-android-repack-')); t.after(() => fs.rmSync(tempRoot, { force: true, recursive: true })); - const buildTools = path.join(tempRoot, 'build-tools', '36.0.0'); + const buildTools = path.join(tempRoot, 'build-tools', 'test-build-tools'); const binDir = path.join(tempRoot, 'bin'); const source = path.join(tempRoot, 'source.apk'); const output = path.join(tempRoot, 'output', 'fixture.apk'); @@ -347,7 +350,7 @@ test('Android APK repack signs the output and preserves its package id', (t) => fs.chmodSync(executable, 0o755); } - const runRepack = (repackOutput, outputDigest = 'source-digest') => + const runRepack = (repackOutput, outputDigest = 'source-digest', buildToolsEnv = {}) => spawnSync( 'bash', ['.github/actions/setup-fixture-app/repack-android-apk.sh', source, repackOutput], @@ -356,6 +359,9 @@ test('Android APK repack signs the output and preserves its package id', (t) => encoding: 'utf8', env: { ...process.env, + AGENT_DEVICE_ANDROID_BUILD_TOOLS: 'test-build-tools', + CI: 'true', + ...buildToolsEnv, ANDROID_HOME: tempRoot, PATH: `${binDir}:${process.env.PATH}`, TEST_COMMAND_LOG: commandLog, @@ -378,6 +384,18 @@ test('Android APK repack signs the output and preserves its package id', (t) => const mismatch = runRepack(mismatchedOutput, 'different-digest'); assert.notEqual(mismatch.status, 0); assert.match(mismatch.stderr, /did not preserve the source signing certificate/); + + const unpinned = runRepack(path.join(tempRoot, 'output', 'unpinned.apk'), 'source-digest', { + AGENT_DEVICE_ANDROID_BUILD_TOOLS: '', + }); + assert.notEqual(unpinned.status, 0); + assert.match(unpinned.stderr, /AGENT_DEVICE_ANDROID_BUILD_TOOLS must name/); + + const local = runRepack(path.join(tempRoot, 'output', 'local.apk'), 'source-digest', { + AGENT_DEVICE_ANDROID_BUILD_TOOLS: '', + CI: '', + }); + assert.equal(local.status, 0, local.stderr); }); test('producer maps each platform to its resolved lookup and matrix artifact name, and gates has-work when both are cached', (t) => { diff --git a/test/repo-guards/android-sdk-setup-action.test.ts b/test/repo-guards/android-sdk-setup-action.test.ts new file mode 100644 index 0000000000..d1f8515a67 --- /dev/null +++ b/test/repo-guards/android-sdk-setup-action.test.ts @@ -0,0 +1,41 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { test } from 'vitest'; + +// `.github/actions/setup-android-sdk` owns the pinned build-tools version and the sdkmanager +// install. The version decides the helper APK bytes, so a second copy can drift silently. + +const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); +const OWNER_DIR = path.join('.github', 'actions', 'setup-android-sdk'); +const SCANNED_ROOTS = ['.github', 'scripts']; +const OWNED_PATTERNS = [/\b36\.0\.0\b/, /sdkmanager/]; + +function listFiles(relativeDir: string, files: string[] = []): string[] { + for (const entry of fs.readdirSync(path.join(REPO_ROOT, relativeDir), { withFileTypes: true })) { + const relativePath = path.join(relativeDir, entry.name); + if (entry.isDirectory()) listFiles(relativePath, files); + else if (entry.isFile()) files.push(relativePath); + } + return files; +} + +test('only setup-android-sdk names the build-tools version or runs sdkmanager', () => { + const offenders = SCANNED_ROOTS.flatMap((root) => listFiles(root)) + .filter((file) => !file.startsWith(`${OWNER_DIR}${path.sep}`)) + .flatMap((file) => { + const source = fs.readFileSync(path.join(REPO_ROOT, file), 'utf8'); + return OWNED_PATTERNS.filter((pattern) => pattern.test(source)).map( + (pattern) => `${file} matches ${pattern}`, + ); + }); + assert.deepEqual(offenders, [], 'call ./.github/actions/setup-android-sdk instead'); +}); + +test('setup-android-sdk lets a real licence failure stop the step', () => { + const action = fs.readFileSync(path.join(REPO_ROOT, OWNER_DIR, 'action.yml'), 'utf8'); + assert.match(action, /set \+o pipefail\n\s*yes \| "\$SDKMANAGER" --licenses >\/dev\/null\n/); + assert.doesNotMatch(action, /--licenses[^\n]*\|\|/); + assert.match(action, /AGENT_DEVICE_ANDROID_BUILD_TOOLS=\$BUILD_TOOLS_VERSION" >> "\$GITHUB_ENV"/); +}); From 819d1ddf9a6b3f39b6e7b118806f4d363b54ac54 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 05:30:54 +0000 Subject: [PATCH 2/2] ci: check every build-tools binary before skipping the SDK install setup-android-sdk skipped sdkmanager when aapt and apksigner existed, but the helper build also runs aapt2, d8, and zipalign. The repack script's newest-version lookup exited silently under pipefail when the build-tools directory was missing; it now reaches its own diagnostic. The repack test covers version ordering (9.0.0, 35.0.1, 36.0.0) and a missing directory. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4 --- .github/actions/setup-android-sdk/action.yml | 10 ++++++-- .../setup-fixture-app/repack-android-apk.sh | 2 +- test/ci/trusted-fixture-artifact.test.mjs | 24 ++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/.github/actions/setup-android-sdk/action.yml b/.github/actions/setup-android-sdk/action.yml index b192aa8365..95142da4ea 100644 --- a/.github/actions/setup-android-sdk/action.yml +++ b/.github/actions/setup-android-sdk/action.yml @@ -27,8 +27,14 @@ runs: set -euo pipefail SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/usr/local/lib/android/sdk}}" BUILD_TOOLS_DIR="$SDK_ROOT/build-tools/$BUILD_TOOLS_VERSION" - if [ ! -x "$BUILD_TOOLS_DIR/aapt" ] || [ ! -x "$BUILD_TOOLS_DIR/apksigner" ] || - [ ! -f "$SDK_ROOT/platforms/android-36/android.jar" ]; then + # Every build-tools binary a caller runs: the helper build (aapt2, d8, zipalign, + # apksigner) and the fixture APK scripts (aapt, apksigner). + INSTALLED=true + for BUILD_TOOL in aapt aapt2 d8 zipalign apksigner; do + [ -x "$BUILD_TOOLS_DIR/$BUILD_TOOL" ] || INSTALLED=false + done + [ -f "$SDK_ROOT/platforms/android-36/android.jar" ] || INSTALLED=false + if [ "$INSTALLED" != true ]; then SDKMANAGER="$SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" if [ ! -x "$SDKMANAGER" ]; then SDKMANAGER="$SDK_ROOT/cmdline-tools/bin/sdkmanager" diff --git a/.github/actions/setup-fixture-app/repack-android-apk.sh b/.github/actions/setup-fixture-app/repack-android-apk.sh index 3394983805..407c61e8e8 100644 --- a/.github/actions/setup-fixture-app/repack-android-apk.sh +++ b/.github/actions/setup-fixture-app/repack-android-apk.sh @@ -13,7 +13,7 @@ if [ -z "$BUILD_TOOLS_VERSION" ]; then fi NEWEST_BUILD_TOOLS_DIR="$( find "$SDK_ROOT/build-tools" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | sort -V | tail -n 1 - )" + )" || true BUILD_TOOLS_VERSION="${NEWEST_BUILD_TOOLS_DIR##*/}" fi BUILD_TOOLS="$SDK_ROOT/build-tools/$BUILD_TOOLS_VERSION" diff --git a/test/ci/trusted-fixture-artifact.test.mjs b/test/ci/trusted-fixture-artifact.test.mjs index 1ecfd16b13..3d2830bd73 100644 --- a/test/ci/trusted-fixture-artifact.test.mjs +++ b/test/ci/trusted-fixture-artifact.test.mjs @@ -360,9 +360,9 @@ test('Android APK repack signs the output and preserves its package id', (t) => env: { ...process.env, AGENT_DEVICE_ANDROID_BUILD_TOOLS: 'test-build-tools', + ANDROID_HOME: tempRoot, CI: 'true', ...buildToolsEnv, - ANDROID_HOME: tempRoot, PATH: `${binDir}:${process.env.PATH}`, TEST_COMMAND_LOG: commandLog, TEST_REPACK_OUTPUT: repackOutput, @@ -391,11 +391,33 @@ test('Android APK repack signs the output and preserves its package id', (t) => assert.notEqual(unpinned.status, 0); assert.match(unpinned.stderr, /AGENT_DEVICE_ANDROID_BUILD_TOOLS must name/); + // A local repack takes the newest installed version by version order, not by name: plain + // `sort` would pick 9.0.0. Only the newest directory holds working tools. + const localSdk = path.join(tempRoot, 'local-sdk'); + for (const version of ['9.0.0', '35.0.1']) { + const dir = path.join(localSdk, 'build-tools', version); + fs.mkdirSync(dir, { recursive: true }); + for (const tool of ['aapt', 'apksigner']) { + fs.writeFileSync(path.join(dir, tool), '#!/bin/sh\nexit 9\n'); + fs.chmodSync(path.join(dir, tool), 0o755); + } + } + fs.cpSync(buildTools, path.join(localSdk, 'build-tools', '36.0.0'), { recursive: true }); const local = runRepack(path.join(tempRoot, 'output', 'local.apk'), 'source-digest', { AGENT_DEVICE_ANDROID_BUILD_TOOLS: '', + ANDROID_HOME: localSdk, CI: '', }); assert.equal(local.status, 0, local.stderr); + assert.match(fs.readFileSync(commandLog, 'utf8'), /build-tools\/36\.0\.0 --js-bundle-only/); + + const missing = runRepack(path.join(tempRoot, 'output', 'missing.apk'), 'source-digest', { + AGENT_DEVICE_ANDROID_BUILD_TOOLS: '', + ANDROID_HOME: path.join(tempRoot, 'empty-sdk'), + CI: '', + }); + assert.notEqual(missing.status, 0); + assert.match(missing.stderr, /Android build tools were not found/); }); test('producer maps each platform to its resolved lookup and matrix artifact name, and gates has-work when both are cached', (t) => {