handle multiple packages on xml encoding

This commit is contained in:
REAndroid 2023-01-09 09:47:24 -05:00
parent de7eb68486
commit fa97a2c74b
3 changed files with 71 additions and 47 deletions

View File

@ -83,6 +83,7 @@ import java.util.*;
public void fromXml(XMLDocument xmlDocument) throws IOException { public void fromXml(XMLDocument xmlDocument) throws IOException {
mTable.fromXml(xmlDocument); mTable.fromXml(xmlDocument);
} }
public XMLDocument toXMLDocument(){ public XMLDocument toXMLDocument(){
return mTable.toXMLDocument(); return mTable.toXMLDocument();
} }

View File

@ -35,11 +35,12 @@
import java.util.regex.Matcher; import java.util.regex.Matcher;
public class EncodeMaterials implements ResourceResolver { public class EncodeMaterials implements ResourceResolver {
private ResourceIds.Table.Package packageIds; private final Set<ResourceIds.Table.Package> packageIdSet = new HashSet<>();
private PackageBlock currentPackage; private PackageBlock currentPackage;
private final Set<FrameworkTable> frameworkTables = new HashSet<>(); private final Set<FrameworkTable> frameworkTables = new HashSet<>();
private APKLogger apkLogger; private APKLogger apkLogger;
private boolean mForceCreateNamespaces = true; private boolean mForceCreateNamespaces = true;
private Set<String> mFrameworkPackageNames;
public EncodeMaterials(){ public EncodeMaterials(){
} }
public SpecString getSpecString(String name){ public SpecString getSpecString(String name){
@ -134,10 +135,11 @@
return getFrameworkEntry(packageName, type, name); return getFrameworkEntry(packageName, type, name);
} }
public int resolveLocalResourceId(String type, String name){ public int resolveLocalResourceId(String type, String name){
ResourceIds.Table.Package.Type.Entry entry = for(ResourceIds.Table.Package pkg:packageIdSet){
this.packageIds.getEntry(type, name); ResourceIds.Table.Package.Type.Entry entry = pkg.getEntry(type, name);
if(entry!=null){ if(entry!=null){
return entry.getResourceId(); return entry.getResourceId();
}
} }
EntryGroup entryGroup=getLocalEntryGroup(type, name); EntryGroup entryGroup=getLocalEntryGroup(type, name);
if(entryGroup!=null){ if(entryGroup!=null){
@ -240,14 +242,20 @@
return null; return null;
} }
private boolean isFrameworkPackageName(String packageName){ private boolean isFrameworkPackageName(String packageName){
return getFrameworkPackageNames().contains(packageName);
}
private Set<String> getFrameworkPackageNames(){
if(mFrameworkPackageNames!=null){
return mFrameworkPackageNames;
}
Set<String> results=new HashSet<>();
for(FrameworkTable table:frameworkTables){ for(FrameworkTable table:frameworkTables){
for(PackageBlock packageBlock:table.listPackages()){ for(PackageBlock packageBlock:table.listPackages()){
if(packageName.equals(packageBlock.getName())){ results.add(packageBlock.getName());
return true;
}
} }
} }
return false; mFrameworkPackageNames=results;
return results;
} }
public EntryBlock getFrameworkEntry(String packageName, String type, String name){ public EntryBlock getFrameworkEntry(String packageName, String type, String name){
for(FrameworkTable table:frameworkTables){ for(FrameworkTable table:frameworkTables){
@ -280,8 +288,12 @@
this.mForceCreateNamespaces = force; this.mForceCreateNamespaces = force;
return this; return this;
} }
public EncodeMaterials setPackageIds(ResourceIds.Table.Package packageIds) { public EncodeMaterials addPackageIds(Collection<ResourceIds.Table.Package> packageIdList) {
this.packageIds = packageIds; this.packageIdSet.addAll(packageIdList);
return this;
}
public EncodeMaterials addPackageIds(ResourceIds.Table.Package packageIds) {
this.packageIdSet.add(packageIds);
return this; return this;
} }
public EncodeMaterials setCurrentPackage(PackageBlock currentPackage) { public EncodeMaterials setCurrentPackage(PackageBlock currentPackage) {
@ -291,6 +303,7 @@
public EncodeMaterials addFramework(FrameworkTable frameworkTable) { public EncodeMaterials addFramework(FrameworkTable frameworkTable) {
frameworkTable.loadResourceNameMap(); frameworkTable.loadResourceNameMap();
this.frameworkTables.add(frameworkTable); this.frameworkTables.add(frameworkTable);
this.mFrameworkPackageNames=null;
return this; return this;
} }
public EncodeMaterials setAPKLogger(APKLogger logger) { public EncodeMaterials setAPKLogger(APKLogger logger) {
@ -298,9 +311,6 @@
return this; return this;
} }
public ResourceIds.Table.Package getPackageIds() {
return packageIds;
}
public PackageBlock getCurrentPackage() { public PackageBlock getCurrentPackage() {
return currentPackage; return currentPackage;
} }
@ -317,7 +327,7 @@
@Override @Override
public int resolveResourceId(String packageName, String type, String name) { public int resolveResourceId(String packageName, String type, String name) {
if(packageName==null || packageName.equals(getCurrentPackageName())){ if(!isFrameworkPackageName(packageName)){
return resolveLocalResourceId(type, name); return resolveLocalResourceId(type, name);
} }
return resolveFrameworkResourceId(packageName, type, name); return resolveFrameworkResourceId(packageName, type, name);
@ -356,7 +366,7 @@
resourceIds.loadPackageBlock(packageBlock); resourceIds.loadPackageBlock(packageBlock);
ResourceIds.Table.Package packageId = resourceIds.getTable().listPackages().get(0); ResourceIds.Table.Package packageId = resourceIds.getTable().listPackages().get(0);
return new EncodeMaterials() return new EncodeMaterials()
.setPackageIds(packageId) .addPackageIds(packageId)
.setCurrentPackage(packageBlock) .setCurrentPackage(packageBlock)
.addFramework(Frameworks.getAndroid()); .addFramework(Frameworks.getAndroid());
} }

View File

@ -28,10 +28,7 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
public class RESEncoder { public class RESEncoder {
@ -70,9 +67,23 @@
+ApkUtil.FILE_NAME_PUBLIC_XML+" file found in '"+mainDir); +ApkUtil.FILE_NAME_PUBLIC_XML+" file found in '"+mainDir);
} }
preloadStringPool(pubXmlFileList); preloadStringPool(pubXmlFileList);
for(File pubXmlFile:pubXmlFileList){
EncodeMaterials encodeMaterials = loadPublicXml(pubXmlFile); EncodeMaterials encodeMaterials=new EncodeMaterials();
Map<File, ResourceIds.Table.Package> map =
initializeEncodeMaterials(pubXmlFileList, encodeMaterials);
for(Map.Entry<File, ResourceIds.Table.Package> entry:map.entrySet()){
File pubXmlFile=entry.getKey();
ResourceIds.Table.Package pkgResourceIds=entry.getValue();
addParsedFiles(pubXmlFile); addParsedFiles(pubXmlFile);
PackageCreator packageCreator = new PackageCreator();
packageCreator.setPackageName(pkgResourceIds.name);
PackageBlock packageBlock = packageCreator.createNew(tableBlock, pkgResourceIds);
encodeMaterials.setCurrentPackage(packageBlock);
File resDir=toResDirectory(pubXmlFile); File resDir=toResDirectory(pubXmlFile);
encodeResDir(encodeMaterials, resDir); encodeResDir(encodeMaterials, resDir);
FilePathEncoder filePathEncoder = new FilePathEncoder(encodeMaterials); FilePathEncoder filePathEncoder = new FilePathEncoder(encodeMaterials);
@ -80,7 +91,6 @@
filePathEncoder.setUncompressedFiles(getApkModule().getUncompressedFiles()); filePathEncoder.setUncompressedFiles(getApkModule().getUncompressedFiles());
filePathEncoder.encodeResDir(resDir); filePathEncoder.encodeResDir(resDir);
PackageBlock packageBlock = encodeMaterials.getCurrentPackage();
packageBlock.sortTypes(); packageBlock.sortTypes();
packageBlock.refresh(); packageBlock.refresh();
@ -99,7 +109,6 @@
ValuesStringPoolBuilder poolBuilder=new ValuesStringPoolBuilder(); ValuesStringPoolBuilder poolBuilder=new ValuesStringPoolBuilder();
for(File pubXml:pubXmlFileList){ for(File pubXml:pubXmlFileList){
File resDir=toResDirectory(pubXml); File resDir=toResDirectory(pubXml);
logMessage("Scanning: "+resDir.getParentFile().getName());
List<File> valuesDirList = listValuesDir(resDir); List<File> valuesDirList = listValuesDir(resDir);
for(File dir:valuesDirList){ for(File dir:valuesDirList){
logVerbose(poolBuilder.size()+" building pool: "+dir.getName()); logVerbose(poolBuilder.size()+" building pool: "+dir.getName());
@ -108,33 +117,37 @@
} }
poolBuilder.addTo(tableBlock.getTableStringPool()); poolBuilder.addTo(tableBlock.getTableStringPool());
} }
private EncodeMaterials loadPublicXml(File pubXmlFile) throws IOException, XMLException {
ResourceIds resourceIds=new ResourceIds();
resourceIds.fromXml(pubXmlFile);
List<ResourceIds.Table.Package> pkgList = resourceIds private Map<File, ResourceIds.Table.Package> initializeEncodeMaterials(
.getTable().listPackages(); List<File> pubXmlFileList, EncodeMaterials encodeMaterials)
if(pkgList.size()!=1){ throws IOException, XMLException {
throw new IOException("Package count should be 1, count="
+pkgList.size()+", in file: "+pubXmlFile); Map<File, ResourceIds.Table.Package> results = new HashMap<>();
String packageName=null;
for(File pubXmlFile:pubXmlFileList){
if(packageName==null){
packageName=readManifestPackageName(toAndroidManifest(pubXmlFile));
}
ResourceIds resourceIds=new ResourceIds();
resourceIds.fromXml(pubXmlFile);
ResourceIds.Table.Package pkg = resourceIds.getTable()
.listPackages().get(0);
if(pkg.name==null){
pkg.name=packageName;
}
encodeMaterials.addPackageIds(pkg);
results.put(pubXmlFile, pkg);
} }
XMLDocument manifestDocument = XMLDocument.load(toAndroidManifest(pubXmlFile)); encodeMaterials.addFramework(Frameworks.getAndroid())
String packageName = manifestDocument
.getDocumentElement().getAttributeValue("package");
ResourceIds.Table.Package pkgResourceIds = pkgList.get(0);
PackageCreator packageCreator = new PackageCreator();
packageCreator.setPackageName(packageName);
PackageBlock packageBlock = packageCreator.createNew(tableBlock, pkgResourceIds);
return new EncodeMaterials()
.addFramework(Frameworks.getAndroid())
.setCurrentPackage(packageBlock)
.setPackageIds(pkgResourceIds)
.setAPKLogger(apkLogger); .setAPKLogger(apkLogger);
return results;
}
private String readManifestPackageName(File manifestFile) throws XMLException {
XMLDocument manifestDocument = XMLDocument.load(manifestFile);
return manifestDocument
.getDocumentElement().getAttributeValue("package");
} }
private void encodeResDir(EncodeMaterials materials, File resDir) throws XMLException { private void encodeResDir(EncodeMaterials materials, File resDir) throws XMLException {