diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/DebugInjector.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/DebugInjector.java index 75fe7284..7de89117 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/DebugInjector.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/DebugInjector.java @@ -193,7 +193,7 @@ public class DebugInjector { } private String next() { - return mIt.next().trim(); + return mIt.next().split("//", 2)[1].trim(); } private String nextAndAppend() { diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java index 16992c29..ceeef835 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java @@ -76,17 +76,16 @@ public class SmaliBuilder { if (!mFlags.containsKey("debug")) { final String[] linesArray = lines.toArray(new String[0]); - for (int i = 2; i < linesArray.length - 2; i++) { - out.append(linesArray[i]).append('\n'); + for (int i = 1; i < linesArray.length - 1; i++) { + out.append(linesArray[i].split("//", 2)[1]).append('\n'); } } else { lines.remove(lines.size() - 1); - lines.remove(lines.size() - 1); - ListIterator it = lines.listIterator(2); + ListIterator it = lines.listIterator(1); out.append(".source \"").append(inFile.getName()).append("\"\n"); while (it.hasNext()) { - String line = it.next().trim(); + String line = it.next().split("//", 2)[1].trim(); if (line.isEmpty() || line.charAt(0) == '#' || line.startsWith(".source")) { continue; diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliDecoder.java index 248e0dc3..2d8aba8a 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliDecoder.java @@ -17,10 +17,20 @@ package brut.androlib.src; import brut.androlib.AndrolibException; +import org.jf.baksmali.baksmali; +import org.jf.dexlib.Code.Analysis.ClassPath; +import org.jf.dexlib.DexFile; + +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.IOException; -import org.jf.baksmali.baksmali; -import org.jf.dexlib.DexFile; +import java.nio.charset.Charset; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; /** * @author Ryszard Wiśniewski @@ -35,24 +45,62 @@ public class SmaliDecoder { private SmaliDecoder(File apkFile, File outDir, boolean debug, boolean bakdeb) { mApkFile = apkFile; - mOutDir = outDir; + mOutDir = outDir.toPath(); mDebug = debug; mBakDeb = bakdeb; } private void decode() throws AndrolibException { try { + ClassPath.dontLoadClassPath = mDebug; baksmali.disassembleDexFile(mApkFile.getAbsolutePath(), - new DexFile(mApkFile), false, mOutDir.getAbsolutePath(), + new DexFile(mApkFile), false, mOutDir.toAbsolutePath().toString(), null, null, null, false, true, true, mBakDeb, false, false, - 0, false, false, null, false); + mDebug ? org.jf.baksmali.main.DIFFPRE : 0, false, false, null, false); + + if (mDebug) { + Files.walkFileTree(mOutDir, new SmaliFileVisitor()); + } } catch (IOException ex) { throw new AndrolibException(ex); } } private final File mApkFile; - private final File mOutDir; + private final Path mOutDir; private final boolean mDebug; private final boolean mBakDeb; + + + private class SmaliFileVisitor extends SimpleFileVisitor { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + String fileName = file.getFileName().toString(); + if (! fileName.endsWith(".smali")) { + return FileVisitResult.CONTINUE; + } + fileName = fileName.substring(0, fileName.length() - 6); + try ( + BufferedReader in = Files.newBufferedReader(file, Charset.defaultCharset()); + BufferedWriter out = Files.newBufferedWriter( + file.resolveSibling(fileName + ".java"), Charset.defaultCharset()) + ) { + TypeName type = TypeName.fromPath(mOutDir.relativize(file.resolveSibling(fileName))); + out.write("package " + type.package_ + "; class " + type.getName(true, true) + " {"); + out.newLine(); + + String line; + while ((line = in.readLine()) != null) { + out.write("// "); + out.write(line); + out.newLine(); + } + + out.write("}"); + out.newLine(); + } + Files.delete(file); + return FileVisitResult.CONTINUE; + } + } }