Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Last active December 10, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylertreat/4464869 to your computer and use it in GitHub Desktop.
Save tylertreat/4464869 to your computer and use it in GitHub Desktop.
Method used to extract classes.dex from an APK and copy it to a cache directory for processing.
private void extractDex(String apk) {
try {
JarFile file = new JarFile(apk);
Enumeration<JarEntry> entries = file.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().equals("classes.dex")) {
File cache = getDir("dex_cache", Context.MODE_PRIVATE);
if (!cache.exists())
cache.mkdirs();
File classes = new File(cache + File.separator + "classes.dex");
InputStream stream = file.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(classes);
while (stream.available() > 0)
fos.write(stream.read());
fos.close();
stream.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment