Merge pull request #916 from iBotPeaches/issue_901

Support for emptying framework directory.
This commit is contained in:
Connor Tumbleson 2016-10-06 09:26:43 -04:00 committed by GitHub
commit 31ce5a315d
4 changed files with 63 additions and 12 deletions

View File

@ -48,7 +48,7 @@ public class Main {
// cli parser // cli parser
CommandLineParser parser = new PosixParser(); CommandLineParser parser = new PosixParser();
CommandLine commandLine = null; CommandLine commandLine;
// load options // load options
_Options(); _Options();
@ -57,7 +57,7 @@ public class Main {
commandLine = parser.parse(allOptions, args, false); commandLine = parser.parse(allOptions, args, false);
} catch (ParseException ex) { } catch (ParseException ex) {
System.err.println(ex.getMessage()); System.err.println(ex.getMessage());
usage(commandLine); usage();
return; return;
} }
@ -86,6 +86,9 @@ public class Main {
} else if (opt.equalsIgnoreCase("if") || opt.equalsIgnoreCase("install-framework")) { } else if (opt.equalsIgnoreCase("if") || opt.equalsIgnoreCase("install-framework")) {
cmdInstallFramework(commandLine); cmdInstallFramework(commandLine);
cmdFound = true; cmdFound = true;
} else if (opt.equalsIgnoreCase("empty-framework-dir")) {
cmdEmptyFrameworkDirectory(commandLine);
cmdFound = true;
} else if (opt.equalsIgnoreCase("publicize-resources")) { } else if (opt.equalsIgnoreCase("publicize-resources")) {
cmdPublicizeResources(commandLine); cmdPublicizeResources(commandLine);
cmdFound = true; cmdFound = true;
@ -97,7 +100,7 @@ public class Main {
if (commandLine.hasOption("version")) { if (commandLine.hasOption("version")) {
_version(); _version();
} else { } else {
usage(commandLine); usage();
} }
} }
} }
@ -107,7 +110,7 @@ public class Main {
int paraCount = cli.getArgList().size(); int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1); String apkName = (String) cli.getArgList().get(paraCount - 1);
File outDir = null; File outDir;
// check for options // check for options
if (cli.hasOption("s") || cli.hasOption("no-src")) { if (cli.hasOption("s") || cli.hasOption("no-src")) {
@ -224,8 +227,7 @@ public class Main {
new Androlib(apkOptions).build(new File(appDirName), outFile); new Androlib(apkOptions).build(new File(appDirName), outFile);
} }
private static void cmdInstallFramework(CommandLine cli) private static void cmdInstallFramework(CommandLine cli) throws AndrolibException {
throws AndrolibException {
int paraCount = cli.getArgList().size(); int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1); String apkName = (String) cli.getArgList().get(paraCount - 1);
@ -239,14 +241,26 @@ public class Main {
new Androlib(apkOptions).installFramework(new File(apkName)); new Androlib(apkOptions).installFramework(new File(apkName));
} }
private static void cmdPublicizeResources(CommandLine cli) private static void cmdPublicizeResources(CommandLine cli) throws AndrolibException {
throws AndrolibException {
int paraCount = cli.getArgList().size(); int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1); String apkName = (String) cli.getArgList().get(paraCount - 1);
new Androlib().publicizeResources(new File(apkName)); new Androlib().publicizeResources(new File(apkName));
} }
private static void cmdEmptyFrameworkDirectory(CommandLine cli) throws AndrolibException {
ApkOptions apkOptions = new ApkOptions();
if (cli.hasOption("f") || cli.hasOption("force")) {
apkOptions.forceDeleteFramework = true;
}
if (cli.hasOption("p") || cli.hasOption("frame-path")) {
apkOptions.frameworkFolderLocation = cli.getOptionValue("p");
}
new Androlib(apkOptions).emptyFrameworkDirectory();
}
private static void _version() { private static void _version() {
System.out.println(Androlib.getVersion()); System.out.println(Androlib.getVersion());
} }
@ -362,7 +376,6 @@ public class Main {
// check for advance mode // check for advance mode
if (isAdvanceMode()) { if (isAdvanceMode()) {
DecodeOptions.addOption(debugDecOption);
DecodeOptions.addOption(noDbgOption); DecodeOptions.addOption(noDbgOption);
DecodeOptions.addOption(keepResOption); DecodeOptions.addOption(keepResOption);
DecodeOptions.addOption(analysisOption); DecodeOptions.addOption(analysisOption);
@ -394,6 +407,10 @@ public class Main {
frameOptions.addOption(tagOption); frameOptions.addOption(tagOption);
frameOptions.addOption(frameIfDirOption); frameOptions.addOption(frameIfDirOption);
// add empty framework options
emptyFrameworkOptions.addOption(forceDecOption);
emptyFrameworkOptions.addOption(frameIfDirOption);
// add all, loop existing cats then manually add advance // add all, loop existing cats then manually add advance
for (Object op : normalOptions.getOptions()) { for (Object op : normalOptions.getOptions()) {
allOptions.addOption((Option)op); allOptions.addOption((Option)op);
@ -426,7 +443,7 @@ public class Main {
} }
} }
private static void usage(CommandLine commandLine) { private static void usage() {
// load basicOptions // load basicOptions
_Options(); _Options();
@ -452,8 +469,9 @@ public class Main {
formatter.printHelp("apktool " + verbosityHelp() + "d[ecode] [options] <file_apk>", DecodeOptions); formatter.printHelp("apktool " + verbosityHelp() + "d[ecode] [options] <file_apk>", DecodeOptions);
formatter.printHelp("apktool " + verbosityHelp() + "b[uild] [options] <app_path>", BuildOptions); formatter.printHelp("apktool " + verbosityHelp() + "b[uild] [options] <app_path>", BuildOptions);
if (isAdvanceMode()) { if (isAdvanceMode()) {
formatter.printHelp("apktool " + verbosityHelp() + "publicize-resources <file_path>", formatter.printHelp("apktool " + verbosityHelp() + "publicize-resources <file_path>", emptyOptions);
"Make all framework resources public.", emptyOptions, null); formatter.printHelp("apktool " + verbosityHelp() + "empty-framework-dir [options]", emptyFrameworkOptions);
System.out.println("");
} else { } else {
System.out.println(""); System.out.println("");
} }
@ -536,6 +554,7 @@ public class Main {
private final static Options frameOptions; private final static Options frameOptions;
private final static Options allOptions; private final static Options allOptions;
private final static Options emptyOptions; private final static Options emptyOptions;
private final static Options emptyFrameworkOptions;
static { static {
//normal and advance usage output //normal and advance usage output
@ -545,5 +564,6 @@ public class Main {
frameOptions = new Options(); frameOptions = new Options();
allOptions = new Options(); allOptions = new Options();
emptyOptions = new Options(); emptyOptions = new Options();
emptyFrameworkOptions = new Options();
} }
} }

View File

@ -684,6 +684,10 @@ public class Androlib {
mAndRes.installFramework(frameFile); mAndRes.installFramework(frameFile);
} }
public void emptyFrameworkDirectory() throws AndrolibException {
mAndRes.emptyFrameworkDirectory();
}
public boolean isFrameworkApk(ResTable resTable) { public boolean isFrameworkApk(ResTable resTable) {
for (ResPackage pkg : resTable.listMainPackages()) { for (ResPackage pkg : resTable.listMainPackages()) {
if (pkg.getId() < 64) { if (pkg.getId() < 64) {

View File

@ -19,6 +19,7 @@ import java.util.Collection;
public class ApkOptions { public class ApkOptions {
public boolean forceBuildAll = false; public boolean forceBuildAll = false;
public boolean forceDeleteFramework = false;
public boolean debugMode = false; public boolean debugMode = false;
public boolean verbose = false; public boolean verbose = false;
public boolean copyOriginalFiles = false; public boolean copyOriginalFiles = false;

View File

@ -591,6 +591,32 @@ final public class AndrolibResources {
throw new CantFindFrameworkResException(id); throw new CantFindFrameworkResException(id);
} }
public void emptyFrameworkDirectory() throws AndrolibException {
File dir = getFrameworkDir();
File apk;
apk = new File(dir, "1.apk");
if (! apk.exists()) {
LOGGER.warning("Can't empty framework directory, no file found at: " + apk.getAbsolutePath());
} else {
try {
if (apk.exists() && dir.listFiles().length > 1 && ! apkOptions.forceDeleteFramework) {
LOGGER.warning("More than default framework detected. Please run command with `--force` parameter to wipe framework directory.");
} else {
for (File file : dir.listFiles()) {
if (file.isFile() && file.getName().endsWith(".apk")) {
LOGGER.info("Removing " + file.getName() + " framework file...");
file.delete();
}
}
}
} catch (NullPointerException e) {
throw new AndrolibException(e);
}
}
}
public void installFramework(File frameFile) throws AndrolibException { public void installFramework(File frameFile) throws AndrolibException {
installFramework(frameFile, apkOptions.frameworkTag); installFramework(frameFile, apkOptions.frameworkTag);
} }