Skip to content

Commit

Permalink
Merge pull request #916 from iBotPeaches/issue_901
Browse files Browse the repository at this point in the history
Support for emptying framework directory.
  • Loading branch information
iBotPeaches committed Oct 6, 2016
2 parents 466319a + 6361fa9 commit 31ce5a3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
44 changes: 32 additions & 12 deletions brut.apktool/apktool-cli/src/main/java/brut/apktool/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void main(String[] args) throws IOException, InterruptedException,

// cli parser
CommandLineParser parser = new PosixParser();
CommandLine commandLine = null;
CommandLine commandLine;

// load options
_Options();
Expand All @@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
commandLine = parser.parse(allOptions, args, false);
} catch (ParseException ex) {
System.err.println(ex.getMessage());
usage(commandLine);
usage();
return;
}

Expand Down Expand Up @@ -86,6 +86,9 @@ public static void main(String[] args) throws IOException, InterruptedException,
} else if (opt.equalsIgnoreCase("if") || opt.equalsIgnoreCase("install-framework")) {
cmdInstallFramework(commandLine);
cmdFound = true;
} else if (opt.equalsIgnoreCase("empty-framework-dir")) {
cmdEmptyFrameworkDirectory(commandLine);
cmdFound = true;
} else if (opt.equalsIgnoreCase("publicize-resources")) {
cmdPublicizeResources(commandLine);
cmdFound = true;
Expand All @@ -97,7 +100,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
if (commandLine.hasOption("version")) {
_version();
} else {
usage(commandLine);
usage();
}
}
}
Expand All @@ -107,7 +110,7 @@ private static void cmdDecode(CommandLine cli) throws AndrolibException {

int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1);
File outDir = null;
File outDir;

// check for options
if (cli.hasOption("s") || cli.hasOption("no-src")) {
Expand Down Expand Up @@ -224,8 +227,7 @@ private static void cmdBuild(CommandLine cli) throws BrutException {
new Androlib(apkOptions).build(new File(appDirName), outFile);
}

private static void cmdInstallFramework(CommandLine cli)
throws AndrolibException {
private static void cmdInstallFramework(CommandLine cli) throws AndrolibException {
int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1);

Expand All @@ -239,14 +241,26 @@ private static void cmdInstallFramework(CommandLine cli)
new Androlib(apkOptions).installFramework(new File(apkName));
}

private static void cmdPublicizeResources(CommandLine cli)
throws AndrolibException {
private static void cmdPublicizeResources(CommandLine cli) throws AndrolibException {
int paraCount = cli.getArgList().size();
String apkName = (String) cli.getArgList().get(paraCount - 1);

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() {
System.out.println(Androlib.getVersion());
}
Expand Down Expand Up @@ -362,7 +376,6 @@ private static void _Options() {

// check for advance mode
if (isAdvanceMode()) {
DecodeOptions.addOption(debugDecOption);
DecodeOptions.addOption(noDbgOption);
DecodeOptions.addOption(keepResOption);
DecodeOptions.addOption(analysisOption);
Expand Down Expand Up @@ -394,6 +407,10 @@ private static void _Options() {
frameOptions.addOption(tagOption);
frameOptions.addOption(frameIfDirOption);

// add empty framework options
emptyFrameworkOptions.addOption(forceDecOption);
emptyFrameworkOptions.addOption(frameIfDirOption);

// add all, loop existing cats then manually add advance
for (Object op : normalOptions.getOptions()) {
allOptions.addOption((Option)op);
Expand Down Expand Up @@ -426,7 +443,7 @@ private static String verbosityHelp() {
}
}

private static void usage(CommandLine commandLine) {
private static void usage() {

// load basicOptions
_Options();
Expand All @@ -452,8 +469,9 @@ private static void usage(CommandLine commandLine) {
formatter.printHelp("apktool " + verbosityHelp() + "d[ecode] [options] <file_apk>", DecodeOptions);
formatter.printHelp("apktool " + verbosityHelp() + "b[uild] [options] <app_path>", BuildOptions);
if (isAdvanceMode()) {
formatter.printHelp("apktool " + verbosityHelp() + "publicize-resources <file_path>",
"Make all framework resources public.", emptyOptions, null);
formatter.printHelp("apktool " + verbosityHelp() + "publicize-resources <file_path>", emptyOptions);
formatter.printHelp("apktool " + verbosityHelp() + "empty-framework-dir [options]", emptyFrameworkOptions);
System.out.println("");
} else {
System.out.println("");
}
Expand Down Expand Up @@ -536,6 +554,7 @@ private static enum Verbosity {
private final static Options frameOptions;
private final static Options allOptions;
private final static Options emptyOptions;
private final static Options emptyFrameworkOptions;

static {
//normal and advance usage output
Expand All @@ -545,5 +564,6 @@ private static enum Verbosity {
frameOptions = new Options();
allOptions = new Options();
emptyOptions = new Options();
emptyFrameworkOptions = new Options();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ public void installFramework(File frameFile)
mAndRes.installFramework(frameFile);
}

public void emptyFrameworkDirectory() throws AndrolibException {
mAndRes.emptyFrameworkDirectory();
}

public boolean isFrameworkApk(ResTable resTable) {
for (ResPackage pkg : resTable.listMainPackages()) {
if (pkg.getId() < 64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class ApkOptions {
public boolean forceBuildAll = false;
public boolean forceDeleteFramework = false;
public boolean debugMode = false;
public boolean verbose = false;
public boolean copyOriginalFiles = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,32 @@ public File getFrameworkApk(int id, String frameTag)
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 {
installFramework(frameFile, apkOptions.frameworkTag);
}
Expand Down

0 comments on commit 31ce5a3

Please sign in to comment.