/** * The Android app's privacy on the generated project, and the proof on the built APK * (src/android-privacy.ts; tauri-platforms.md D6). * * tsx scripts/android.ts prepare `tauri android init` if needed, icons, then apply * tsx scripts/android.ts apply patch the manifest, copy the rule files, then check * tsx scripts/android.ts check what the generated project lacks; exit 1 if anything * tsx scripts/android.ts verify [apk] the same, read from the BUILT APK with aapt2 * * `apply` runs from tauri.android.conf.json's beforeBuildCommand, so `tauri android build` * cannot make an APK without it. A Gradle build started outside the Tauri CLI skips that hook; * `verify` reads the artefact itself, and `pnpm tauri:android` runs it last. */ import { spawnSync } from 'node:child_process'; import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { apkProblems, manifestProblems, patchManifest, RULE_FILES, RULE_SECTIONS, ruleProblems } from '../src/android-privacy.js'; import { PACKAGE, SRC_TAURI, tauri } from './tauri.js'; const MAIN = join(SRC_TAURI, 'gen', 'android', 'app', 'src', 'main'); const MANIFEST = join(MAIN, 'AndroidManifest.xml'); const RES_FROM = join(PACKAGE, 'android', 'res', 'xml'); const RES_TO = join(MAIN, 'res', 'xml'); const OUTPUTS = join(SRC_TAURI, 'gen', 'android', 'app', 'build', 'outputs', 'apk'); // Rosea's own MainActivity: the save bridge (keep-her-safe-apps.md A2, A5). const IDENTIFIER = (JSON.parse(readFileSync(join(SRC_TAURI, 'tauri.conf.json'), 'utf8')) as { identifier: string }).identifier; const ACTIVITY_FROM = join(PACKAGE, 'android', 'java', 'MainActivity.kt'); const ACTIVITY_TO = join(MAIN, 'java', ...IDENTIFIER.split('.'), 'MainActivity.kt'); function fail(problems: readonly string[]): never { for (const p of problems) console.error(`android: ${p}`); process.exit(1); } function problems(): string[] { if (!existsSync(MANIFEST)) return [`no generated project (${MANIFEST}); run 'pnpm tauri:android'`]; const found = manifestProblems(readFileSync(MANIFEST, 'utf8')); for (const f of RULE_FILES) { const at = join(RES_TO, f); if (!existsSync(at)) found.push(`res/xml/${f} is missing`); else if (readFileSync(at, 'utf8') !== readFileSync(join(RES_FROM, f), 'utf8')) found.push(`res/xml/${f} differs from android/res/xml/${f}`); } if (!existsSync(ACTIVITY_TO)) found.push(`no MainActivity.kt at ${ACTIVITY_TO}`); else if (readFileSync(ACTIVITY_TO, 'utf8') !== readFileSync(ACTIVITY_FROM, 'utf8')) { found.push("MainActivity.kt is not Rosea's (android/java/MainActivity.kt): no save bridge"); } // The launcher icon is Rosea's, not the Tauri logo `init` put there (scripts/icons.ts). const ours = join(SRC_TAURI, 'icons', 'android', 'mipmap-mdpi', 'ic_launcher.png'); const there = join(MAIN, 'res', 'mipmap-mdpi', 'ic_launcher.png'); if (!existsSync(ours) || !existsSync(there) || !readFileSync(ours).equals(readFileSync(there))) { found.push("the launcher icon is not Rosea's (run 'tsx scripts/icons.ts')"); } return found; } function apply(): void { for (const f of RULE_FILES) { const left = ruleProblems(readFileSync(join(RES_FROM, f), 'utf8'), RULE_SECTIONS[f]); if (left.length > 0) fail(left.map((p) => `android/res/xml/${f}: ${p}`)); } if (!existsSync(MANIFEST)) fail([`no generated project (${MANIFEST}); run 'pnpm tauri:android'`]); const before = readFileSync(MANIFEST, 'utf8'); const after = patchManifest(before); mkdirSync(RES_TO, { recursive: true }); for (const f of RULE_FILES) copyFileSync(join(RES_FROM, f), join(RES_TO, f)); if (!existsSync(dirname(ACTIVITY_TO))) fail([`no generated activity folder ${dirname(ACTIVITY_TO)}; the identifier changed?`]); copyFileSync(ACTIVITY_FROM, ACTIVITY_TO); if (after !== before) writeFileSync(MANIFEST, after); const left = problems(); if (left.length > 0) fail(left); console.log( `android: privacy ${after === before ? 'already in place' : 'applied'} ` + "(no INTERNET, no backup or transfer, WebView metrics and Safe Browsing off; Rosea's MainActivity)" ); } function newestApk(dir: string): string | undefined { if (!existsSync(dir)) return undefined; let best: { path: string; at: number } | undefined; const walk = (d: string): void => { for (const name of readdirSync(d)) { const path = join(d, name); const stat = statSync(path); if (stat.isDirectory()) walk(path); else if (name.endsWith('.apk') && (!best || stat.mtimeMs > best.at)) best = { path, at: stat.mtimeMs }; } }; walk(dir); return best?.path; } function aapt2(): string { const sdk = process.env['ANDROID_HOME'] ?? process.env['ANDROID_SDK_ROOT']; if (!sdk) fail(['ANDROID_HOME is not set']); const tools = join(sdk, 'build-tools'); const versions = readdirSync(tools).sort((a, b) => b.localeCompare(a, undefined, { numeric: true })); const exe = process.platform === 'win32' ? 'aapt2.exe' : 'aapt2'; for (const v of versions) if (existsSync(join(tools, v, exe))) return join(tools, v, exe); return fail([`no aapt2 under ${tools}`]); } function verify(apk: string | undefined): void { const file = apk ?? newestApk(OUTPUTS); if (file === undefined || !existsSync(file)) fail([`no APK to verify under ${OUTPUTS}`]); const tool = aapt2(); const run = (...args: string[]): string => { const r = spawnSync(tool, args, { encoding: 'utf8' }); if (r.status !== 0) fail([`aapt2 ${args.join(' ')} failed: ${r.stderr}`]); return r.stdout; }; const left = apkProblems(run('dump', 'permissions', file), run('dump', 'xmltree', '--file', 'AndroidManifest.xml', file)); if (left.length > 0) fail(left); console.log(`android: ${file} verified (no permission asked, no backup, WebView metrics and Safe Browsing off)`); } const [command, arg] = process.argv.slice(2); try { if (command === 'prepare') { if (!existsSync(MANIFEST)) tauri('android', 'init', '--ci', '--skip-targets-install'); await import('./icons.js'); apply(); } else if (command === 'apply') apply(); else if (command === 'check') { const left = problems(); if (left.length > 0) fail(left); console.log('android: privacy in place'); } else if (command === 'verify') verify(arg); else fail(['usage: android.ts prepare|apply|check|verify [apk]']); } catch (error) { fail([(error as Error).message]); }