Skip to content

Commit d8907ca

Browse files
committed
skip jar compression & decompression when decoding resources, run apktool on multiple thread
1 parent 5f8fd42 commit d8907ca

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<!-- Dependency versions -->
1616
<annotations.version>24.1.0</annotations.version>
17-
<apktool.version>2.9.3</apktool.version>
17+
<apktool.version>2.11.0</apktool.version>
1818
<asm.version>9.7</asm.version>
1919
<bined.version>0.2.1</bined.version>
2020
<byteanalysis.version>1.0bcv</byteanalysis.version>

src/main/java/the/bytecode/club/bytecodeviewer/resources/importing/impl/APKResourceImporter.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ public void open(File file) throws Exception
4848
// APK Resource Decoding Here
4949
if (BytecodeViewer.viewer.decodeAPKResources.isSelected())
5050
{
51-
File decodedResources = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + ".apk");
52-
APKTool.decodeResources(tempCopy, decodedResources, container);
53-
container.resourceFiles = JarUtils.loadResources(decodedResources);
51+
APKTool.decodeResources(tempCopy, container);
52+
container.resourceFiles = JarUtils.loadResourcesFromFolder(APKTool.DECODED_RESOURCES, container.APKToolContents);
5453
}
5554

5655
container.resourceFiles.putAll(JarUtils.loadResources(tempCopy)); // copy and rename

src/main/java/the/bytecode/club/bytecodeviewer/util/APKTool.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@
3232
*/
3333
public class APKTool
3434
{
35+
public static final String DECODED_RESOURCES = "Decoded Resources";
3536

36-
public static synchronized void decodeResources(File input, File output, ResourceContainer container)
37+
38+
public static synchronized void decodeResources(File input, ResourceContainer container)
3739
{
3840
try
3941
{
40-
File dir = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS + "Decoded Resources");
42+
File dir = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(32) + FS + DECODED_RESOURCES);
4143
dir.mkdirs();
4244

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

46-
brut.apktool.Main.main(new String[]{"r",
48+
brut.apktool.Main.main(new String[] {
49+
"r",
4750
"--frame-path", tempAPKPath.getAbsolutePath(),
4851
"d", input.getAbsolutePath(),
4952
"-o", dir.getAbsolutePath(),
50-
"-f"});
51-
52-
File zip = new File(TEMP_DIRECTORY + FS + MiscUtils.randomString(12) + ".zip");
53-
ZipUtils.zipFolderAPKTool(dir.getAbsolutePath(), zip.getAbsolutePath());
54-
55-
if (zip.exists())
56-
zip.renameTo(output);
53+
"-f",
54+
"-jobs",
55+
String.valueOf(Runtime.getRuntime().availableProcessors())
56+
});
5757

5858
container.APKToolContents = dir;
5959
tempAPKPath.delete();

src/main/java/the/bytecode/club/bytecodeviewer/util/JarUtils.java

+43
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,49 @@ public static List<ClassNode> loadClasses(File jarFile) throws IOException
230230
return classes;
231231
}
232232

233+
/**
234+
* Loads resources only, just for .APK
235+
*
236+
* @param resourcesFolder the input resources folder
237+
* @throws IOException
238+
*/
239+
public static Map<String, byte[]> loadResourcesFromFolder(String pathPrefix, File resourcesFolder) throws IOException
240+
{
241+
if (!resourcesFolder.exists())
242+
return new LinkedHashMap<>(); // just ignore (don't return null for null-safety!)
243+
244+
Map<String, byte[]> files = new LinkedHashMap<>();
245+
246+
String rootPath = resourcesFolder.getAbsolutePath();
247+
loadResourcesFromFolderImpl(rootPath, pathPrefix, files, resourcesFolder);
248+
249+
return files;
250+
}
251+
252+
private static void loadResourcesFromFolderImpl(String rootPath, String pathPrefix, Map<String, byte[]> files, File folder) throws IOException
253+
{
254+
for (File file : folder.listFiles())
255+
{
256+
if (file.isDirectory())
257+
loadResourcesFromFolderImpl(rootPath, pathPrefix, files, file);
258+
else
259+
{
260+
final String name = file.getName();
261+
if (!name.endsWith(".class") && !name.endsWith(".dex"))
262+
{
263+
String relativePath = pathPrefix + file.getAbsolutePath().substring(rootPath.length());
264+
try (InputStream in = new FileInputStream(file))
265+
{
266+
files.put(relativePath, MiscUtils.getBytes(in));
267+
} catch (Exception e)
268+
{
269+
BytecodeViewer.handleException(e);
270+
}
271+
}
272+
}
273+
}
274+
}
275+
233276
/**
234277
* Loads resources only, just for .APK
235278
*

0 commit comments

Comments
 (0)