progress on #403

This commit is contained in:
Connor Tumbleson 2013-03-20 08:53:39 -05:00
parent 071ce71a61
commit dca0182339
4 changed files with 190 additions and 126 deletions

View File

@ -1,3 +1,6 @@
v2.0.0 (TBA)
-Fixed (issue #8) - Correctly uses -c to retain original manifest and META-INF. Thanks M1cha
v1.5.3 (TBA) v1.5.3 (TBA)
-Updated to smali/baksmali to v1.4.2 -Updated to smali/baksmali to v1.4.2
-Fixed (issue #396) - Correctly handle android:debuggable while in debug mode. -Fixed (issue #396) - Correctly handle android:debuggable while in debug mode.

View File

@ -1 +1 @@
application.version=1.4.1 application.version=1.4.2

View File

@ -1 +1 @@
application.version=1.4.1 application.version=1.4.2

View File

@ -28,66 +28,67 @@ import brut.common.BrutException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.*; import java.util.logging.*;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.jf.util.ConsoleUtil;
/** /**
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class Main { public class Main {
public static void main(String[] args) throws IOException, public static void main(String[] args) throws IOException,
InterruptedException, BrutException { InterruptedException, BrutException {
try {
Verbosity verbosity = Verbosity.NORMAL; // set verbosity default
int i; Verbosity verbosity = Verbosity.NORMAL;
for (i = 0; i < args.length; i++) {
String opt = args[i]; // cli parser
CommandLineParser parser = new PosixParser();
CommandLine commandLine = null;
try {
commandLine = parser.parse(normalOptions, args);
} catch (ParseException ex) {
usage(commandLine);
return;
}
// check for verbose / quiet
if (commandLine.hasOption("-v") || commandLine.hasOption("--verbose")) {
verbosity = Verbosity.VERBOSE;
} else if (commandLine.hasOption("-q") || commandLine.hasOption("--quiet")) {
verbosity = Verbosity.QUIET;
}
setupLogging(verbosity);
// check for advance mode
if (commandLine.hasOption("-advance") || commandLine.hasOption("--advanced")) {
setAdvanceMode(true);
}
if (opt.startsWith("--version") || (opt.startsWith("-version"))) { // check for main options
version_print(); if (commandLine.hasOption("d") || commandLine.hasOption("decode")) {
System.exit(1); cmdDecode(args);
} } else if (commandLine.hasOption("b") || commandLine.hasOption("build")) {
if (!opt.startsWith("-")) { cmdBuild(args);
break; } else if (commandLine.hasOption("if") || commandLine.hasOption("install-framework")) {
} cmdInstallFramework(args);
if ("-v".equals(opt) || "--verbose".equals(opt)) { } else if (commandLine.hasOption("publicize-resources")) {
if (verbosity != Verbosity.NORMAL) { cmdPublicizeResources(args);
throw new InvalidArgsError(); } else {
} usage(commandLine);
verbosity = Verbosity.VERBOSE; }
} else if ("-q".equals(opt) || "--quiet".equals(opt)) {
if (verbosity != Verbosity.NORMAL) {
throw new InvalidArgsError();
}
verbosity = Verbosity.QUIET;
} else {
throw new InvalidArgsError();
}
}
setupLogging(verbosity);
if (args.length <= i) {
throw new InvalidArgsError();
}
String cmd = args[i];
args = Arrays.copyOfRange(args, i + 1, args.length);
if ("d".equals(cmd) || "decode".equals(cmd)) {
cmdDecode(args);
} else if ("b".equals(cmd) || "build".equals(cmd)) {
cmdBuild(args);
} else if ("if".equals(cmd) || "install-framework".equals(cmd)) {
cmdInstallFramework(args);
} else if ("publicize-resources".equals(cmd)) {
cmdPublicizeResources(args);
} else {
throw new InvalidArgsError();
}
} catch (InvalidArgsError ex) {
usage();
System.exit(1);
}
} }
private static void cmdDecode(String[] args) throws InvalidArgsError, private static void cmdDecode(String[] args) throws InvalidArgsError,
@ -268,82 +269,123 @@ public class Main {
new Androlib().publicizeResources(new File(args[0])); new Androlib().publicizeResources(new File(args[0]));
} }
private static void version_print() { private static void _version() {
System.out.println(Androlib.getVersion()); System.out.println(Androlib.getVersion());
} }
private static void usage() { private static void _Options() {
System.out
.println("Apktool v" // create basic options
+ Androlib.getVersion() Option versionOption = OptionBuilder.withLongOpt("version")
+ " - a tool for reengineering Android apk files\n" .withDescription("prints the version then exits")
+ "Copyright 2010 Ryszard Wiśniewski <brut.alll@gmail.com>\n" .create("v");
+ "with smali v"
+ ApktoolProperties.get("smaliVersion") Option noSrcOption = OptionBuilder.withLongOpt("no-src")
+ ", and baksmali v" .withDescription("Do not decode sources.")
+ ApktoolProperties.get("baksmaliVersion") .create("s");
+ "\n"
+ "Updated by @iBotPeaches <connor.tumbleson@gmail.com> \n" Option noResOption = OptionBuilder.withLongOpt("no-res")
+ "Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)\n" .withDescription("Do not decode resources.")
+ "\n" .create("r");
+ "Usage: apktool [-q|--quiet OR -v|--verbose] COMMAND [...]\n"
+ "\n" Option debugOption = OptionBuilder.withLongOpt("debug")
+ "COMMANDs are:\n" .withDescription("Decode in debug mode. Check project page for more info.")
+ "\n" .create("d");
+ " d[ecode] [OPTS] <file.apk> [<dir>]\n"
+ " Decode <file.apk> to <dir>.\n" // add them
+ "\n" normalOptions.addOption(versionOption);
+ " OPTS:\n" DecodeOptions.addOption(noSrcOption);
+ "\n" DecodeOptions.addOption(noResOption);
+ " -s, --no-src\n"
+ " Do not decode sources.\n" // check for advance mode
+ " -r, --no-res\n" if (advanceMode) {
+ " Do not decode resources.\n" DecodeOptions.addOption(debugOption);
+ " -d, --debug\n" }
+ " Decode in debug mode. Check project page for more info.\n" }
+ " -b, --no-debug-info\n"
+ " Baksmali -- don't write out debug info (.local, .param, .line, etc.)\n" private static String verbosityHelp() {
+ " -f, --force\n" if (advanceMode) {
+ " Force delete destination directory.\n" return "[-q|--quiet OR -v|--verbose]";
+ " -t <tag>, --frame-tag <tag>\n" } else {
+ " Try to use framework files tagged by <tag>.\n" return "";
+ " --frame-path <dir>\n" }
+ " Use the specified directory for framework files\n" }
+ " --keep-broken-res\n"
+ " Use if there was an error and some resources were dropped, e.g.:\n"
+ " \"Invalid config flags detected. Dropping resources\", but you\n" private static void usage(CommandLine commandLine) {
+ " want to decode them anyway, even with errors. You will have to\n"
+ " fix them manually before building." // load basicOptions
+ "\n\n" _Options();
+ " b[uild] [OPTS] [<app_path>] [<out_file>]\n" HelpFormatter formatter = new HelpFormatter();
+ " Build an apk from already decoded application located in <app_path>.\n" PrintWriter pw = new PrintWriter(System.out, true);
+ "\n"
+ " It will automatically detect, whether files was changed and perform\n" // max their window to 120, if small.
+ " needed steps only.\n" int consoleWidth = ConsoleUtil.getConsoleWidth();
+ "\n" if (consoleWidth <= 0) {
+ " If you omit <app_path> then current directory will be used.\n" consoleWidth = 120;
+ " If you omit <out_file> then <app_path>/dist/<name_of_original.apk>\n" }
+ " will be used.\n" formatter.setWidth(consoleWidth);
+ "\n"
+ " OPTS:\n" // print out license info prior to formatter.
+ "\n" System.out.println(
+ " -f, --force-all\n" "Apktool v" + Androlib.getVersion() + " - a tool for reengineering Android apk files\n" +
+ " Skip changes detection and build all files.\n" "Copyright 2010 Ryszard Wiśniewski <brut.alll@gmail.com>\n" +
+ " -d, --debug\n" "Updated by @iBotPeaches <connor.tumbleson@gmail.com> \n" +
+ " Build in debug mode. Check project page for more info.\n" "with smali v" + ApktoolProperties.get("smaliVersion") +
+ " -a, --aapt\n" " and baksmali v" + ApktoolProperties.get("baksmaliVersion") + "\n" +
+ " Loads aapt from specified location.\n" "Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)\n");
+ " -c, --copy-original\n"
+ " Copies original AndroidManifest.xml and META-INF.\n" // two different outputs for build / decode
+ " --frame-path <dir>\n" formatter.printHelp("apktool " + verbosityHelp() + "d[ecode] [options] <file_apk> [<out_file>]", DecodeOptions);
+ " Use the specified directory for framework files\n" formatter.printHelp("apktool " + verbosityHelp() + "b[uild] [options] <app_path> [<out_file>]", BuildOptions);
+ "\n"
+ " if|install-framework <framework.apk> [<tag>] --frame-path [<location>] \n"
+ " Install framework file to your system.\n"
+ "\n" // + " -d, --debug\n"
+ "For additional info, see: http://code.google.com/p/android-apktool/" // + " Decode in debug mode. Check project page for more info.\n"
+ "\n" // + " -b, --no-debug-info\n"
+ "For smali/baksmali info, see: http://code.google.com/p/smali/"); // + " Baksmali -- don't write out debug info (.local, .param, .line, etc.)\n"
// + " -f, --force\n"
// + " Force delete destination directory.\n"
// + " -t <tag>, --frame-tag <tag>\n"
// + " Try to use framework files tagged by <tag>.\n"
// + " --frame-path <dir>\n"
// + " Use the specified directory for framework files\n"
// + " --keep-broken-res\n"
// + " Use if there was an error and some resources were dropped, e.g.:\n"
// + " \"Invalid config flags detected. Dropping resources\", but you\n"
// + " want to decode them anyway, even with errors. You will have to\n"
// + " fix them manually before building."
// + "\n\n"
// + " b[uild] [OPTS] [<app_path>] [<out_file>]\n"
// + " Build an apk from already decoded application located in <app_path>.\n"
// + "\n"
// + " It will automatically detect, whether files was changed and perform\n"
// + " needed steps only.\n"
// + "\n"
// + " If you omit <app_path> then current directory will be used.\n"
// + " If you omit <out_file> then <app_path>/dist/<name_of_original.apk>\n"
// + " will be used.\n"
// + "\n"
// + " OPTS:\n"
// + "\n"
// + " -f, --force-all\n"
// + " Skip changes detection and build all files.\n"
// + " -d, --debug\n"
// + " Build in debug mode. Check project page for more info.\n"
// + " -a, --aapt\n"
// + " Loads aapt from specified location.\n"
// + " -c, --copy-original\n"
// + " Copies original AndroidManifest.xml and META-INF.\n"
// + " --frame-path <dir>\n"
// + " Use the specified directory for framework files\n"
// + "\n"
// + " if|install-framework <framework.apk> [<tag>] --frame-path [<location>] \n"
// + " Install framework file to your system.\n"
// + "\n"
// + "For additional info, see: http://code.google.com/p/android-apktool/"
// + "\n"
// + "For smali/baksmali info, see: http://code.google.com/p/smali/");
} }
private static void setupLogging(Verbosity verbosity) { private static void setupLogging(Verbosity verbosity) {
@ -373,12 +415,31 @@ public class Main {
} }
} }
private static enum Verbosity { public static boolean isAdvanceMode() {
return advanceMode;
}
public static void setAdvanceMode(boolean advanceMode) {
Main.advanceMode = advanceMode;
}
private static enum Verbosity {
NORMAL, VERBOSE, QUIET; NORMAL, VERBOSE, QUIET;
} }
private static boolean Advanced = false; private static boolean advanceMode = false;
private final static Options normalOptions;
private final static Options DecodeOptions;
private final static Options BuildOptions;
static {
//normal and advance usage output
normalOptions = new Options();
BuildOptions = new Options();
DecodeOptions = new Options();
}
static class InvalidArgsError extends AndrolibException { static class InvalidArgsError extends AndrolibException {
} }