Skip to content

Commit

Permalink
Detect incompatible apk for merging
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Aug 2, 2024
1 parent 4fa8e67 commit 22a5816
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/reandroid/apk/ApkBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public ApkBundle(){
}

public ApkModule mergeModules() throws IOException {
return mergeModules(false);
}
public ApkModule mergeModules(boolean force) throws IOException {
List<ApkModule> moduleList=getApkModuleList();
if(moduleList.size()==0){
throw new FileNotFoundException("Nothing to merge, empty modules");
Expand All @@ -45,7 +48,7 @@ public ApkModule mergeModules() throws IOException {
if(base == null){
base = getLargestTableModule();
}
result.merge(base);
result.merge(base, force);
ApkSignatureBlock signatureBlock = null;
for(ApkModule module:moduleList){
ApkSignatureBlock asb = module.getApkSignatureBlock();
Expand All @@ -58,7 +61,7 @@ public ApkModule mergeModules() throws IOException {
if(signatureBlock == null){
signatureBlock = asb;
}
result.merge(module);
result.merge(module, force);
}

result.setApkSignatureBlock(signatureBlock);
Expand Down
58 changes: 55 additions & 3 deletions src/main/java/com/reandroid/apk/ApkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ private List<Entry> filterResFileEntries(TableString tableString, int resourceId
});
return CollectionUtil.toList(itr);
}
public int getVersionCode() {
AndroidManifestBlock manifestBlock = getAndroidManifest();
if(manifestBlock != null) {
Integer versionCode = manifestBlock.getVersionCode();
if(versionCode != null) {
return versionCode;
}
}
return 0;
}
public String getPackageName(){
if(hasAndroidManifest()){
return getAndroidManifest().getPackageName();
Expand Down Expand Up @@ -1141,20 +1151,62 @@ public void setLoadDefaultFramework(boolean loadDefaultFramework) {
}

public void merge(ApkModule module) throws IOException {
if(module==null||module==this){
merge(module, false);
}
public void merge(ApkModule module, boolean force) throws IOException {
if(module == null || module == this){
return;
}
logMessage("Merging: "+module.getModuleName());
logMessage("Merging: " + module.getModuleName());
validateMerge(module, force);
mergeDexFiles(module);
mergeTable(module);
mergeFiles(module);
getUncompressedFiles().merge(module.getUncompressedFiles());
}
private void validateMerge(ApkModule apkModule, boolean force) throws IOException{
if(!hasTableBlock()) {
return;
}
String packageName = getPackageName();
int code = getVersionCode();
if(packageName == null || code == 0) {
return;
}
String packageName2 = apkModule.getPackageName();
int code2 = apkModule.getVersionCode();
if(packageName2 == null || code2 == 0) {
return;
}
if(!packageName.equals(packageName2)) {
return;
}
if(code == code2) {
return;
}
StringBuilder builder = new StringBuilder();
if(!force) {
builder.append("WARN: ");
}
builder.append("Incompatible to merge: {");
builder.append(packageName);
builder.append(", ");
builder.append(code);
builder.append("}, with {");
builder.append(packageName2);
builder.append(", ");
builder.append(code2);
builder.append("}");
String msg = builder.toString();
if(force) {
throw new IOException(msg);
}
logMessage(msg);
}
private void mergeTable(ApkModule module) {
if(!module.hasTableBlock()){
return;
}
logMessage("Merging resource table: "+module.getModuleName());
TableBlock exist;
if(!hasTableBlock()){
exist=new TableBlock();
Expand Down

0 comments on commit 22a5816

Please sign in to comment.