Skip to content

Commit

Permalink
Support multiple Dex files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gh0u1L5 authored and Gh0u1L5 committed Sep 30, 2017
1 parent ec096c5 commit b265b84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/main/java/net/dongliu/apk/parser/AbstractApkFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.ByteBuffer;
import java.security.cert.CertificateException;
import java.util.*;
import static java.lang.System.arraycopy;

/**
* Common Apk Parser methods.
Expand Down Expand Up @@ -184,20 +185,41 @@ public Icon getIconFile() throws IOException {
*/
public DexClass[] getDexClasses() throws IOException {
if (this.dexClasses == null) {
parseDexFile();
parseDexFiles();
}
return this.dexClasses;
}

private void parseDexFile() throws IOException {
byte[] data = getFileData(AndroidConstants.DEX_FILE);
private DexClass[] mergeDexClasses(DexClass[] first, DexClass[] second) {
DexClass[] result = new DexClass[first.length + second.length];
arraycopy(first, 0, result, 0, first.length);
arraycopy(second, 0, result, first.length, second.length);
return result;
}

private DexClass[] parseDexFile(String path) throws IOException {
byte[] data = getFileData(path);
if (data == null) {
throw new ParserException("Dex file not found");
String msg = String.format("Dex file %s not found", path);
throw new ParserException(msg);
}
ByteBuffer buffer = ByteBuffer.wrap(data);
DexParser dexParser = new DexParser(buffer);
dexParser.parse();
this.dexClasses = dexParser.getDexClasses();
return dexParser.getDexClasses();
}

private void parseDexFiles() throws IOException {
this.dexClasses = parseDexFile(AndroidConstants.DEX_FILE);
for (int i = 2; i < 1000; i++) {
String path = String.format(AndroidConstants.DEX_ADDITIONAL, i);
try {
DexClass[] classes = parseDexFile(path);
this.dexClasses = mergeDexClasses(this.dexClasses, classes);
} catch (ParserException e){
break;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class AndroidConstants {

public static final String DEX_FILE = "classes.dex";

public static final String DEX_ADDITIONAL = "classes%d.dex";

public static final String RES_PREFIX = "res/";

public static final String ASSETS_PREFIX = "assets/";
Expand Down

0 comments on commit b265b84

Please sign in to comment.