From 75ad01c60a60b09d1af79c0b4505e37cdf2056a6 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Mon, 17 Sep 2012 22:26:10 -0500 Subject: [PATCH] Using zipj4 for adding stuff back into original apk --- .../src/main/assembly/jar-with-deps.xml | 1 + .../src/main/java/brut/apktool/Main.java | 7 +-- apktool-lib/pom.xml | 5 ++ .../src/main/java/brut/androlib/Androlib.java | 50 ++++++++++++++++--- .../brut/androlib/BuildAndDecodeTest.java | 3 +- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/apktool-cli/src/main/assembly/jar-with-deps.xml b/apktool-cli/src/main/assembly/jar-with-deps.xml index 2069ce10..cfea65c5 100644 --- a/apktool-cli/src/main/assembly/jar-with-deps.xml +++ b/apktool-cli/src/main/assembly/jar-with-deps.xml @@ -38,6 +38,7 @@ org.antlr:antlr-runtime com.google.* org.apache.* + net.lingala.* diff --git a/apktool-cli/src/main/java/brut/apktool/Main.java b/apktool-cli/src/main/java/brut/apktool/Main.java index 227d405a..64b8a4a5 100644 --- a/apktool-cli/src/main/java/brut/apktool/Main.java +++ b/apktool-cli/src/main/java/brut/apktool/Main.java @@ -23,6 +23,7 @@ import brut.androlib.ApktoolProperties; import brut.androlib.err.CantFindFrameworkResException; import brut.androlib.err.InFileNotFoundException; import brut.androlib.err.OutDirExistsException; +import brut.androlib.res.util.ExtFile; import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -165,7 +166,7 @@ public class Main { int i; int skip = 0; - File mOrigApk = null; + ExtFile mOrigApk = null; for (i = 0; i < args.length; i++) { String opt = args[i]; if (! opt.startsWith("-")) { @@ -182,7 +183,7 @@ public class Main { throw new InvalidArgsError(); } else { flags.put("injectOriginal", true); - mOrigApk = new File(args[i + 1]); + mOrigApk = new ExtFile(args[i + 1]); skip = 1; } } else { @@ -205,7 +206,7 @@ public class Main { throw new InvalidArgsError(); } - new Androlib().build(new File(appDirName), outFile, flags); + new Androlib().build(new File(appDirName), outFile, flags, mOrigApk); } private static void cmdInstallFramework(String[] args) diff --git a/apktool-lib/pom.xml b/apktool-lib/pom.xml index aac12364..1a9bed02 100644 --- a/apktool-lib/pom.xml +++ b/apktool-lib/pom.xml @@ -137,6 +137,11 @@ smali 1.3.4-ibot8 + + net.lingala.zip4j + zip4j + 1.3.1 + diff --git a/apktool-lib/src/main/java/brut/androlib/Androlib.java b/apktool-lib/src/main/java/brut/androlib/Androlib.java index 240529d6..a6d6565a 100644 --- a/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -33,6 +33,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -172,16 +176,25 @@ public class Androlib { } public void build(File appDir, File outFile, - HashMap flags) throws AndrolibException { - build(new ExtFile(appDir), outFile, flags); + HashMap flags, ExtFile origApk) throws AndrolibException { + build(new ExtFile(appDir), outFile, flags, origApk); } public void build(ExtFile appDir, File outFile, - HashMap flags) throws AndrolibException { + HashMap flags, ExtFile origApk) throws AndrolibException { Map meta = readMetaFile(appDir); Object t1 = meta.get("isFrameworkApk"); flags.put("framework", t1 == null ? false : (Boolean) t1); mAndRes.setSdkInfo((Map) meta.get("sdkInfo")); + + // check the orig apk + if (flags.get("injectOriginal")) { + if (!origApk.isFile() || !origApk.canRead()) { + throw new InFileNotFoundException(); + } else { + mOrigApkFile = origApk; + } + } if (outFile == null) { String outFileName = (String) meta.get("apkFileName"); @@ -439,9 +452,32 @@ public class Androlib { /* check for re-insert */ if (flags.get("injectOriginal")) { - // if (!mApkFile.isFile() || !mApkFile.canRead()) { - // throw new InFileNotFoundException(); - //} + try { + LOGGER.info("Building resources into original apk file..."); + ZipFile editOrig = new ZipFile(mOrigApkFile.getAbsoluteFile()); + + // no compression levels, paras + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + parameters.setCompressionLevel(0); + parameters.setIncludeRootFolder(true); + parameters.setRootFolderInZip("/"); + + // add res folder + editOrig.addFolder(new File(appDir, APK_DIRNAME + "/res").getAbsolutePath(), parameters); + + // add assets, if there + if (assetDir.exists()) { + //editOrig.addFolder(new File(appDir, APK_DIRNAME + "/assets").getAbsolutePath(), parameters); + } + + // add resources.arsc + parameters.setFileNameInZip("resources.arsc"); + // editOrig.addFile(new File(appDir, "resources.arsc"), parameters); + } catch(ZipException e) { + LOGGER.warning(e.getMessage()); + } + } } @@ -522,7 +558,7 @@ public class Androlib { } - private ExtFile mOrigApkFile; + private ExtFile mOrigApkFile = null; private final static Logger LOGGER = Logger.getLogger(Androlib.class.getName()); diff --git a/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java b/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java index 43e65aa2..13355d0d 100644 --- a/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java +++ b/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java @@ -45,7 +45,8 @@ public class BuildAndDecodeTest { "brut/apktool/testapp/", sTestOrigDir); LOGGER.info("Building testapp.apk..."); - new Androlib().build(sTestOrigDir, testApk, BuildAndDecodeTest.returnStock()); + ExtFile blank = null; + new Androlib().build(sTestOrigDir, testApk, BuildAndDecodeTest.returnStock(),blank); LOGGER.info("Decoding testapp.apk..."); ApkDecoder apkDecoder = new ApkDecoder(testApk);