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

in the ApkDecoder use apkFile and unknownFiles from ApkInfo #3242

Merged
merged 2 commits into from
Aug 1, 2023
Merged
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
@@ -21,8 +21,6 @@
import brut.androlib.exceptions.OutDirExistsException;
import brut.androlib.apk.ApkInfo;
import brut.androlib.res.ResourcesDecoder;
import brut.androlib.res.data.*;
import brut.androlib.res.xml.ResXmlPatcher;
import brut.androlib.src.SmaliDecoder;
import brut.directory.Directory;
import brut.directory.ExtFile;
@@ -41,9 +39,7 @@ public class ApkDecoder {
private final static Logger LOGGER = Logger.getLogger(ApkDecoder.class.getName());

private final Config mConfig;
private final ExtFile mApkFile;
protected final ResUnknownFiles mResUnknownFiles;
private ApkInfo mApkInfo;
private final ApkInfo mApkInfo;
private int mMinSdkVersion = 0;

private final static String SMALI_DIRNAME = "smali";
@@ -73,17 +69,17 @@ public ApkDecoder(Config config, File apkFile) {

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

public ApkInfo decode(File outDir) throws AndrolibException, IOException, DirectoryException {
ExtFile apkFile = mApkInfo.getApkFile();
try {
if (!mConfig.forceDelete && outDir.exists()) {
throw new OutDirExistsException();
}

if (!mApkFile.isFile() || !mApkFile.canRead()) {
if (!apkFile.isFile() || !apkFile.canRead()) {
throw new InFileNotFoundException();
}

@@ -94,11 +90,9 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
}
outDir.mkdirs();

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

mApkInfo = new ApkInfo(mApkFile);

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

if (mApkInfo.hasResources()) {
switch (mConfig.decodeResources) {
@@ -136,7 +130,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct

if (mApkInfo.hasMultipleSources()) {
// foreach unknown dex file in root, lets disassemble it
Set<String> files = mApkFile.getDirectory().getFiles(true);
Set<String> files = apkFile.getDirectory().getFiles(true);
for (String file : files) {
if (file.endsWith(".dex")) {
if (!file.equalsIgnoreCase("classes.dex")) {
@@ -174,7 +168,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
return mApkInfo;
} finally {
try {
mApkFile.close();
apkFile.close();
} catch (IOException ignored) {}
}
}
@@ -186,7 +180,7 @@ private void writeApkInfo(File outDir) throws AndrolibException {
private void copyManifestRaw(File outDir) throws AndrolibException {
try {
LOGGER.info("Copying raw manifest...");
mApkFile.getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
@@ -195,7 +189,7 @@ private void copyManifestRaw(File outDir) throws AndrolibException {
private void copyResourcesRaw(File outDir) throws AndrolibException {
try {
LOGGER.info("Copying raw resources...");
mApkFile.getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
@@ -204,7 +198,7 @@ private void copyResourcesRaw(File outDir) throws AndrolibException {
private void copySourcesRaw(File outDir, String filename) throws AndrolibException {
try {
LOGGER.info("Copying raw " + filename + " file...");
mApkFile.getDirectory().copyToDir(outDir, filename);
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, filename);
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
@@ -222,7 +216,7 @@ private void decodeSourcesSmali(File outDir, String filename) throws AndrolibExc
//noinspection ResultOfMethodCallIgnored
smaliDir.mkdirs();
LOGGER.info("Baksmaling " + filename + "...");
DexFile dexFile = SmaliDecoder.decode(mApkFile, smaliDir, filename,
DexFile dexFile = SmaliDecoder.decode(mApkInfo.getApkFile(), smaliDir, filename,
mConfig.baksmaliDebugMode, mConfig.apiLevel);
int minSdkVersion = dexFile.getOpcodes().api;
if (mMinSdkVersion == 0 || mMinSdkVersion > minSdkVersion) {
@@ -236,7 +230,7 @@ private void decodeSourcesSmali(File outDir, String filename) throws AndrolibExc
private void copyRawFiles(File outDir) throws AndrolibException {
LOGGER.info("Copying assets and libs...");
try {
Directory in = mApkFile.getDirectory();
Directory in = mApkInfo.getApkFile().getDirectory();

if (mConfig.decodeAssets == Config.DECODE_ASSETS_FULL) {
if (in.containsDir("assets")) {
@@ -270,7 +264,7 @@ private void copyUnknownFiles(File outDir) throws AndrolibException {
LOGGER.info("Copying unknown files...");
File unknownOut = new File(outDir, UNK_DIRNAME);
try {
Directory unk = mApkFile.getDirectory();
Directory unk = mApkInfo.getApkFile().getDirectory();

// loop all items in container recursively, ignoring any that are pre-defined by aapt
Set<String> files = unk.getFiles(true);
@@ -281,11 +275,9 @@ private void copyUnknownFiles(File outDir) throws AndrolibException {
unk.copyToDir(unknownOut, file);
// let's record the name of the file, and its compression type
// so that we may re-include it the same way
mResUnknownFiles.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
mApkInfo.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
}
}
// update apk info
mApkInfo.unknownFiles = mResUnknownFiles.getUnknownFiles();
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
@@ -300,7 +292,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
}

try {
Directory in = mApkFile.getDirectory();
Directory in = mApkInfo.getApkFile().getDirectory();
if (in.containsFile("AndroidManifest.xml")) {
in.copyToDir(originalDir, "AndroidManifest.xml");
}
@@ -326,7 +318,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
private void recordUncompressedFiles(Map<String, String> resFileMapping) throws AndrolibException {
try {
List<String> uncompressedFilesOrExts = new ArrayList<>();
Directory unk = mApkFile.getDirectory();
Directory unk = mApkInfo.getApkFile().getDirectory();
Set<String> files = unk.getFiles(true);

for (String file : files) {
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public class ApkInfo implements YamlSerializable {
public boolean resourcesAreCompressed;
public boolean sharedLibrary;
public boolean sparseResources;
public Map<String, String> unknownFiles;
public Map<String, String> unknownFiles = new LinkedHashMap<>();
public List<String> doNotCompress;

/** @deprecated use {@link #resourcesAreCompressed} */
@@ -120,6 +120,10 @@ public boolean hasMultipleSources() throws AndrolibException {
}
}

public void addUnknownFileInfo(String file, String value) {
unknownFiles.put(file, value);
}

public String checkTargetSdkVersionBounds() {
int target = mapSdkShorthandToVersion(getTargetSdkVersion());

@@ -295,7 +299,9 @@ public void write(YamlWriter writer) {
writer.writeBool("resourcesAreCompressed", resourcesAreCompressed);
writer.writeBool("sharedLibrary", sharedLibrary);
writer.writeBool("sparseResources", sparseResources);
writer.writeStringMap("unknownFiles", unknownFiles);
if (unknownFiles.size() > 0) {
iBotPeaches marked this conversation as resolved.
Show resolved Hide resolved
writer.writeStringMap("unknownFiles", unknownFiles);
}
writer.writeList("doNotCompress", doNotCompress);
}
}
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@
import brut.androlib.res.xml.ResXmlPatcher;
import brut.directory.Directory;
import brut.directory.DirectoryException;
import brut.directory.ExtFile;
import brut.directory.FileDirectory;
import org.xmlpull.v1.XmlSerializer;

@@ -39,7 +38,6 @@ public class ResourcesDecoder {
private final static Logger LOGGER = Logger.getLogger(ResourcesDecoder.class.getName());

private final Config mConfig;
private final ExtFile mApkFile;
private final ApkInfo mApkInfo;
private final ResTable mResTable;
private final Map<String, String> mResFileMapping = new HashMap<>();
@@ -48,9 +46,8 @@ public class ResourcesDecoder {
"android", "com.htc", "com.lge", "com.lge.internal", "yi", "flyme", "air.com.adobe.appentry",
"FFFFFFFFFFFFFFFFFFFFFF" };

public ResourcesDecoder(Config config, ExtFile apkFile, ApkInfo apkInfo) {
public ResourcesDecoder(Config config, ApkInfo apkInfo) {
mConfig = config;
mApkFile = apkFile;
mApkInfo = apkInfo;
mResTable = new ResTable(mConfig, mApkInfo);
}
@@ -68,7 +65,7 @@ public Map<String, String> getResFileMapping() {
}

public void loadMainPkg() throws AndrolibException {
mResTable.loadMainPkg(mApkFile);
mResTable.loadMainPkg(mApkInfo.getApkFile());
}

public void decodeManifest(File outDir) throws AndrolibException {
@@ -81,7 +78,7 @@ public void decodeManifest(File outDir) throws AndrolibException {

Directory inApk, out;
try {
inApk = mApkFile.getDirectory();
inApk = mApkInfo.getApkFile().getDirectory();
out = new FileDirectory(outDir);

if (mApkInfo.hasResources()) {
@@ -147,7 +144,7 @@ public void decodeResources(File outDir) throws AndrolibException {
return;
}

mResTable.loadMainPkg(mApkFile);
mResTable.loadMainPkg(mApkInfo.getApkFile());

ResStreamDecoderContainer decoders = new ResStreamDecoderContainer();
decoders.setDecoder("raw", new ResRawStreamDecoder());
@@ -161,7 +158,7 @@ public void decodeResources(File outDir) throws AndrolibException {

try {
out = new FileDirectory(outDir);
in = mApkFile.getDirectory();
in = mApkInfo.getApkFile().getDirectory();
out = out.createDir("res");
} catch (DirectoryException ex) {
throw new AndrolibException(ex);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ public static void beforeClass() throws Exception {

LOGGER.info("Decoding pkgid8.apk...");
ApkInfo testInfo = new ApkInfo(testApk);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), testApk, testInfo);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), testInfo);

sTestNewDir.mkdirs();
resourcesDecoder.decodeResources(sTestNewDir);
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ public void decodeStringArray() throws BrutException {
ExtFile apkFile = new ExtFile(sTmpDir, "issue1994.apk");
ApkInfo apkInfo = new ApkInfo(apkFile);
//ApkDecoder apkDecoder = new ApkDecoder(apkFile);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkFile, apkInfo);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkInfo);

resourcesDecoder.loadMainPkg();
ResTable resTable = resourcesDecoder.getResTable();
@@ -68,7 +68,7 @@ public void decodeStringArray() throws BrutException {
public void decodeArray() throws BrutException {
ExtFile apkFile = new ExtFile(sTmpDir, "issue1994.apk");
ApkInfo apkInfo = new ApkInfo(apkFile);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkFile, apkInfo);
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkInfo);

resourcesDecoder.loadMainPkg();
ResTable resTable = resourcesDecoder.getResTable();