Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support aapt optimization #3558

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions brut.apktool/apktool-cli/src/main/java/brut/apktool/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ private static void cmdBuild(CommandLine cli, Config config) {
if (cli.hasOption("nc") || cli.hasOption("no-crunch")) {
config.noCrunch = true;
}
if (cli.hasOption("srp") || cli.hasOption("shorten-res-paths")) {
config.shortenResPaths = true;
}
if (cli.hasOption("ese") || cli.hasOption("enable-sparse-encoding")) {
config.enableSparseEncoding = true;
}
if (cli.hasOption("crn") || cli.hasOption("collapse-res-names")) {
config.collapseResNames = true;
}
if (cli.hasOption("use-aapt1")) {
config.useAapt2 = false;
}
Expand All @@ -286,8 +295,8 @@ private static void cmdBuild(CommandLine cli, Config config) {
outFile = null;
}

if (config.netSecConf && !config.useAapt2) {
System.err.println("-n / --net-sec-conf is only supported with --use-aapt2.");
if ((config.netSecConf || config.shortenResPaths || config.enableSparseEncoding || config.collapseResNames) && !config.useAapt2) {
System.err.println("-n / --net-sec-conf, -srp / --shorten-res-paths, -ese / --enable-sparse-encoding, -crn / --collapse-res-names are only supported with --use-aapt2.");
System.exit(1);
}

Expand Down Expand Up @@ -481,6 +490,21 @@ private static void _options() {
.desc("Disable crunching of resource files during the build step.")
.build();

Option shortenResPathsOption = Option.builder("srp")
.longOpt("shorten-res-paths")
.desc("Shortens the paths of resources inside the APK.")
.build();

Option enableSparseEncodingOption = Option.builder("ese")
.longOpt("enable-sparse-encoding")
.desc("Enables encoding of sparse entries using a binary search tree. This option is useful for optimization of APK size but at the cost of resource retrieval performance.")
.build();

Option collapseResNamesOption = Option.builder("crn")
.longOpt("collapse-res-names")
.desc("Collapses resource names to a single value in the key string pool.")
.build();

Option tagOption = Option.builder("t")
.longOpt("tag")
.desc("Tag frameworks using <tag>.")
Expand Down Expand Up @@ -530,6 +554,9 @@ private static void _options() {
buildOptions.addOption(originalOption);
buildOptions.addOption(aapt1Option);
buildOptions.addOption(noCrunchOption);
buildOptions.addOption(shortenResPathsOption);
buildOptions.addOption(enableSparseEncodingOption);
buildOptions.addOption(collapseResNamesOption);
}

// add global options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import brut.util.OS;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.logging.Logger;

Expand Down Expand Up @@ -243,6 +246,45 @@ private void invokeAapt2(File apkFile, File manifest, File resDir, File rawDir,
} catch (BrutException ex) {
throw new AndrolibException(ex);
}

if (mConfig.shortenResPaths || mConfig.enableSparseEncoding || mConfig.collapseResNames) {
Path inputFilePath = new File(apkFile.getParent(), apkFile.getName() + ".tmp").toPath();
Path apkFilePath = apkFile.toPath();
try {
Files.copy(apkFilePath, inputFilePath, StandardCopyOption.REPLACE_EXISTING);
Files.delete(apkFilePath);
} catch (IOException e) {
throw new AndrolibException(e);
}

cmd = new ArrayList<>(compileCommand);
cmd.add("optimize");

cmd.add("-o");
cmd.add(apkFilePath.toString());

if (mConfig.shortenResPaths) {
cmd.add("--shorten-resource-paths");
}

if (mConfig.enableSparseEncoding) {
cmd.add("--enable-sparse-encoding");
}

if (mConfig.collapseResNames) {
cmd.add("--collapse-resource-names");
}

cmd.add(inputFilePath.toString());

try {
OS.exec(cmd.toArray(new String[0]));
LOGGER.fine("aapt2 optimize command ran: ");
LOGGER.fine(cmd.toString());
} catch (BrutException ex) {
throw new AndrolibException(ex);
}
}
}

private void invokeAapt1(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class Config {
public boolean useAapt2 = true;
public boolean noCrunch = false;

// Optimize options, only supported by aapt2.
// see https://developer.android.com/tools/aapt2#optimize_options
public boolean shortenResPaths = false;
public boolean enableSparseEncoding = false;
public boolean collapseResNames = false;

// Decode options
public short decodeSources = DECODE_SOURCES_SMALI;
public short decodeResources = DECODE_RESOURCES_FULL;
Expand Down