Skip to content

Commit

Permalink
Experimental dex features
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed May 16, 2024
1 parent a454a19 commit 0fc5229
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
Binary file modified libs/ARSCLib.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.reandroid.dex.sections.Marker;
import com.reandroid.json.JSONObject;
import com.reandroid.json.JSONWriter;
import com.reandroid.utils.collection.CollectionUtil;

import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOExc
.key("name").value(dexFile.getFileName())
.key("version").value(dexFile.getVersion())
.key("markers").array();
List<Marker> markersList = dexFile.getMarkers();
List<Marker> markersList = CollectionUtil.toList(dexFile.getMarkers());
for(Marker marker : markersList){
jsonWriter.value(marker.getJsonObject());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.reandroid.dex.sections.Marker;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.StringsUtil;
import com.reandroid.utils.collection.CollectionUtil;

import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOExc
writer.write("\n");
writeNameValue("Name", dexFile.getFileName());
writeNameValue("Version", dexFile.getVersion());
List<Marker> markersList = dexFile.getMarkers();
List<Marker> markersList = CollectionUtil.toList(dexFile.getMarkers());
if(markersList.size() != 0){
writer.write("Markers:");
for(Marker marker : markersList){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.reandroid.dex.sections.MapItem;
import com.reandroid.dex.sections.MapList;
import com.reandroid.dex.sections.Marker;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.ComputeList;
import com.reandroid.xml.kxml2.KXmlSerializer;
import com.reandroid.arsc.array.ResValueMapArray;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOExc
serializer.startTag(null, "dex");
serializer.attribute(null, "name", dexFile.getFileName());
serializer.attribute(null, "version", Integer.toString(dexFile.getVersion()));
List<Marker> markersList = dexFile.getMarkers();
List<Marker> markersList = CollectionUtil.toList(dexFile.getMarkers());
writeArray("markers", markersList.toArray());

MapList mapList = dexFile.getDexLayout().getMapList();
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/reandroid/apkeditor/smali/SmaliCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,24 @@ public static int apiToDexVersion(int api) {
if (api <= 23) {
return 35;
}
if (api <= 25) {
return 37;
}
if (api <= 27) {
return 38;
switch (api) {
case 24:
case 25:
return 37;
case 26:
case 27:
return 38;
case 28:
return 39;
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
return 40;
case 35:
return 41;
}
return 39;
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/reandroid/apkeditor/smali/SmaliDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
import com.reandroid.apkeditor.APKEditor;
import com.reandroid.apkeditor.decompile.DecompileOptions;
import com.reandroid.arsc.chunk.TableBlock;
import com.reandroid.dex.common.AnnotationVisibility;
import com.reandroid.dex.data.AnnotationItem;
import com.reandroid.dex.key.TypeKey;
import com.reandroid.dex.model.DexClassRepository;
import com.reandroid.dex.model.DexDirectory;
import com.reandroid.dex.model.DexFile;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.dex.smali.SmaliWriterSetting;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.collection.ArrayCollection;
import org.jf.baksmali.Baksmali;
import org.jf.baksmali.BaksmaliOptions;
import org.jf.dexlib2.Opcodes;
Expand All @@ -34,7 +41,10 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class SmaliDecompiler implements DexDecoder {
private final TableBlock tableBlock;
Expand Down Expand Up @@ -67,6 +77,12 @@ public void decodeDex(ApkModule apkModule, File mainDirectory) throws IOExceptio
}
logMessage("Loading full dex ...");
DexDirectory directory = DexDirectory.fromZip(apkModule.getZipEntryMap());
if(decompileOptions.noDexDebug) {
logMessage("Clean debug info ...");
clearDebug_DirtyMethod(directory);
directory.refresh();
}
directory.refresh();
File smali = toSmaliRoot(mainDirectory);
SmaliWriterSetting setting = new SmaliWriterSetting();
setting.setResourceIdComment(tableBlock.pickOne());
Expand Down Expand Up @@ -102,6 +118,11 @@ private void disassembleJesusFreke(DexFileInputSource inputSource, File mainDir)
private void disassembleDexFileExperimental(DexFileInputSource inputSource, File mainDir) throws IOException {
DexFile dexFile = DexFile.read(inputSource.openStream());
dexFile.setSimpleName(inputSource.getAlias());
if(decompileOptions.noDexDebug) {
logMessage("Clean debug info ...");
clearDebug_DirtyMethod(dexFile);
dexFile.refresh();
}
SmaliWriterSetting setting = new SmaliWriterSetting();
setting.setResourceIdComment(tableBlock.pickOne());
setting.addClassComments(dexFile);
Expand All @@ -111,6 +132,38 @@ private void disassembleDexFileExperimental(DexFileInputSource inputSource, File
dexFile.writeSmali(smaliWriter, toSmaliRoot(mainDir));
dexFile.close();
}
private void clearDebug_DirtyMethod(DexClassRepository repository) {
repository.clearDebug();
Set<TypeKey> removeSet = new HashSet<>();
Iterator<AnnotationItem> iterator = repository.getItems(SectionType.ANNOTATION_ITEM);
while (iterator.hasNext()) {
AnnotationItem annotationItem = iterator.next();
TypeKey typeKey = annotationItem.getTypeKey();
if(annotationItem.getVisibility() == AnnotationVisibility.BUILD ||
removeAnnotation(typeKey)) {
removeSet.add(typeKey);
}
}
List<TypeKey> removedList = new ArrayCollection<>(removeSet);
removedList.sort(CompareUtil.getComparableComparator());
for(TypeKey typeKey : removedList) {
logMessage(" Removed @: " + typeKey);
repository.removeAnnotations(typeKey);
}
}
private boolean removeAnnotation(TypeKey typeKey) {
String name = typeKey.getTypeName();
if(name.startsWith("Lkotlin")) {
return true;
}
if(name.startsWith("Ljava/")) {
return true;
}
if(name.startsWith("Ljavax/")) {
return true;
}
return name.contains("Null");
}
private void writeDexCache(DexFileInputSource inputSource, File mainDir) throws IOException {
File cache = new File(mainDir, SmaliUtil.CACHE_DIR);
cache = new File(cache, inputSource.getAlias());
Expand Down

0 comments on commit 0fc5229

Please sign in to comment.