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:
parent
2245f35f61
commit
f215471400
|
|
@ -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
|
if (permission.startsWith(originPackage)) {
|
||||||
public String map(PermissionType type, String permission) {
|
return permission.replaceFirst(originPackage, targetPackage);
|
||||||
if (permission.startsWith(originPackage)){
|
|
||||||
assert newPackage != null;
|
|
||||||
return permission.replaceFirst(originPackage, newPackage);
|
|
||||||
}
|
|
||||||
if (permission.startsWith("android")
|
|
||||||
|| permission.startsWith("com.android")){
|
|
||||||
return permission;
|
|
||||||
}
|
|
||||||
return newPackage + "_" + permission;
|
|
||||||
}
|
}
|
||||||
|
if (permission.startsWith("android")
|
||||||
|
|| permission.startsWith("com.android")
|
||||||
|
|| permission.startsWith("com.google.android")) {
|
||||||
|
return 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)){
|
return value.replaceFirst(originPackage, targetPackage);
|
||||||
assert newPackage != null;
|
|
||||||
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();
|
return os.toByteArray();
|
||||||
os.flush();
|
} finally {
|
||||||
os.close();
|
if (is != null) is.close();
|
||||||
return os.toByteArray();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue