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>
This commit is contained in:
weiry
2026-04-28 10:38:13 +08:00
parent 8f866944ee
commit 7a3406511f
2 changed files with 43 additions and 4 deletions
+41
View File
@@ -0,0 +1,41 @@
/**
* 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);
}
}
};
+2 -4
View File
@@ -78,10 +78,8 @@
</platform>
<platform name="android">
<!-- aar 合并时与 cn.jiguang.sdk:jpush 的 meta-data 冲突,需声明 tools 并用 tools:replace 覆盖 -->
<edit-config file="AndroidManifest.xml" target="/manifest" mode="merge">
<manifest xmlns:tools="http://schemas.android.com/tools" />
</edit-config>
<!-- xmlns:tools 由 hooks/androidManifestToolsNs.js 在 prepare 后按需注入,避免与其它插件/工程 edit-config 冲突 -->
<hook type="after_prepare" src="hooks/androidManifestToolsNs.js" />
<config-file target="res/xml/config.xml" parent="/*">
<feature name="JPushPlugin">