/** * 修 vendored 的 Termux 源码:它们 import 的是 com.termux.view.R, * 但代码并进我们工程后 R 类变成 com.doubao.lynxclone.R 了。 * 顺手把 res-extra 里的资源并进 res/。 */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const repo = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const appMain = path.join(repo, 'android', 'app', 'src', 'main'); const javaRoot = path.join(appMain, 'java'); const resRoot = path.join(appMain, 'res'); const resExtra = path.join(appMain, 'res-extra'); let fixed = 0; function walk(dir) { for (const entry of fs.readdirSync(dir)) { const full = path.join(dir, entry); if (fs.statSync(full).isDirectory()) { walk(full); continue; } if (!full.endsWith('.java')) continue; const src = fs.readFileSync(full, 'utf8'); if (src.includes('import com.termux.view.R;')) { fs.writeFileSync(full, src.replace('import com.termux.view.R;', 'import com.doubao.lynxclone.R;'), 'utf8'); fixed += 1; console.log(' 改 R 引用:', path.relative(javaRoot, full)); } } } console.log('修正 R 类引用…'); walk(path.join(javaRoot, 'com', 'termux')); if (fs.existsSync(resExtra)) { console.log('合并 Termux 资源…'); const copy = (from, to) => { if (fs.statSync(from).isDirectory()) { fs.mkdirSync(to, { recursive: true }); for (const e of fs.readdirSync(from)) copy(path.join(from, e), path.join(to, e)); } else { // ★ values/ 下的 strings.xml 必须改名,否则会把我们自己的 strings.xml 覆盖掉(踩过) let target = to; if (path.basename(from) === 'strings.xml') { target = path.join(path.dirname(to), 'termux_strings.xml'); } fs.mkdirSync(path.dirname(target), { recursive: true }); fs.copyFileSync(from, target); console.log(' +', path.relative(resRoot, target)); } }; copy(resExtra, resRoot); fs.rmSync(resExtra, { recursive: true, force: true }); console.log(' 已删除 res-extra'); } console.log(`完成:改了 ${fixed} 个文件的 R 引用`);