fix(patch): 优化Manifest权限与authority映射

重构了权限和authority的映射逻辑,统一使用targetPackage变量,支持com.google.android前缀不变。

修改部分參考 #37,主要用於修復 #25

Co-Authored-By: areteruhiro <108941410+areteruhiro@users.noreply.github.com>
This commit is contained in:
NkBe 2025-12-11 21:21:41 +08:00
parent 2245f35f61
commit f215471400
No known key found for this signature in database
GPG Key ID: 525137026FF031DF
1 changed files with 24 additions and 28 deletions

View File

@ -441,38 +441,34 @@ public class NPatch {
private byte[] modifyManifestFile(InputStream is, String metadata, int minSdkVersion, String originPackage, String newPackage, List<String> permissions, List<String> uses_permissions, List<String> authorities) throws IOException { private byte[] modifyManifestFile(InputStream is, String metadata, int minSdkVersion, String originPackage, String newPackage, List<String> permissions, List<String> uses_permissions, List<String> authorities) throws IOException {
ModificationProperty property = new ModificationProperty(); ModificationProperty property = new ModificationProperty();
String targetPackage = (newPackage != null && !newPackage.isEmpty()) ? newPackage : originPackage;
if (overrideVersionCode) if (overrideVersionCode)
property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, 1)); property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, 1));
if (minSdkVersion < 28) if (minSdkVersion < 28)
property.addUsesSdkAttribute(new AttributeItem(NodeValue.UsesSDK.MIN_SDK_VERSION, 27)); property.addUsesSdkAttribute(new AttributeItem(NodeValue.UsesSDK.MIN_SDK_VERSION, 27));
property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggableFlag)); property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggableFlag));
property.addApplicationAttribute(new AttributeItem("appComponentFactory", PROXY_APP_COMPONENT_FACTORY)); property.addApplicationAttribute(new AttributeItem("appComponentFactory", PROXY_APP_COMPONENT_FACTORY));
if (newPackage != null && !newPackage.isEmpty()){ if (!targetPackage.equals(originPackage)) {
property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.PACKAGE, newPackage).setNamespace(null)); property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.PACKAGE, targetPackage).setNamespace(null));
} }
property.setPermissionMapper(new PermissionMapper() { property.setPermissionMapper((type, permission) -> {
@Override
public String map(PermissionType type, String permission) {
if (permission.startsWith(originPackage)) { if (permission.startsWith(originPackage)) {
assert newPackage != null; return permission.replaceFirst(originPackage, targetPackage);
return permission.replaceFirst(originPackage, newPackage);
} }
if (permission.startsWith("android") if (permission.startsWith("android")
|| permission.startsWith("com.android")){ || permission.startsWith("com.android")
|| permission.startsWith("com.google.android")) {
return permission; return permission;
} }
return newPackage + "_" + permission; return targetPackage + "_" + permission;
}
}); });
property.setAuthorityMapper(new AttributeMapper<String>() {
@Override property.setAuthorityMapper(value -> {
public String map(String value) {
if (value.startsWith(originPackage)) { if (value.startsWith(originPackage)) {
assert newPackage != null; return value.replaceFirst(originPackage, targetPackage);
return value.replaceFirst(originPackage, newPackage);
}
return newPackage + "_" + value;
} }
return targetPackage + "_" + value;
}); });
property.addMetaData(new ModificationProperty.MetaData("npatch", metadata)); property.addMetaData(new ModificationProperty.MetaData("npatch", metadata));
@ -491,11 +487,11 @@ public class NPatch {
} }
var os = new ByteArrayOutputStream(); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
(new ManifestEditor(is, os, property)).processManifest(); new ManifestEditor(is, os, property).processManifest();
is.close();
os.flush();
os.close();
return os.toByteArray(); return os.toByteArray();
} finally {
if (is != null) is.close();
}
} }
} }