Is there a way to get a list of all classes from a .dex file?

JavaAndroidClassDexDx

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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 .

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionioreskovicView Question on Stackoverflow
Solution 1 - Javajcsahnwaldt Reinstate MonicaView Answer on Stackoverflow
Solution 2 - JavaJesusFrekeView Answer on Stackoverflow
Solution 3 - JavaUVMView Answer on Stackoverflow
Solution 4 - JavaJesusFrekeView Answer on Stackoverflow
Solution 5 - JavaHany SalemView Answer on Stackoverflow