53

I have a .dex file, call it classes.dex.

Is there a way to "read" the contents of that classes.dex and get a list of all classes in there as full class names, including their package, com.mypackage.mysubpackage.MyClass, for exmaple?

I was thinking about com.android.dx.dex.file.DexFile, but I cannot seem to find a method for retrieving an entire set of classes.

ioreskovic
  • 5,531
  • 5
  • 39
  • 70

5 Answers5

125

Use the command line tool dexdump from the Android-SDK. It's in $ANDROID_HOME/build-tools/<some_version>/dexdump. It prints a lot more info than you probably want. I didn't find a way to make dexdump less verbose, but

dexdump classes.dex | grep 'Class descriptor'

should work.

raphinesse
  • 19,068
  • 6
  • 39
  • 48
  • 25
    I don't know if this is an OSX thing or if it's just because I have a different ADT version... but for me the dexdump tool is in sdk/build-tools// rather than under platform-tools/. – Troy Jan 14 '14 at 20:13
  • 7
    `dexdump classes.dex | findstr 'Class descriptor'` under windows. – naXa stands with Ukraine Jun 21 '14 at 00:17
  • # this is what I did on my Mac to use the latests dexdump sudo ln -s /source/android-sdks/build-tools/21.1.2/dexdump /usr/local/bin/dexdump – Keith John Hutchison Dec 28 '14 at 23:17
  • @naXa How to use this command? Where should the classes.dex be localed? - I can't fint it anywhere. – Morten Holmgaard Jan 25 '15 at 11:07
  • 1
    @MortenHolmgaard, in the current directory. classes.dex is packed inside apk file. Extract it, `cd` to the directory, where you have extracted it, and execute the command (in PowerShell). – naXa stands with Ukraine Jan 25 '15 at 13:23
  • 1
    Usage: dexdump: [-c] [-d] [-f] [-h] [-i] [-l layout] [-m] [-t tempfile] dexfile... -c : verify checksum and exit -d : disassemble code sections -f : display summary information from file header -h : display file header details -i : ignore checksum failures -l : output layout, either 'plain' or 'xml' -m : dump register maps (and nothing else) -t : temp file name (defaults to /sdcard/dex-temp-*) – Atishay Jan 28 '15 at 14:51
  • @csmu that's an interesting way to append a directory to `PATH` – TWiStErRob Apr 25 '15 at 16:32
  • @MortenHolmgaard you can get the 'classes.dex' file by unpacking your apk file using 'tar -xvf '. – Dan Borza May 21 '16 at 00:37
  • You can find the 'dexdump' executable in '/build-tools//' – Dan Borza May 21 '16 at 00:38
  • This has now moved to `platform-tools/(version)/dexdump`. – EntangledLoops Jun 17 '16 at 18:18
  • If you get `ERROR: Failed structural verification of 'classes.dex'`. You may be using a build tools version that's too old. I had to use `Android/sdk/build-tools/30.0.2/dexdump` instead of `Android/sdk/build-tools/29.0.3/dexdump` – Matt Nov 30 '20 at 15:53
17

You can use the dexlib2 library as a standalone library (available in maven), to read the dex file and get a list of classes.

DexFile dexFile = DexFileFactory.loadDexFile("classes.dex", 19 /*api level*/);
for (ClassDef classDef: dexFile.getClasses()) {
    System.out.println(classDef.getType());
}

Note that the class names will be of the form "Ljava/lang/String;", which is how they are stored in the dex file (and in a java class file). To convert, just remove the first and last letter, and replace / with .

JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • Actually, .class files are inconsistent with how classes are named in them. Sometimes they have the "L...;" form and sometimes it's just the name (but with slashes not dots). – danfuzz Jul 21 '12 at 01:14
  • Interesting! Didn't know that. – JesusFreke Jul 22 '12 at 22:47
  • Hey JesusFreke, you have a one character typo: it should be classdef.getType() instead of classDef.getType(). Thanks! – Alex Lipov Apr 02 '14 at 14:30
12

You can use dex2jar utility that will convert .dex to .jar.

http://code.google.com/p/dex2jar/

Then you can extract that .jar file.

Also , you can use this framework

Dedexer

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
UVM
  • 9,776
  • 6
  • 41
  • 66
5

baksmali has functionality to do this starting in baksmali v2.2.

baksmali list classes my.dex will print a list of all classes in the given dex file.

Reference: It is downloadable from here: https://github.com/JesusFreke/smali.

Peter Teoh
  • 6,337
  • 4
  • 42
  • 58
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
0

dxshow mydexfile.dex

dxshow:

strings -a $1 | grep "^L.*/" | grep -v "Ljava" | grep -v "Landroid" | sed "s/^L\(.*\);/\1/" | sed "s:/:.:g"

ezpz hack... didn't wanna spend a lifetime java coding

Hany Salem
  • 11
  • 1