diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java index bc8af177..55effc50 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -185,11 +185,11 @@ public class Androlib { // loop all items in container recursively, ignoring any that are pre-defined by aapt Set files = unk.getFiles(true); for (String file : files) { - if (!isAPKFileNames(file)) { + if (!isAPKFileNames(file) && !file.endsWith(".dex")) { // copy file out of archive into special "unknown" folder // to be re-included on build - unk.copyToDir(unknownOut,file); + unk.copyToDir(unknownOut, file); try { // ignore encryption apkZipFile.getEntry(file).getGeneralPurposeBit().useEncryption(false); @@ -309,6 +309,7 @@ public class Androlib { new File(appDir, APK_DIRNAME).mkdirs(); buildSources(appDir, flags); + buildNonDefaultSources(appDir, flags); buildResources(appDir, flags, (Map) meta.get("usesFramework")); buildLib(appDir, flags); buildCopyOriginalFiles(appDir, flags); @@ -321,44 +322,61 @@ public class Androlib { public void buildSources(File appDir, HashMap flags) throws AndrolibException { - if (!buildSourcesRaw(appDir, flags) - && !buildSourcesSmali(appDir, flags) - && !buildSourcesJava(appDir, flags)) { + if (!buildSourcesRaw(appDir, "classes.dex", flags) && !buildSourcesSmali(appDir, "smali", "classes.dex", flags) && !buildSourcesJava(appDir, flags)) { LOGGER.warning("Could not find sources"); } } - public boolean buildSourcesRaw(File appDir, HashMap flags) + public void buildNonDefaultSources(ExtFile appDir, HashMap flags) throws AndrolibException { try { - File working = new File(appDir, "classes.dex"); - if (!working.exists()) { - return false; + Map dirs = appDir.getDirectory().getDirs(); + for (Map.Entry directory : dirs.entrySet()) { + String name = directory.getKey(); + if (name.startsWith("smali_")) { + String filename = name.substring(name.indexOf("_") + 1) + ".dex"; + + if (!buildSourcesRaw(appDir, filename, flags) && !buildSourcesSmali(appDir, name, filename, flags) && !buildSourcesJava(appDir, flags)) { + LOGGER.warning("Could not find sources"); + } + } } - File stored = new File(appDir, APK_DIRNAME + "/classes.dex"); - if (flags.get("forceBuildAll") || isModified(working, stored)) { - LOGGER.info("Copying classes.dex file..."); - BrutIO.copyAndClose(new FileInputStream(working), - new FileOutputStream(stored)); - } - return true; - } catch (IOException ex) { + } catch(DirectoryException ex) { throw new AndrolibException(ex); } } - public boolean buildSourcesSmali(File appDir, HashMap flags) + public boolean buildSourcesRaw(File appDir, String filename, HashMap flags) throws AndrolibException { - ExtFile smaliDir = new ExtFile(appDir, "smali"); + File working = new File(appDir, filename); + if (!working.exists()) { + return false; + } + File stored = new File(appDir, APK_DIRNAME + "/" + filename); + if (flags.get("forceBuildAll") || isModified(working, stored)) { + LOGGER.info("Copying " + appDir.toString() + " " + filename + " file..."); + try { + BrutIO.copyAndClose(new FileInputStream(working), new FileOutputStream(stored)); + return true; + } catch (IOException ex) { + throw new AndrolibException(ex); + } + } + return true; + } + + public boolean buildSourcesSmali(File appDir, String folder, String filename, HashMap flags) + throws AndrolibException { + ExtFile smaliDir = new ExtFile(appDir, folder); if (!smaliDir.exists()) { return false; } - File dex = new File(appDir, APK_DIRNAME + "/classes.dex"); + File dex = new File(appDir, APK_DIRNAME + "/" + filename); if (!flags.get("forceBuildAll")) { LOGGER.info("Checking whether sources has changed..."); } if (flags.get("forceBuildAll") || isModified(smaliDir, dex)) { - LOGGER.info("Smaling..."); + LOGGER.info("Smaling " + folder + " folder into " + filename +"..."); dex.delete(); SmaliBuilder.build(smaliDir, dex, flags); } @@ -385,8 +403,7 @@ public class Androlib { public void buildResources(ExtFile appDir, HashMap flags, Map usesFramework) throws BrutException { - if (!buildResourcesRaw(appDir, flags) - && !buildResourcesFull(appDir, flags, usesFramework) + if (!buildResourcesRaw(appDir, flags) && !buildResourcesFull(appDir, flags, usesFramework) && !buildManifest(appDir, flags, usesFramework)) { LOGGER.warning("Could not find resources"); } @@ -727,8 +744,7 @@ public class Androlib { private String mAaptPath = null; private Path mPath = null; - private final static Logger LOGGER = Logger.getLogger(Androlib.class - .getName()); + private final static Logger LOGGER = Logger.getLogger(Androlib.class.getName()); private final static String SMALI_DIRNAME = "smali"; private final static String APK_DIRNAME = "build/apk"; @@ -742,5 +758,5 @@ public class Androlib { private final static String[] APK_MANIFEST_FILENAMES = new String[] { "AndroidManifest.xml" }; private final static String[] APK_STANDARD_ALL_FILENAMES = new String[] { - "classes.dex", "AndroidManifest.xml", "resources.arsc","res","lib", "libs","assets","META-INF" }; + "classes.dex", "AndroidManifest.xml", "resources.arsc", "res", "lib", "libs", "assets", "META-INF" }; } 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 29bac1be..e5735b78 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 @@ -36,13 +36,12 @@ import org.jf.dexlib2.writer.io.FileDataStore; */ public class SmaliBuilder { - public static void build(ExtFile smaliDir, File dexFile, - HashMap flags) throws AndrolibException { + public static void build(ExtFile smaliDir, File dexFile, HashMap flags) + throws AndrolibException { new SmaliBuilder(smaliDir, dexFile, flags).build(); } - private SmaliBuilder(ExtFile smaliDir, File dexFile, - HashMap flags) { + private SmaliBuilder(ExtFile smaliDir, File dexFile, HashMap flags) { mSmaliDir = smaliDir; mDexFile = dexFile; mFlags = flags; @@ -61,8 +60,8 @@ public class SmaliBuilder { } } - private void buildFile(String fileName, DexBuilder dexBuilder) throws AndrolibException, - IOException { + private void buildFile(String fileName, DexBuilder dexBuilder) + throws AndrolibException, IOException { File inFile = new File(mSmaliDir, fileName); InputStream inStream = new FileInputStream(inFile); @@ -96,8 +95,7 @@ public class SmaliBuilder { out.append(".source \"").append(inFile.getName()).append("\"\n"); while (it.hasNext()) { String line = it.next().split("//", 2)[1].trim(); - if (line.isEmpty() || line.charAt(0) == '#' - || line.startsWith(".source")) { + if (line.isEmpty() || line.charAt(0) == '#' || line.startsWith(".source")) { continue; } if (line.startsWith(".method ")) { @@ -123,6 +121,5 @@ public class SmaliBuilder { private final File mDexFile; private final HashMap mFlags; - private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class - .getName()); + private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class.getName()); } diff --git a/brut.apktool/apktool-lib/src/test/resources/brut/apktool/testapp/smali_classes2/HelloDualDexSupport.smali b/brut.apktool/apktool-lib/src/test/resources/brut/apktool/testapp/smali_classes2/HelloDualDexSupport.smali new file mode 100644 index 00000000..e9613981 --- /dev/null +++ b/brut.apktool/apktool-lib/src/test/resources/brut/apktool/testapp/smali_classes2/HelloDualDexSupport.smali @@ -0,0 +1,15 @@ +.class public LHelloDualDexSupport; + +.super Ljava/lang/Object; + +.method public static main([Ljava/lang/String;)V + .registers 2 + + sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream; + + const/high16 v1, 0x7f020000 + + invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V + + return-void +.end method \ No newline at end of file