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

5

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.

Bearable answered 5/7, 2012 at 11:28 Comment(0)
H
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 .

Hong answered 5/7, 2012 at 18:54 Comment(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).Nylons
Interesting! Didn't know that.Hong
Hey JesusFreke, you have a one character typo: it should be classdef.getType() instead of classDef.getType(). Thanks!Swithin
C
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.

Cupellation answered 5/7, 2012 at 11:28 Comment(11)
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/.Accumulative
dexdump classes.dex | findstr 'Class descriptor' under windows.Minnesinger
# 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/dexdumpIodize
@naXa How to use this command? Where should the classes.dex be localed? - I can't fint it anywhere.Gurge
@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).Minnesinger
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-*)Roundtheclock
@csmu that's an interesting way to append a directory to PATHDiuresis
@MortenHolmgaard you can get the 'classes.dex' file by unpacking your apk file using 'tar -xvf <apkFile>'.Mulley
You can find the 'dexdump' executable in '<android-sdk-dir>/build-tools/<version>/'Mulley
This has now moved to platform-tools/(version)/dexdump.Pierre
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/dexdumpSissy
H
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 .

Hong answered 5/7, 2012 at 18:54 Comment(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).Nylons
Interesting! Didn't know that.Hong
Hey JesusFreke, you have a one character typo: it should be classdef.getType() instead of classDef.getType(). Thanks!Swithin
C
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

Chare answered 5/7, 2012 at 11:37 Comment(2)
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).Bearable
You can also unpack an 'apk' file by using 'tar -xvf <apk_file>'Mulley
H
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.

Hong answered 11/9, 2017 at 18:22 Comment(0)
P
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

Piracy answered 20/4, 2020 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.