Files
jpush-phonegap-plugin/hooks/androidManifestToolsNs.js
weiry 7a3406511f android: 用 after_prepare hook 替代 edit-config 注入 xmlns:tools
避免多插件场景下 edit-config 修改同一 <manifest> 节点时产生冲突,
改为在 prepare 后由 hooks/androidManifestToolsNs.js 按需幂等注入。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 10:38:13 +08:00

42 lines
1.3 KiB
JavaScript

/**
* Ensures root <manifest> declares xmlns:tools so tools:replace on JPUSH_* meta-data merges.
* Idempotent: if xmlns:tools is already present, does nothing (avoids edit-config conflicts at plugin add).
*/
'use strict';
const fs = require('fs');
const path = require('path');
const TOOLS_NS = 'xmlns:tools="http://schemas.android.com/tools"';
function hasToolsNamespace(xml) {
return /\bxmlns:tools\s*=\s*["']http:\/\/schemas\.android\.com\/tools["']/.test(xml);
}
function ensureToolsNamespace(xml) {
if (hasToolsNamespace(xml)) {
return xml;
}
return xml.replace(/<manifest(\s+)/, '<manifest ' + TOOLS_NS + '$1');
}
module.exports = function (context) {
const projectRoot = (context.opts && context.opts.projectRoot) || process.cwd();
const candidates = [
path.join(projectRoot, 'platforms', 'android', 'app', 'src', 'main', 'AndroidManifest.xml'),
path.join(projectRoot, 'platforms', 'android', 'AndroidManifest.xml'),
];
for (const manifestPath of candidates) {
if (!fs.existsSync(manifestPath)) {
continue;
}
const before = fs.readFileSync(manifestPath, 'utf8');
const after = ensureToolsNamespace(before);
if (after !== before) {
fs.writeFileSync(manifestPath, after, 'utf8');
console.log('[jpush-phonegap-plugin] Added ' + TOOLS_NS + ' to ' + manifestPath);
}
}
};