Skip to content

Commit

Permalink
Merge pull request #536 from oraveczandrew/resources
Browse files Browse the repository at this point in the history
resources load optimization
  • Loading branch information
Konloch authored Feb 28, 2025
2 parents 9c866ec + d8907ca commit 8708506
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!-- Dependency versions -->
<annotations.version>24.1.0</annotations.version>
<apktool.version>2.9.3</apktool.version>
<apktool.version>2.11.0</apktool.version>
<asm.version>9.7</asm.version>
<bined.version>0.2.1</bined.version>
<byteanalysis.version>1.0bcv</byteanalysis.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public void open(File file) throws Exception
// APK Resource Decoding Here
if (BytecodeViewer.viewer.decodeAPKResources.isSelected())
{
File decodedResources = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + ".apk");
APKTool.decodeResources(tempCopy, decodedResources, container);
container.resourceFiles = JarUtils.loadResources(decodedResources);
APKTool.decodeResources(tempCopy, container);
container.resourceFiles = JarUtils.loadResourcesFromFolder(APKTool.DECODED_RESOURCES, container.APKToolContents);
}

container.resourceFiles.putAll(JarUtils.loadResources(tempCopy)); // copy and rename
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/the/bytecode/club/bytecodeviewer/util/APKTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@
*/
public class APKTool
{
public static final String DECODED_RESOURCES = "Decoded Resources";

public static synchronized void decodeResources(File input, File output, ResourceContainer container)

public static synchronized void decodeResources(File input, ResourceContainer container)
{
try
{
File dir = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS + "Decoded Resources");
File dir = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS + DECODED_RESOURCES);
dir.mkdirs();

File tempAPKPath = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(12));
tempAPKPath.mkdirs();

brut.apktool.Main.main(new String[]{"r",
brut.apktool.Main.main(new String[] {
"r",
"--frame-path", tempAPKPath.getAbsolutePath(),
"d", input.getAbsolutePath(),
"-o", dir.getAbsolutePath(),
"-f"});

File zip = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(12) + ".zip");
ZipUtils.zipFolderAPKTool(dir.getAbsolutePath(), zip.getAbsolutePath());

if (zip.exists())
zip.renameTo(output);
"-f",
"-jobs",
String.valueOf(Runtime.getRuntime().availableProcessors())
});

container.APKToolContents = dir;
tempAPKPath.delete();
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,49 @@ public static List<ClassNode> loadClasses(File jarFile) throws IOException
return classes;
}

/**
* Loads resources only, just for .APK
*
* @param resourcesFolder the input resources folder
* @throws IOException
*/
public static Map<String, byte[]> loadResourcesFromFolder(String pathPrefix, File resourcesFolder) throws IOException
{
if (!resourcesFolder.exists())
return new LinkedHashMap<>(); // just ignore (don't return null for null-safety!)

Map<String, byte[]> files = new LinkedHashMap<>();

String rootPath = resourcesFolder.getAbsolutePath();
loadResourcesFromFolderImpl(rootPath, pathPrefix, files, resourcesFolder);

return files;
}

private static void loadResourcesFromFolderImpl(String rootPath, String pathPrefix, Map<String, byte[]> files, File folder) throws IOException
{
for (File file : folder.listFiles())
{
if (file.isDirectory())
loadResourcesFromFolderImpl(rootPath, pathPrefix, files, file);
else
{
final String name = file.getName();
if (!name.endsWith(".class") && !name.endsWith(".dex"))
{
String relativePath = pathPrefix + file.getAbsolutePath().substring(rootPath.length());
try (InputStream in = new FileInputStream(file))
{
files.put(relativePath, MiscUtils.getBytes(in));
} catch (Exception e)
{
BytecodeViewer.handleException(e);
}
}
}
}
}

/**
* Loads resources only, just for .APK
*
Expand Down

0 comments on commit 8708506

Please sign in to comment.