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

refactor: clean up style and redundancy #3232

Merged
merged 2 commits into from
Jul 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import brut.util.OSDetection;
import org.apache.commons.cli.*;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.logging.*;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
import brut.util.AaptManager;
import brut.util.OS;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;

public class AaptInvoker {
Expand Down Expand Up @@ -81,8 +76,7 @@ private File createDoNotCompressExtensionsFile(ApkInfo apkInfo) throws AndrolibE
}

private void invokeAapt2(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include,
List<String> cmd, boolean customAapt)
throws AndrolibException {
List<String> cmd, boolean customAapt) throws AndrolibException {

List<String> compileCommand = new ArrayList<>(cmd);
File resourcesZip = null;
Expand Down Expand Up @@ -249,8 +243,7 @@ private void invokeAapt2(File apkFile, File manifest, File resDir, File rawDir,
}

private void invokeAapt1(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include,
List<String> cmd, boolean customAapt)
throws AndrolibException {
List<String> cmd, boolean customAapt) throws AndrolibException {

cmd.add("p");

Expand Down Expand Up @@ -365,7 +358,7 @@ private void invokeAapt1(File apkFile, File manifest, File resDir, File rawDir,
}

public void invokeAapt(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include)
throws AndrolibException {
throws AndrolibException {

String aaptPath = mConfig.aaptPath;
boolean customAapt = !aaptPath.isEmpty();
Expand Down
210 changes: 95 additions & 115 deletions brut.apktool/apktool-lib/src/main/java/brut/androlib/ApkBuilder.java

Large diffs are not rendered by default.

128 changes: 39 additions & 89 deletions brut.apktool/apktool-lib/src/main/java/brut/androlib/ApkDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
import com.android.tools.smali.dexlib2.iface.DexFile;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;
Expand All @@ -44,6 +43,7 @@ public class ApkDecoder {
private final Config mConfig;
private final ExtFile mApkFile;
protected final ResUnknownFiles mResUnknownFiles;
private ApkInfo mApkInfo;
private int mMinSdkVersion = 0;

private final static String SMALI_DIRNAME = "smali";
Expand All @@ -59,22 +59,22 @@ public class ApkDecoder {
"jpg|jpeg|png|gif|wav|mp2|mp3|ogg|aac|mpg|mpeg|mid|midi|smf|jet|rtttl|imy|xmf|mp4|" +
"m4a|m4v|3gp|3gpp|3g2|3gpp2|amr|awb|wma|wmv|webm|webp|mkv)$");

public ApkDecoder(File apkFile) {
this(Config.getDefaultConfig(), new ExtFile(apkFile));
}

public ApkDecoder(ExtFile apkFile) {
this(Config.getDefaultConfig(), apkFile);
}

public ApkDecoder(Config config, File apkFile) {
this(config, new ExtFile(apkFile));
}

public ApkDecoder(Config config, ExtFile apkFile) {
mConfig = config;
mResUnknownFiles = new ResUnknownFiles();
mApkFile = apkFile;
}

public ApkDecoder(File apkFile) {
this(new ExtFile(apkFile));
}

public ApkDecoder(Config config, File apkFile) {
this(config, new ExtFile(apkFile));
mResUnknownFiles = new ResUnknownFiles();
}

public ApkInfo decode(File outDir) throws AndrolibException, IOException, DirectoryException {
Expand All @@ -96,8 +96,11 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct

LOGGER.info("Using Apktool " + ApktoolProperties.getVersion() + " on " + mApkFile.getName());

ResourcesDecoder resourcesDecoder = new ResourcesDecoder(mConfig, mApkFile);
if (hasResources()) {
mApkInfo = new ApkInfo(mApkFile);

ResourcesDecoder resourcesDecoder = new ResourcesDecoder(mConfig, mApkFile, mApkInfo);

if (mApkInfo.hasResources()) {
switch (mConfig.decodeResources) {
case Config.DECODE_RESOURCES_NONE:
copyResourcesRaw(outDir);
Expand All @@ -108,7 +111,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
}
}

if (hasManifest()) {
if (mApkInfo.hasManifest()) {
if (mConfig.decodeResources == Config.DECODE_RESOURCES_FULL ||
mConfig.forceDecodeManifest == Config.FORCE_DECODE_MANIFEST_FULL) {
resourcesDecoder.decodeManifest(outDir);
Expand All @@ -119,7 +122,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
}
resourcesDecoder.updateApkInfo(outDir);

if (hasSources()) {
if (mApkInfo.hasSources()) {
switch (mConfig.decodeSources) {
case Config.DECODE_SOURCES_NONE:
copySourcesRaw(outDir, "classes.dex");
Expand All @@ -131,12 +134,12 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
}
}

if (hasMultipleSources()) {
if (mApkInfo.hasMultipleSources()) {
// foreach unknown dex file in root, lets disassemble it
Set<String> files = mApkFile.getDirectory().getFiles(true);
for (String file : files) {
if (file.endsWith(".dex")) {
if (! file.equalsIgnoreCase("classes.dex")) {
if (!file.equalsIgnoreCase("classes.dex")) {
switch(mConfig.decodeSources) {
case Config.DECODE_SOURCES_NONE:
copySourcesRaw(outDir, file);
Expand All @@ -158,73 +161,29 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
}

// In case we have no resources. We should store the minSdk we pulled from the source opcode api level
ApkInfo apkInfo = resourcesDecoder.getApkInfo();
if (!hasResources() && mMinSdkVersion > 0) {
apkInfo.setSdkInfoField("minSdkVersion", Integer.toString(mMinSdkVersion));
if (!mApkInfo.hasResources() && mMinSdkVersion > 0) {
mApkInfo.setSdkInfoField("minSdkVersion", Integer.toString(mMinSdkVersion));
}

copyRawFiles(outDir);
copyUnknownFiles(apkInfo, outDir);
List<String> mUncompressedFiles = new ArrayList<>();
recordUncompressedFiles(apkInfo, resourcesDecoder.getResFileMapping(), mUncompressedFiles);
copyUnknownFiles(outDir);
recordUncompressedFiles(resourcesDecoder.getResFileMapping());
copyOriginalFiles(outDir);
writeApkInfo(apkInfo, outDir);
writeApkInfo(outDir);

return apkInfo;
return mApkInfo;
} finally {
try {
mApkFile.close();
} catch (IOException ignored) {}
}
}

private boolean hasManifest() throws AndrolibException {
try {
return mApkFile.getDirectory().containsFile("AndroidManifest.xml");
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
}

private boolean hasResources() throws AndrolibException {
try {
return mApkFile.getDirectory().containsFile("resources.arsc");
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
}

private boolean hasSources() throws AndrolibException {
try {
return mApkFile.getDirectory().containsFile("classes.dex");
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
}

private boolean hasMultipleSources() throws AndrolibException {
try {
Set<String> files = mApkFile.getDirectory().getFiles(false);
for (String file : files) {
if (file.endsWith(".dex")) {
if (! file.equalsIgnoreCase("classes.dex")) {
return true;
}
}
}

return false;
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
private void writeApkInfo(File outDir) throws AndrolibException {
mApkInfo.save(new File(outDir, "apktool.yml"));
}

private void writeApkInfo(ApkInfo apkInfo, File outDir) throws AndrolibException {
apkInfo.save(new File(outDir, "apktool.yml"));
}

private void copyManifestRaw(File outDir)
throws AndrolibException {
private void copyManifestRaw(File outDir) throws AndrolibException {
try {
LOGGER.info("Copying raw manifest...");
mApkFile.getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
Expand All @@ -233,8 +192,7 @@ private void copyManifestRaw(File outDir)
}
}

private void copyResourcesRaw(File outDir)
throws AndrolibException {
private void copyResourcesRaw(File outDir) throws AndrolibException {
try {
LOGGER.info("Copying raw resources...");
mApkFile.getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
Expand All @@ -243,8 +201,7 @@ private void copyResourcesRaw(File outDir)
}
}

private void copySourcesRaw(File outDir, String filename)
throws AndrolibException {
private void copySourcesRaw(File outDir, String filename) throws AndrolibException {
try {
LOGGER.info("Copying raw " + filename + " file...");
mApkFile.getDirectory().copyToDir(outDir, filename);
Expand All @@ -253,8 +210,7 @@ private void copySourcesRaw(File outDir, String filename)
}
}

private void decodeSourcesSmali(File outDir, String filename)
throws AndrolibException {
private void decodeSourcesSmali(File outDir, String filename) throws AndrolibException {
try {
File smaliDir;
if (filename.equalsIgnoreCase("classes.dex")) {
Expand All @@ -277,8 +233,7 @@ private void decodeSourcesSmali(File outDir, String filename)
}
}

private void copyRawFiles(File outDir)
throws AndrolibException {
private void copyRawFiles(File outDir) throws AndrolibException {
LOGGER.info("Copying assets and libs...");
try {
Directory in = mApkFile.getDirectory();
Expand Down Expand Up @@ -311,8 +266,7 @@ private boolean isAPKFileNames(String file) {
return false;
}

private void copyUnknownFiles(ApkInfo apkInfo, File outDir)
throws AndrolibException {
private void copyUnknownFiles(File outDir) throws AndrolibException {
LOGGER.info("Copying unknown files...");
File unknownOut = new File(outDir, UNK_DIRNAME);
try {
Expand All @@ -331,14 +285,13 @@ private void copyUnknownFiles(ApkInfo apkInfo, File outDir)
}
}
// update apk info
apkInfo.unknownFiles = mResUnknownFiles.getUnknownFiles();
mApkInfo.unknownFiles = mResUnknownFiles.getUnknownFiles();
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
}

private void copyOriginalFiles(File outDir)
throws AndrolibException {
private void copyOriginalFiles(File outDir) throws AndrolibException {
LOGGER.info("Copying original files...");
File originalDir = new File(outDir, "original");
if (!originalDir.exists()) {
Expand Down Expand Up @@ -370,11 +323,9 @@ private void copyOriginalFiles(File outDir)
}
}

private void recordUncompressedFiles(ApkInfo apkInfo,
Map<String, String> resFileMapping,
List<String> uncompressedFilesOrExts)
throws AndrolibException {
private void recordUncompressedFiles(Map<String, String> resFileMapping) throws AndrolibException {
try {
List<String> uncompressedFilesOrExts = new ArrayList<>();
Directory unk = mApkFile.getDirectory();
Set<String> files = unk.getFiles(true);

Expand All @@ -398,9 +349,8 @@ private void recordUncompressedFiles(ApkInfo apkInfo,
}
// update apk info
if (!uncompressedFilesOrExts.isEmpty()) {
apkInfo.doNotCompress = uncompressedFilesOrExts;
mApkInfo.doNotCompress = uncompressedFilesOrExts;
}

} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
Expand Down
Loading