Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 保留 Android 构建失败诊断

DavidShen3dc650e03465
diff --git a/apps/mobile/scripts/build-android.mjs b/apps/mobile/scripts/build-android.mjs
index cd31cfcd79..d2d3d5e3b8 100644
--- a/apps/mobile/scripts/build-android.mjs
+++ b/apps/mobile/scripts/build-android.mjs
@@ -90,6 +90,47 @@ const NPX = process.platform === 'win32' ? 'npx.cmd' : 'npx';
function log(msg) { console.error(msg); }
+const ANDROID_BUILD_FAILURE_STAGE = Object.freeze({
+ arguments: 'arguments',
+ regionConfig: 'region-config',
+ buildPlan: 'build-plan',
+ preflight: 'preflight',
+ toolchain: 'toolchain',
+ artifactValidation: 'artifact-validation',
+ output: 'output',
+});
+
+let androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.arguments;
+
+// 只按脚本控制的阶段输出固定诊断,不打印异常消息或环境变量值。
+function reportAndroidBuildFailure() {
+ switch (androidBuildFailureStage) {
+ case ANDROID_BUILD_FAILURE_STAGE.arguments:
+ console.error('Android 构建失败(参数检查):请确认已传 --region cn|global|dev,且 --artifacts、--version-code 等参数合法。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.regionConfig:
+ console.error('Android 构建失败(区域配置):请检查 apps/mobile/scripts/self-host-regions.json 及对应 endpoint 配置是否存在且有效。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.buildPlan:
+ console.error('Android 构建失败(构建计划):请检查 apps/mobile/app.json、android-version.json 与区域产物配置。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.preflight:
+ console.error('Android 构建失败(构建前检查):请检查 Git 门禁、签名配置、JDK 17+ 与必需的 EXPO_PUBLIC_* 配置。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.toolchain:
+ console.error('Android 构建失败(工具链):请查看上方 Expo/Gradle 输出;若无输出,请检查 npx、JDK 17+、Android SDK 与 Gradle wrapper。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.artifactValidation:
+ console.error('Android 构建失败(产物校验):请检查 APK/AAB 路径、包名、versionCode、runtimeVersion 与签名证书配置。');
+ break;
+ case ANDROID_BUILD_FAILURE_STAGE.output:
+ console.error('Android 构建失败(复制产物):请检查 --out 目录权限与磁盘空间。');
+ break;
+ default:
+ console.error('Android 构建失败;未能确定失败阶段。');
+ }
+}
+
// self-host 变体的构建环境(与原发布线同源),注入 versionCode。
function selfhostEnv(region, versionCode, desktopVersion) {
const env = {
@@ -321,6 +362,7 @@ function validateAabSignature(aabPath, javaEnv, expectedCertificateSha256) {
}
async function main() {
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.arguments;
const args = parseArgs(process.argv.slice(2));
// --region 必填(cn|global|dev):选出本次出包身份 + 签名配置(见 lib/self-host-region.mjs)。
// 不走 resolveSelfHostRegion:它对 dev 强校验发布专用的 npkgExpectBundle,与纯构建
@@ -334,10 +376,12 @@ async function main() {
if (!SELF_HOST_REGIONS.includes(rawRegion)) {
throw new Error(`--region 只能是 ${SELF_HOST_REGIONS.join(' 或 ')},收到: ${rawRegion}`);
}
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.regionConfig;
const region = loadSelfHostRegions({ mode: 'local' })[rawRegion];
if (!region.androidPackage?.trim()) {
throw new Error(`self-host-regions.json 的 ${region.authRegion}.androidPackage 未填(构建必需)`);
}
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.buildPlan;
const artifactKinds = resolveAndroidArtifactKinds(region.authRegion, args.artifacts);
const appJson = readAppJson();
const version = appJson?.expo?.version ?? '';
@@ -359,6 +403,7 @@ async function main() {
// git 闸门只管真构建:dry-run 纯本地(不做 origin/main 远端比对,分支/离线可跑)。
let expectedUploadCertificateSha256 = null;
if (args.execute) {
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.preflight;
if (!args.skipGitGate) assertProductionGitGate();
else log(' warn: --skip-git-gate,跳过 main/clean/HEAD 校验(仅本地迭代用)');
if (missingBake.length) {
@@ -409,12 +454,15 @@ async function main() {
return;
}
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.preflight;
// region / endpoint manifest 自举基址必须齐全(读仓内 config/endpoint*.json,离线可用)。
assertPublicEnv(env, { variant: 'production', requiredKeys: SELF_HOST_PUBLIC_ENV_KEYS });
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.toolchain;
const built = buildAndroidArtifacts(env, region, artifactKinds);
for (const kind of artifactKinds) log(` ✓ ${kind}: ${built.artifacts[kind]}`);
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.artifactValidation;
// 权威 runtimeVersion 始终从真正烤进产物的 fingerprint 回读。双产物必须一致,
// 否则同一个 versionCode 会出现两条不兼容 OTA runtime,直接中止。
const runtimeVersions = {};
@@ -447,6 +495,7 @@ async function main() {
const finalArtifacts = { ...built.artifacts };
if (typeof args.out === 'string' && args.out) {
+ androidBuildFailureStage = ANDROID_BUILD_FAILURE_STAGE.output;
const outDir = resolve(String(args.out));
if (!existsSync(outDir)) mkdirSync(outDir, { recursive: true });
for (const kind of artifactKinds) {
@@ -469,7 +518,6 @@ async function main() {
}
main().catch(() => {
- // 子进程已继承 stdout/stderr;这里只输出固定提示,避免异常消息夹带签名口令等 env 值。
- console.error('Android 构建失败;详细原因请查看上方构建工具输出。');
+ reportAndroidBuildFailure();
process.exit(1);
});
diff --git a/scripts/__tests__/client-endpoint-build-env.test.mjs b/scripts/__tests__/client-endpoint-build-env.test.mjs
index b47aa23b6f..19ec8bd94e 100644
--- a/scripts/__tests__/client-endpoint-build-env.test.mjs
+++ b/scripts/__tests__/client-endpoint-build-env.test.mjs
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
@@ -109,6 +110,27 @@ test('Mobile 构建入口不把动态异常或环境变量值写入失败日志'
}
});
+test('Android 构建在子进程启动前失败时输出安全且可执行的诊断', () => {
+ const fakeSecret = 'fake-keystore-password-that-must-not-leak';
+ const result = spawnSync(
+ process.execPath,
+ [path.resolve('apps/mobile/scripts/build-android.mjs'), '--region', 'invalid'],
+ {
+ encoding: 'utf8',
+ env: {
+ ...process.env,
+ XDT_ANDROID_KEYSTORE_PASSWORD_DEV: fakeSecret,
+ },
+ },
+ );
+
+ assert.equal(result.status, 1);
+ assert.match(result.stderr, /Android 构建失败(参数检查)/);
+ assert.match(result.stderr, /--region cn\|global\|dev/);
+ assert.doesNotMatch(result.stderr, new RegExp(fakeSecret));
+ assert.doesNotMatch(result.stderr, /详细原因请查看上方构建工具输出/);
+});
+
test('端点清单自举基址缺失、非法协议或携带凭据时 fail closed', () => {
const repoRoot = writeRepoFixtures();
const cnPath = path.join(repoRoot, 'config', 'endpoint.json');