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.

5 Answers 5

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.

11
  • 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/<some_version>/ rather than under platform-tools/.
    – Troy
    Jan 14, 2014 at 20:13
  • 7
    dexdump classes.dex | findstr 'Class descriptor' under windows. Jun 21, 2014 at 0: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 Dec 28, 2014 at 23:17
  • 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). Jan 25, 2015 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, 2015 at 14:51
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 .

3
  • 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, 2012 at 1:14
  • Interesting! Didn't know that.
    – JesusFreke
    Jul 22, 2012 at 22:47
  • Hey JesusFreke, you have a one character typo: it should be classdef.getType() instead of classDef.getType(). Thanks!
    – Alex Lipov
    Apr 2, 2014 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

2
  • 3
    I don't need to (nor I want to) physically decompile and extract the .dex. I only need a set of full class names (Strings).
    – ioreskovic
    Jul 5, 2012 at 11:42
  • You can also unpack an 'apk' file by using 'tar -xvf <apk_file>'
    – Dan Borza
    May 21, 2016 at 0:39
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.