Skip to content
Merged
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
24 changes: 12 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 @@ -91,16 +91,16 @@ private enum Verbosity { NORMAL, VERBOSE, QUIET }
.desc("Force delete destination directory.")
.get();

private static final Option decodeAllSrcOption = Option.builder("a")
.longOpt("all-src")
.desc("Decode all sources in the apk (includes unknown dex files).")
.get();

private static final Option decodeNoSrcOption = Option.builder("s")
.longOpt("no-src")
.desc("Do not decode sources.")
.get();

private static final Option decodeOnlyMainClassesOption = Option.builder()
.longOpt("only-main-classes")
.desc("Only disassemble the main dex classes (classes[0-9]*.dex) in the root.")
.get();

private static final Option decodeNoDebugInfoOption = Option.builder()
.longOpt("no-debug-info")
.desc("Do not include debug info in sources (.local, .param, .line, etc.)")
Expand Down Expand Up @@ -242,11 +242,11 @@ private static void loadOptions(Options options, boolean advanced) {
decodeOptions.addOption(jobsOption);
decodeOptions.addOption(libOption);
if (advanced) {
decodeOptions.addOption(decodeAllSrcOption);
decodeOptions.addOption(decodeKeepBrokenResOption);
decodeOptions.addOption(decodeMatchOriginalOption);
decodeOptions.addOption(decodeNoAssetsOption);
decodeOptions.addOption(decodeNoDebugInfoOption);
decodeOptions.addOption(decodeOnlyMainClassesOption);
decodeOptions.addOption(decodeOnlyManifestOption);
decodeOptions.addOption(decodeResResolveModeOption);
}
Expand Down Expand Up @@ -424,14 +424,14 @@ private static void cmdDecode(String[] args) throws AndrolibException {
if (cli.hasOption(decodeForceOption)) {
config.setForced(true);
}
if (cli.hasOption(decodeNoSrcOption)) {
config.setDecodeSources(Config.DecodeSources.NONE);
if (cli.hasOption(decodeAllSrcOption)) {
config.setDecodeSources(Config.DecodeSources.FULL);
}
if (cli.hasOption(decodeOnlyMainClassesOption)) {
if (cli.hasOption(decodeNoSrcOption)) {
printOptionConflict(decodeOnlyMainClassesOption, decodeNoSrcOption);
if (cli.hasOption(decodeNoSrcOption)) {
if (cli.hasOption(decodeAllSrcOption)) {
printOptionConflict(decodeNoSrcOption, decodeAllSrcOption);
} else {
config.setDecodeSources(Config.DecodeSources.ONLY_MAIN_CLASSES);
config.setDecodeSources(Config.DecodeSources.NONE);
}
}
if (cli.hasOption(decodeNoDebugInfoOption)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public class ApkBuilder {
private final AtomicReference<AndrolibException> mFirstError;

private ApkInfo mApkInfo;
private int mMinSdkVersion;
private SmaliBuilder mSmaliBuilder;
private AaptInvoker mAaptInvoker;
private BackgroundWorker mWorker;

public ApkBuilder(ExtFile apkDir, Config config) {
mApkDir = apkDir;
mConfig = config;
mFirstError = new AtomicReference<>(null);
mFirstError = new AtomicReference<>();
}

public void build(File outApk) throws AndrolibException {
Expand All @@ -65,11 +66,10 @@ public void build(File outApk) throws AndrolibException {
}
try {
mApkInfo = ApkInfo.load(mApkDir);

String minSdkVersion = mApkInfo.getSdkInfo().getMinSdkVersion();
if (minSdkVersion != null) {
mMinSdkVersion = SdkInfo.parseSdkInt(minSdkVersion);
}
mSmaliBuilder = new SmaliBuilder(minSdkVersion != null
? SdkInfo.parseSdkInt(minSdkVersion) : 0);
mAaptInvoker = new AaptInvoker(mApkInfo, mConfig);

String apkName = mApkInfo.getApkFileName();
if (apkName == null) {
Expand Down Expand Up @@ -141,7 +141,8 @@ private void buildSources(File outDir) throws AndrolibException {
if (dirName.equals("smali")) {
fileName = "classes.dex";
} else if (dirName.startsWith("smali_")) {
fileName = dirName.substring(dirName.indexOf('_') + 1) + ".dex";
fileName = dirName.substring(dirName.indexOf('_') + 1)
.replace('@', File.separatorChar) + ".dex";
} else {
continue;
}
Expand All @@ -157,11 +158,8 @@ private void buildSources(File outDir) throws AndrolibException {

private void copySourcesRaw(File outDir, String fileName) throws AndrolibException {
File inFile = new File(mApkDir, fileName);
if (!inFile.isFile()) {
return;
}

File outFile = new File(outDir, fileName);

if (!mConfig.isForced() && !isFileNewer(inFile, outFile)) {
LOGGER.info("File " + fileName + " has not changed.");
return;
Expand Down Expand Up @@ -193,20 +191,15 @@ private void buildSourcesSmali(File outDir, String dirName, String fileName) thr

private void buildSourcesSmaliJob(File outDir, String dirName, String fileName) throws AndrolibException {
File smaliDir = new File(mApkDir, dirName);
if (!smaliDir.isDirectory()) {
return;
}

File dexFile = new File(outDir, fileName);

if (!mConfig.isForced() && !isFileNewer(smaliDir, dexFile)) {
LOGGER.info("Sources in " + dirName + " have not changed.");
return;
}
OS.rmfile(dexFile);

LOGGER.info("Smaling " + dirName + " folder into " + fileName + "...");
SmaliBuilder builder = new SmaliBuilder(smaliDir, mMinSdkVersion);
builder.build(dexFile);
mSmaliBuilder.build(smaliDir, dexFile);
}

private void backupManifestFile(File manifest, File manifestOrig) throws AndrolibException {
Expand All @@ -219,8 +212,6 @@ private void backupManifestFile(File manifest, File manifestOrig) throws Androli
return;
}

OS.rmfile(manifestOrig);

try {
OS.cpfile(manifest, manifestOrig);
ResXmlUtils.fixingPublicAttrsInProviderAttributes(manifest);
Expand Down Expand Up @@ -272,40 +263,38 @@ private void copyResourcesRaw(File outDir, File manifest, File arscFile) throws
}

private void buildResourcesFull(File outDir, File manifest, File resDir) throws AndrolibException {
File resZip = new File(outDir.getParentFile(), "resources.zip");
if (!mConfig.isForced() && resZip.isFile()
if (!mConfig.isForced()
&& !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))
&& !isFileNewer(resDir, new File(outDir, "res"))) {
LOGGER.info("Resources have not changed.");
return;
}
OS.rmfile(resZip);

if (mConfig.isDebuggable()) {
LOGGER.info("Setting 'debuggable' attribute to 'true' in AndroidManifest.xml");
LOGGER.info("Setting 'debuggable' attribute to 'true' in AndroidManifest.xml...");
ResXmlUtils.setApplicationDebugTagTrue(manifest);
}

if (mConfig.isNetSecConf()) {
String targetSdkVersion = mApkInfo.getSdkInfo().getTargetSdkVersion();
if (targetSdkVersion != null && SdkInfo.parseSdkInt(targetSdkVersion) < ResConfig.SDK_NOUGAT) {
LOGGER.warning("Target SDK version is lower than 24! Network Security Configuration might be ignored!");
}

LOGGER.info("Adding permissive network security config in manifest...");
File netSecConfOrig = new File(mApkDir, "res/xml/network_security_config.xml");
OS.mkdir(netSecConfOrig.getParentFile());
ResXmlUtils.modNetworkSecurityConfig(netSecConfOrig);
ResXmlUtils.setNetworkSecurityConfig(manifest);
LOGGER.info("Added permissive network security config in manifest");

String targetSdkVersion = mApkInfo.getSdkInfo().getTargetSdkVersion();
if (targetSdkVersion != null && SdkInfo.parseSdkInt(targetSdkVersion) < ResConfig.SDK_NOUGAT) {
LOGGER.warning("Target SDK version is lower than 24, Network Security Configuration might be ignored!");
}
}

ExtFile tmpFile;
try {
tmpFile = new ExtFile(File.createTempFile("APKTOOL", null));
OS.rmfile(tmpFile);
} catch (IOException ex) {
throw new AndrolibException(ex);
}
OS.rmfile(tmpFile);

File npDir = new File(mApkDir, "9patch");
if (!npDir.isDirectory()) {
Expand All @@ -314,8 +303,7 @@ private void buildResourcesFull(File outDir, File manifest, File resDir) throws

LOGGER.info("Building resources with " + AaptManager.getBinaryName() + "...");
try {
AaptInvoker invoker = new AaptInvoker(mApkInfo, mConfig);
invoker.invoke(tmpFile, manifest, resDir, npDir, null, getIncludeFiles());
mAaptInvoker.invoke(tmpFile, manifest, resDir, npDir, null, getIncludeFiles());

Directory tmpDir = tmpFile.getDirectory();
tmpDir.copyToDir(outDir, "AndroidManifest.xml", "resources.arsc", "res");
Expand All @@ -336,10 +324,10 @@ private void buildManifest(File outDir, File manifest) throws AndrolibException
ExtFile tmpFile;
try {
tmpFile = new ExtFile(File.createTempFile("APKTOOL", null));
OS.rmfile(tmpFile);
} catch (IOException ex) {
throw new AndrolibException(ex);
}
OS.rmfile(tmpFile);

File npDir = new File(mApkDir, "9patch");
if (!npDir.isDirectory()) {
Expand All @@ -348,8 +336,7 @@ private void buildManifest(File outDir, File manifest) throws AndrolibException

LOGGER.info("Building AndroidManifest.xml with " + AaptManager.getBinaryName() + "...");
try {
AaptInvoker invoker = new AaptInvoker(mApkInfo, mConfig);
invoker.invoke(tmpFile, manifest, null, npDir, null, getIncludeFiles());
mAaptInvoker.invoke(tmpFile, manifest, null, npDir, null, getIncludeFiles());

Directory tmpDir = tmpFile.getDirectory();
tmpDir.copyToDir(outDir, "AndroidManifest.xml");
Expand Down Expand Up @@ -389,7 +376,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
Directory in = originalDir.getDirectory();

for (String fileName : in.getFiles(true)) {
if (ApkInfo.ORIGINAL_FILENAMES_PATTERN.matcher(fileName).matches()) {
if (ApkInfo.ORIGINAL_FILES_PATTERN.matcher(fileName).matches()) {
in.copyToDir(outDir, fileName);
}
}
Expand Down Expand Up @@ -417,7 +404,7 @@ private void buildApkFile(File outDir, File outApk) throws AndrolibException {
ZipUtils.zipDir(outDir, out, doNotCompress);

// Zip standard raw files.
for (String dirName : ApkInfo.RAW_DIRNAMES) {
for (String dirName : ApkInfo.RAW_DIRS) {
File rawDir = new File(mApkDir, dirName);
if (rawDir.isDirectory()) {
LOGGER.info("Importing " + dirName + "...");
Expand Down
Loading
Loading