Skip to content

Commit 0b6221e

Browse files
committed
[baksmali] Load full dex minimally for commenting class & method hierarchy of large no of dex files
1 parent 7ba6433 commit 0b6221e

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/main/java/com/reandroid/apkeditor/smali/SmaliDecompiler.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SmaliDecompiler implements DexDecoder {
5050
private SmaliWriterSetting smaliWriterSetting;
5151
private Opcodes mCurrentOpcodes;
5252
private APKLogger apkLogger;
53+
private boolean mDexForCommentLoaded;
5354

5455
public SmaliDecompiler(TableBlock tableBlock, DecompileOptions decompileOptions) {
5556
this.tableBlock = tableBlock;
@@ -74,11 +75,16 @@ public void decodeDex(ApkModule apkModule, File mainDirectory) throws IOExceptio
7475
}
7576
DexDirectory directory = (DexDirectory) apkModule.getTag(DexDirectory.class);
7677
if (directory == null) {
77-
if (apkModule.listDexFiles().size() > decompileOptions.loadDex) {
78+
int size = apkModule.listDexFiles().size();
79+
logMessage("Dex files: " + size);
80+
if (size > decompileOptions.loadDex) {
81+
if (size < decompileOptions.loadDex * 5) {
82+
loadMinimalDexForComment(apkModule);
83+
}
7884
DexDecoder.super.decodeDex(apkModule, mainDirectory);
7985
return;
8086
}
81-
logMessage("Loading full dex files: " + apkModule.listDexFiles().size());
87+
logMessage("Loading full dex files ...");
8288
Predicate<SectionType<?>> filter;
8389
if (decompileOptions.noDexDebug) {
8490
filter = sectionType -> sectionType != SectionType.DEBUG_INFO;
@@ -106,6 +112,19 @@ public void decodeDex(ApkModule apkModule, File mainDirectory) throws IOExceptio
106112
}
107113
}
108114

115+
private void loadMinimalDexForComment(ApkModule apkModule) throws IOException {
116+
String commentLevel = decompileOptions.commentLevel;
117+
if (!DecompileOptions.COMMENT_LEVEL_DETAIL.equals(commentLevel) &&
118+
!DecompileOptions.COMMENT_LEVEL_FULL.equals(commentLevel)) {
119+
return;
120+
}
121+
logMessage("Loading basic structures of dex ...");
122+
DexDirectory dexDirectory = DexDirectory.fromZip(
123+
apkModule.getZipEntryMap(), SectionType.minimal());
124+
mDexForCommentLoaded = false;
125+
getSmaliWriterSetting(dexDirectory);
126+
mDexForCommentLoaded = true;
127+
}
109128
private void disassembleWithJesusFrekeLib(DexFileInputSource inputSource, File mainDir) throws IOException {
110129
File dir = toOutDir(inputSource, mainDir);
111130
BaksmaliOptions options = new BaksmaliOptions();
@@ -135,8 +154,10 @@ private void disassembleWithInternalDexLib(DexFileInputSource inputSource, File
135154
SmaliWriter smaliWriter = new SmaliWriter();
136155
smaliWriter.setWriterSetting(setting);
137156
dexFile.writeSmali(smaliWriter, toSmaliRoot(mainDir));
138-
setting.clearClassComments();
139-
setting.clearMethodComments();
157+
if (!mDexForCommentLoaded) {
158+
setting.clearClassComments();
159+
setting.clearMethodComments();
160+
}
140161
dexFile.close();
141162
}
142163
private void writeDexCache(DexFileInputSource inputSource, File mainDir) throws IOException {
@@ -191,12 +212,14 @@ public ResourceComment getComment() {
191212

192213
private SmaliWriterSetting getSmaliWriterSetting(DexClassRepository classRepository) {
193214
SmaliWriterSetting setting = getSmaliWriterSetting();
194-
setting.clearClassComments();
195-
setting.clearMethodComments();
196-
String commentLevel = decompileOptions.commentLevel;
197-
if (DecompileOptions.COMMENT_LEVEL_DETAIL.equals(commentLevel) || DecompileOptions.COMMENT_LEVEL_FULL.equals(commentLevel)) {
198-
setting.addClassComments(classRepository);
199-
setting.addMethodComments(classRepository);
215+
if (!mDexForCommentLoaded) {
216+
setting.clearClassComments();
217+
setting.clearMethodComments();
218+
String commentLevel = decompileOptions.commentLevel;
219+
if (DecompileOptions.COMMENT_LEVEL_DETAIL.equals(commentLevel) || DecompileOptions.COMMENT_LEVEL_FULL.equals(commentLevel)) {
220+
setting.addClassComments(classRepository);
221+
setting.addMethodComments(classRepository);
222+
}
200223
}
201224
return setting;
202225
}

0 commit comments

Comments
 (0)